Nirali Joshi
Nirali Joshi

Reputation: 2008

select box's value after submit

I have simple code File name:newEmptyPHP.php

<form method="post" action="newEmptyPHP.php">Title:
    <select name="title">
        <option value="select">Select</option>;
        <option value="Dr" selected=<?php if(isset($_POST[ 'title'])=="Dr" ){ echo "selected"; } ?>>Dr</option>';
        <option value="Prof">Prof</option>';
        <option value="Mr">Mr</option>';
        <option value="Ms">Ms</option>';
        <option value="Miss">Miss</option>';
        <option value="Mrs">Mrs</option>';</select>
    <input type="submit" value="submit">
</form>

I want to keep selected particular value which was selected before the form submission. But i did not get the desirable output. any help appreciated.

Upvotes: 1

Views: 1100

Answers (7)

alwaysLearn
alwaysLearn

Reputation: 6950

I would rather prefer this code for the purpose

$option_array = array('select'=>'select','mr'=>'mr'... other values);

<?php
  $option_string = '';
 foreach($option_array as $key=>$value)
 {
   if($key == $_POST['title'])
   {
     $selected = 'selected';
   }
   else
   {
    $selected = '';
   }
   $option_string .= "<option value='$key' $selected>$value</option>";
}
?>
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<?php echo $option_string; ?>    
</select>
<input type="submit" value="submit">

Upvotes: 0

Arvind
Arvind

Reputation: 938

I thin you have static option values.If so the you have to check for every option like as below for one.

<form method="post" action="newEmptyPHP.php">
    Title:<select name="title" > 
                                <option value="select" >Select</option>; 
<?php 
     $selected = ""; 
     if(isset($_POST['title']) &&  $_POST['title'] == "Dr")
     { 
       $selected = 'selected="selected"'; 
     } 
 ?>
 <option value="Dr" <?php echo $selected; ?>>Dr</option> 
 <option value="Prof">Prof</option> 
 <option value="Mr">Mr</option>
 <option value="Ms">Ms</option> 
 <option value="Miss">Miss</option> 
 <option value="Mrs">Mrs</option> 
 </select>
 <input type="submit" value="submit">
</form>

Upvotes: 0

Sanjay
Sanjay

Reputation: 1745

There is a mistake in your code. isset(Something) will return either true or false and will not return Dr you are looking for.

Retry with this

<form method="post" action="newEmptyPHP.php">
    Title:<select name="title" > 
              <option value="" >Select</option> 
              <option value="Dr" selected=<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected";  } ?> >Dr</option>'; 
              <option value="Prof" <?php if(isset($_POST['title']) && $_POST['title'] == "Prof"){ echo "selected";  } ?>>Prof</option>'; 
              <option value="Mr" <?php if(isset($_POST['title']) && $_POST['title'] == "Mr"){ echo "selected";  } ?>>Mr</option>'; 
              <option value="Ms" <?php if(isset($_POST['title']) && $_POST['title'] == "Ms"){ echo "selected";  } ?>>Ms</option>'; 
              <option value="Miss" <?php if(isset($_POST['title']) && $_POST['title'] == "Miss"){ echo "selected";  } ?>>Miss</option>'; 
              <option value="Mrs" <?php if(isset($_POST['title']) && $_POST['title'] == "Mrs"){ echo "selected";  } ?>>Mrs</option>'; 
         </select>
<input type="submit" value="submit">

Upvotes: 0

Prasanth Bendra
Prasanth Bendra

Reputation: 32830

Change

<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr"){ echo "selected";  } ?> >Dr</option>';

To :

<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo 'selected="selected"';  } ?> >Dr</option>';

1.Remove isset where you are checking it for value, isset will return either true or false
2.Make selected="selected inside if condition

Upvotes: 0

Jignesh Mesvaniya
Jignesh Mesvaniya

Reputation: 31

<?php 
if($_POST['title'] == "Dr")
{ ?>
<option value="Dr" selected>Dr</option>
<?php
}
else
{
?>
<?php
<option value="Dr">Dr</option>
<?php
}
?>

Upvotes: 0

Narek
Narek

Reputation: 3823

<?php if(isset($_POST['title']) == "Dr"){ echo "selected";  } ?>

Change to

<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected";  } ?>

But better to use JS / jQuery. Example for jQuery:

<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"):?>
    <script type="text/javascript">
    $(function(){
        $('select[name=title]').val('<?php echo html_special_chars($_POST[''title'])?>');
    });
    </script>
<?php endif?>

Upvotes: 0

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

change this

<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr")
{ echo "selected";  } ?> >Dr</option>'; 

to

<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr")
{ echo "selected='selected'";  } ?> >Dr</option>'; 

Upvotes: 2

Related Questions