Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

drop down menu selection return to the first item after submission

Community Beginner ,
May 02, 2009 May 02, 2009

i've created a search using drop down menu.

Menu items are coming from a data source (record set) but after clicking submit button the section goes to the first item in the drop down menu. how can i keep the selection to the selected item?

Here is the code, i'm not aware where to add the eco selected,

      <option value="<?php echo $row_RSspeciality['id']?>"<?php if($HTTP_POST_VARS['id'] == $row_RSspeciality['id']){ echo "SELECTED"; }?>><?php echo $row_RSspeciality['Speciality']?></option>

I have another question, how can i add an (All) item to the drop down menu, which enable the user to search in all the dept.?

TOPICS
Server side applications
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 08, 2009 May 08, 2009

You need to match the values. If $_POST['id'] doesn't exist, you need to use whatever value is being sent by the form. Judging from your code, it's Specialty (with a capital S).

<option value="<?php echo $row_RSspeciality['id']?>"<?php
if($_POST['Specialty'] == $row_RSspeciality['Specialty'] ) {echo "SELECTED";}
?>><?php echo
$row_RSspeciality['Speciality']?></option>

Translate
LEGEND ,
May 02, 2009 May 02, 2009

You are using $HTTP_POST_VARS, which is obsolete and won't work on modern servers. In place of $HTTP_POST_VARS and $HTTP_GET_VARS, you should use $_POST and $_GET respectively.

If your search form uses the GET method, your code should look like this:

<option value="<?php echo $row_RSspeciality['id']?>"

<?php
if($_GET['id'] == $row_RSspeciality['id']){

  echo "SELECTED";
}?>><?php echo $row_RSspeciality['Speciality']?></option>

how can i add an (All) item to the drop down menu, which enable the user to search in all the dept.?

See the second half of the answer I gave here. Basically, you need to split the SQL query in two. If the option is set to all, run the query without the WHERE clause. If the option is set to anything else, add the WHERE clause to the query using .= (the combined concatenation operator).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2009 May 08, 2009

It didn't work.

the drop down menu returns back to the first item in the list not to the selected one!!

let me write the code from the begining of the select tag.

<select name="Speciality" id="Speciality">
  <?php
do { 
?>
  <option value="<?php echo $row_RSspeciality['id']?>"<?php if($_POST['id'] == $row_RSspeciality['id'] ) {echo "SELECTED";} ?>><?php echo $row_RSspeciality['Speciality']?></option>
  <?php
} while ($row_RSspeciality = mysql_fetch_assoc($RSspeciality));
  $rows = mysql_num_rows($RSspeciality);
  if($rows > 0) {
      mysql_data_seek($RSspeciality, 0);
   $row_RSspeciality = mysql_fetch_assoc($RSspeciality);
  }
?>
</select>

Please help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2009 May 08, 2009

Does $_POST['id'] exist? It will exist only if you have submitted the value from a form through the POST method.

Maybe you should be using $_GET['id'].

In either case, use echo to see what its value is:

if (isset($_POST['id'])) {

  echo '$_POST[\'id\'] is ' . $_POST['id'] . '<br />';

} else {

  echo '$_POST[\'id\'] does not exist<br />';

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 08, 2009 May 08, 2009

I'm using the Post Method because the values are going out from the form to be resulted in a repeated reagion.

I tried the

if (isset($_POST['id'])) {
  echo '$_POST[\'id\'] is ' . $_POST['id'] . '<br />';
} else {
  echo '$_POST[\'id\'] does not exist<br />';
}
  and it gaves me (does not exist)

I think i mixed puting the id and speciality.

I have the table with 2 fileds,

id (primary key)

speciality (contain the name)

the value of the drop down menu is (id) but the label is (speciality)

I don't know where each one should go in the code

    <select name="Speciality" id="Speciality">
  <?php
do { 
?>
  <option value="<?php echo $row_RSspeciality['id']?>"<?php if($_POST['id'] == $row_RSspeciality['id'] ) {echo "SELECTED";} ?>><?php echo $row_RSspeciality['Speciality']?></option>
  <?php
} while ($row_RSspeciality = mysql_fetch_assoc($RSspeciality));
  $rows = mysql_num_rows($RSspeciality);
  if($rows > 0) {
      mysql_data_seek($RSspeciality, 0);
   $row_RSspeciality = mysql_fetch_assoc($RSspeciality);
  }
?>
</select>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 08, 2009 May 08, 2009
LATEST

You need to match the values. If $_POST['id'] doesn't exist, you need to use whatever value is being sent by the form. Judging from your code, it's Specialty (with a capital S).

<option value="<?php echo $row_RSspeciality['id']?>"<?php
if($_POST['Specialty'] == $row_RSspeciality['Specialty'] ) {echo "SELECTED";}
?>><?php echo
$row_RSspeciality['Speciality']?></option>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines