Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please Help I'm a newcomer !

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
IE
Hi,

Is it possible to keep a selected value in a drop down when

selected. For example I have a three step registration

process, with lots of drop downs from mysql tables. What I

want to do is keep the values that were selected in the drop

downs even if the user navigates back to change them.

How to you assign sessions to arrays. I can do it with

text boxes but cannot for the life of me figure it out with

drop down arrays.

Please someone help
 
You dont need to assign the session to arrays, as you can simply echo out the session variable..

eg:

echo "<option value=\"{$_POST['value']}\" selected=\"selected\">Selected: {$_POST['value']}</option>";

ps. not tested, so it might not work.

Olav Alexander Mjelde
Admin & Webmaster
 
Could you please show me how to do this with my code

much appreciated

------------------------------------------------------------------------------------------------------------------------

echo'<tr>
<td colspan="2" class="labelcell"><label for="junior_subject_id">Select Subject:</label></td>';

echo'<td colspan="2" class="fieldcell2"><select name="junior_subject_id">';

$query_result = mysql_query ('SELECT * FROM junior_subjects ORDER BY junior_subject_id');

while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))

{
echo "<option value=\"$row[0]\">$row[1]</option>\n";
}

echo'</select></td>';

echo'</tr>';

------------------------------------------------------------------------------------------------------------------------
 
Code:
 {
      $selected = "";
      if ($_SESSION[$row[0] == row[0]) {
        $selected = 'selected="selected"';
      }

      echo "<option value=\"$row[0]\" {$selected}>$row[1]</option>\n";
 }

you need to start the session on the very top of all pages which are to access the session variables:
session_start();

do this before *any*thing else, as a simple space (" ") can be enough to invoke an error.

You also need to set the session variable to something, when it's submitted.

Why do you use the field-name as row[0]?

Olav Alexander Mjelde
Admin & Webmaster
 

Ok I've changed row[0] to reflect my table but still the code won't work, it just keeps going back to select item
and the selected item does not remain present.

{
$selected = "";
if ($_SESSION[$row['junior_subject_id'] == row['junior_subject_id'])
{
$selected = 'selected="selected"';
}

echo "<option value=\"$row['junior_subject_id']\"
{$selected}>$row[1]</option>\n";
}


Why I am I finding this so difficult !
 
I included the $ but it still doesn't work, it now just gives me the third option in the dropdown all the time , could you have another look at the updated code please

----------------------------------------------------------------------------------------------------------------------

echo'<table align="center">';

echo'<tr>
<td colspan="2" class="labelcell"><label for="secondary_exam_id">Select Exam:</label></td>';

echo'<td colspan="2" class="fieldcell2">
<select name="secondary_exam_id">';

$query_result = mysql_query ('SELECT * FROM secondary_exam_type ORDER BY secondary_exam_id');

while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))

{
$selected = "";
if ($_SESSION[$row['secondary_exam_id']] == $row['secondary_exam_id'])
{
$selected = 'selected="selected"';
}

echo "<option value=\"{$row['secondary_exam_id']}\"
{$selected}>$row[1]</option>\n";
}


----------------------------------------------------------------------------------------------------------------------
 
ah, wait.. I see one thing here.

I'm sorry that I have done something very faulty here :p

Code:
if ($_SESSION['secondary_exam_id'] == $row['secondary_exam_id'])

ps. you have to set the session somewhere too!
Also, where is your <form action="... ?

the mistake I typed, was terrible.. I was very stressed and tired today, so my brain was not o.k.

Olav Alexander Mjelde
Admin & Webmaster
 
Code:
session_start();

   echo'<table align="center">';

      echo'<tr>
           <td colspan="2" class="labelcell"><label for="secondary_exam_id">Select Exam:</label></td>';        
        
      echo'<td colspan="2" class="fieldcell2">
           <form action="page_2.php" method="post">
           <select name="secondary_exam_id">';
 
                    $query_result = mysql_query ('SELECT * FROM secondary_exam_type ORDER BY secondary_exam_id');

while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))
   
  {
       $selected = "";
      if ($_SESSION[$row['secondary_exam_id']] == $row['secondary_exam_id'])
      {
        $selected = 'selected="selected"';
      }

      echo "<option value=\"{$row['secondary_exam_id']}\"    
      {$selected}>$row[1]</option>\n";
  }

page_2.php
Code:
session_start();

?>
<form action="page_3.php" method="post">
<?php

if (isset($_POST['secondary_exam_id'])) {
$_SESSION['secondary_exam_id'] = $_POST['secondary_exam_id'];
}
?>
<input type="submit" value="submit" name="submit" />
</form>

page_3.php
Code:
session_start();
echo $_SESSION['secondary_exam_id'];

I think this should work..

Olav Alexander Mjelde
Admin & Webmaster
 
sorry, looks like I managed to do it again :p
if ($_SESSION['secondary_exam_id'] == $row['secondary_exam_id'])

*hit my self in the head with a sledgehammer*

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top