Page 1 of 1

How to store the selected item from a drop down list

Posted: Sun Jan 23, 2011 12:11 pm
by tsalaki
Hi to all,

I have the following problem. I have made a drop down list which is filled with items from my database. I have written the following script in order to show this list which is selected the item that corresponds to the specified registered user.

Code: Select all

<select name="description" class="textfield"> 
            <?php 
				$sql = "SELECT * FROM specialisation";
				$rs = mysql_query($sql);
           		while($thisRow = mysql_fetch_array($rs)){ 
            ?> 
            <option value="<?php echo $thisRow["id_specialisation"]; ?>" <?php if($thisRow["id_specialisation"] == $row["description_id"]) echo('selected="selected"'); ?>><?php echo $thisRow["desc_specialisation"]; ?></option> 
             <?php } ?> 
         </select>  
With this code I can show the list which is selected the item that corresponds to each user. What I want is in case user change this item value to other to store this value in a Session variable in order to store it afterwards in my database.

Re: How to store the selected item from a drop down list

Posted: Sun Jan 23, 2011 2:01 pm
by Jade
Okay, so then you need to put your select box into a form, add a submit button and if they submit the form you update the session variable. It will look something like this:

Code: Select all

<?php
session_start();

if ($_POST['submit'])
    $_SESSION['selectBox'] = $_POST['select_box_name'];
?>
<form action="#" method="post">
Pick A Number: <select name="select_box_name">
  <option value=1>One</option>
  <option value=2>Two</option>
</select>
<input type="submit" name="submit" value="Save Changes" />
</form>


Re: How to store the selected item from a drop down list

Posted: Mon Jan 24, 2011 3:11 am
by tsalaki
Hi,

I have put the selection box into a form with a submit button. What I don't know is how can I store a change in the selection box. I don't know how can I search which item is selected so as to store it to a session variable and then clicking in the submit button store the changes in the database. If it's not clear to you what I mean I can put the whole code I have written in order to be more clear.

Re: How to store the selected item from a drop down list

Posted: Mon Jan 24, 2011 4:10 am
by JustPlainJef
Taking Jade's code....

Code: Select all

<?php
session_start();

if ($_POST['submit'])
    $_SESSION['selectBox'] = $_POST['select_box_name'];
?>
<form action="#" method="post">
Pick A Number: <select name="select_box_name">
  <option value=1>One</option>
  <option value=2>Two</option>
</select>
<input type="submit" name="submit" value="Save Changes" />
</form>

 
The IF statement checks to see if the form has been submitted. I'm thinking that Jade is re-opening the same page when the submit button is pressed. If they have clicked submit, the same page opens, but the value from your select box ($_POST['select_box_name']) is stored in $_SESSION['selectBox'].

Do some reading into the $_POST variable. Here's a brief start. http://www.w3schools.com/php/php_post.asp

Re: How to store the selected item from a drop down list

Posted: Mon Jan 24, 2011 4:30 am
by tsalaki
Thanks a lot for your help I will stare reading about $_POST.. :)

Re: How to store the selected item from a drop down list

Posted: Mon Jan 31, 2011 7:39 am
by Jade
Exactly. If you print out the value of $_SESSION['selectBox'] you'll see that it changes and saves whatever they last had in there.

Code: Select all

<?php
session_start();

if ($_POST['submit'])
    $_SESSION['selectBox'] = $_POST['select_box_name'];

echo "Your most recent selection was: " . $_SESSION['selectBox'] . "<br/><br/>";
?>
<form action="#" method="post">
Pick A Number: <select name="select_box_name">
  <option value=1>One</option>
  <option value=2>Two</option>
</select>
<input type="submit" name="submit" value="Save Changes" />
</form>