how to remain value of drop down list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mit
Forum Commoner
Posts: 32
Joined: Mon Sep 15, 2008 6:37 am

how to remain value of drop down list

Post 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?
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: how to remain value of drop down list

Post 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>    
 
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: how to remain value of drop down list

Post 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>";
?>
 
mit
Forum Commoner
Posts: 32
Joined: Mon Sep 15, 2008 6:37 am

Re: how to remain value of drop down list

Post by mit »

thx alot, this one is perfectly works... :lol:
Post Reply