473,382 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

$_SESSION in <select> element

118 100+
Hi all,

On my current project MyChatbox I have a box for which the user enters a name and the value of which is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Name: <input type="textbox" name="username" onkeyup="countdown(username, 10, used)" maxlength="10" size="10" value="<?php echo $_SESSION['username']; ?>" /> 
  3.  
  4.  
So everytime they go to that page, or if the page refreshes; if they haven't closed the browser, the username they pick will stay in the box.

I also have a drop down list to pick a colour for the font.

The drop down is as follows:

Expand|Select|Wrap|Line Numbers
  1.             Select Text color:
  2.               <select  name="color">
  3.     <option value="black" >black</option>
  4.     <option value="white" >white</option>
  5.     <option value="blue">blue</option>
  6.     <option value="red" >Red</option>
  7.     <option value="yellow" >yellow</option>
  8.     <option value="green">green</option>
  9.   </select>
  10.  
Is there a way that I could get it with the $_SESSION tag within the select element so that the same colour is selected on each session for the user?

Thanks,
Sam
Oct 9 '07 #1
1 1936
Atli
5,058 Expert 4TB
Hi Sam.

You could add the color to the session the way you did with the username, and have PHP print out the options for the <select> box, adding the 'selected' property to the <option> tag you want selected.

Something like:
Expand|Select|Wrap|Line Numbers
  1. # Create array of options
  2. $boxes = array();
  3. $boxes[] = "blue";
  4. $boxes[] = "green";
  5. $boxes[] = "red";
  6.  
  7. # The selected box, this would be what you store in Session.
  8. $selectedBox = "red";
  9.  
  10. # Loop through each box and print an <option> tag
  11. foreach($boxes as $box) {
  12.   if($box == $selectedBox) {
  13.     # Selected box
  14.     echo "<option selected=\"true\" value=\"$box\">$box</option>";
  15.   }
  16.   else {
  17.     # Not selected
  18.     echo "<option value=\"$box\">$box</option>";
  19.   }
  20. }
  21.  
Oct 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Hal Vaughan | last post by:
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton) that removes or adds a choice to a <SELECT> element. The <FORM> is form1 and the <SELECT> is theDay. The example uses...
3
by: Dennis M. Marks | last post by:
I have a problem with the following code. It generates a <FORM><SELECT><OPTION> list. There is no problem in the generating. The problem is in the execution as follows. It works fine in Mac IE...
9
by: aragon | last post by:
Hi all, is it legal to use a fully-numeric string as "name" attribute for the <select> tag in the XHTML standard? E.g.: <form action="action.php" method="get"> <select name="12345"> <option...
3
by: gekoblu | last post by:
Hi!, I want to fix via javascript the combo width to a fix value. I'd like to implement a kind of ALT / TITLE function to show the entire option when the text is longer than the combo width......
2
by: abs | last post by:
Hello everybody. A piece of html code: <form ... > <select ....> <option .... <option .... </select> <select ....>
5
by: Brian Foley | last post by:
Hello, I am used to using the label tag with check boxes and radio buttons in html forms. This allows me to click on the text of the label to activate/deactivate the check box / button, rather...
6
by: joseph.lindley | last post by:
Forgive me for I am a bit of a web-dev novice - but I'm not doing too bad. I'm currently working with a bit of javascript to dynamically add <option>s into a select box. My code currently works...
7
by: Alex Maghen | last post by:
I have some client-side JavaScript that I want to run whenever a pulldown <SELECT> is changes on th client. I'm trying to do this as follows... <select id="MyPulldown"...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.