Page 1 of 1

how to remain value of drop down list

Posted: Mon Sep 15, 2008 6:44 am
by mit
<select name="category" class="forFormFields" ">
<option value="Automobile">Automobile</option>
<option value="Blogs">Blogs</option>
<option value="Business">Business</option>
</select>
there is a sign up form , that user must enter name, family name, category (above) , if user enter a wrong value , it goes to an other page , but when back to sign up form page values are still there with using sessions , except category which i cant show the selected value, and by default is in automobile , i know there is a java script that can help me to solve this problem ... can any one help me?

Re: how to remain value of drop down list

Posted: Mon Sep 15, 2008 7:09 am
by marcth

Code: Select all

 
<?php
if(!isset($_REQUEST['category'] && isset($_SESSION['category'])) {
  $_REQUEST['category'] = $_SESSION['category'];
}
?>
 
<select name="category" class="forFormFields" ">
  <option value="Automobile"<?= ($_REQUEST['category'] == 'Automobile') ? ' selected="selected" : ''; ?>>Automobile</option>
  <option value="Blogs"<?= ($_REQUEST['category'] == 'Blogs') ? ' selected="selected" : ''; ?>>Blogs</option>
  <option value="Business"<?= ($_REQUEST['category'] == 'Business') ? ' selected="selected" : ''; ?>>Business</option>
</select>    
 

Re: how to remain value of drop down list

Posted: Mon Sep 15, 2008 7:33 am
by pcoder
Try this, may be it will be helpful for you.

Code: Select all

 
<?
$cat = array('0'=>'category1'
                ,'1'=>'category2');
echo "<form method=\"post\">";
echo "<select name=\"category\">";
foreach($cat as $key=>$value){
    $selected = trim($_POST['category'])==$key?'selected=true':NULL;
    echo "<option value='".$key."'".$selected."'>".$value."</option>";
}
echo "</select>";
echo "<input type=\"submit\" value=\"submit\">";
echo "</form>";
?>
 

Re: how to remain value of drop down list

Posted: Mon Sep 15, 2008 10:47 pm
by mit
thx alot, this one is perfectly works... :lol: