tags:

views:

171

answers:

3

Im trying to implement the search feature in my website.

when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.

what i want is to keep the selected category of the combo by default in the form after posted

For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated

+2  A: 

Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":

When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.

David Dorward
+2  A: 

I assume you get categories from database.

you should try:

<?php

$categories = $rows; //array from database
foreach($rows as $row){
     if($row['name'] == $_POST['category']){
          $isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
     } else {
          $isSelected = ''; // else we remove any tag
     }
     echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Mihai Iorga
A: 

This Solved my Problem. Thanks for all those answered

 <select name="name" id="name">
  <option value="a">a</option>
  <option value="b">b</option>
 </select>

<script type="text/javascript">
  document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
Rajasekar