<select> <option> using php - Help needed

A

Anonymous

Guest
hello,

I am trying to create a drop down list where users can select their title Mr, Mrs etc and I have the code below:
Code:
<select name="title">
	<?php
		$titlevalue=array(0,'Mr','Mrs','Miss','Ms');
		$titleoption=array('Please Select','Mr','Mrs','Miss','Ms');
		for($i=0;$i<=4;$i++){
			echo "<option value=\"".$titlevalue[$i]."\"";
			if($titlevalue[$i]==$_POST['title']){
				echo " selected";
			}
			echo ">".$titleoption[$i]."</option>";
		}
	?>
</select>

This is a form that will POST back to itself so at the top of the page I have included the next bit of code to prevent the variable $_POST['title'] from being undeclared on first loading the page:

Code:
if(!isset($_POST['submit'])){
	$_POST['title']=0;
}

However, when I load the page, I find the default value of the options set to index 4 of my $titleoption array "Ms" and no matter how much I stare at my code, I can't figure out why it would do this.

Can anyone help?
 
I checked your code and i found the issue, you need to put the value 0 within single quotes like this '0'.
change your below code

if(!isset($_POST['submit'])){
$_POST['title']=0;
}

to as below

if(!isset($_POST['submit'])){
$_POST['title']='0';
}

and change your below code

$titlevalue=array(0,'Mr','Mrs','Miss','Ms');

to as below

$titlevalue=array('0','Mr','Mrs','Miss','Ms');

The reason was that if you are using 0 without quotes it system will consider as false value and in your case your condition "if($titlevalue[$i]==$_POST['title'])" was not matching and each time it was false and hence the control was going inside the condition and making the option value selected.

0 and 1 without quotes are considered as false and true.

In your case your condition is each time comparing each value with false and as each it is false the condition is satisfied and you were not getting your required result.

hope this will help you.

Thanks & Regards,
Prensil Technologies Pvt. Ltd.
php web development company
http://www.prensil.com
 
Not sure if this is possible, I never tried to assign a value to a post item which might not exist yet. Your logic is wrong $_POST['title'] will only exist when $_POST['submit'] exists, $_POST is used to get content from forms, I don't know if you can assign a value to it just like a normal variable but I would strongly not recommend it.

you could do something like this:

Code:
<select name="title">
   <?php
      $titlevalue=array(0,'Mr','Mrs','Miss','Ms');
      $titleoption=array('Please Select','Mr','Mrs','Miss','Ms');
      for($i=0;$i<=4;$i++){
         echo "<option value=\"".$titlevalue[$i]."\"";
         $title = $_POST['title']; //always a good thing to escape first!
         if($titlevalue[$i]==$title){
            echo " selected";
         }
         echo ">".$titleoption[$i]."</option>";
      }
   ?>
</select>


Code:
if(!isset($_POST['submit'])){
   $title = 0;
}

and normally numeric values do not require quotes.
 
JohnPringle83 said:
hello,

I am trying to create a drop down list where users can select their title Mr, Mrs etc and I have the code below:
Code:
<select name="title">
	<?php
		$titlevalue=array(0,'Mr','Mrs','Miss','Ms');
		$titleoption=array('Please Select','Mr','Mrs','Miss','Ms');
		for($i=0;$i<=4;$i++){
			echo "<option value=\"".$titlevalue[$i]."\"";
			if($titlevalue[$i]==$_POST['title']){
				echo " selected";
			}
			echo ">".$titleoption[$i]."</option>";
		}
	?>
</select>

This is a form that will POST back to itself so at the top of the page I have included the next bit of code to prevent the variable $_POST['title'] from being undeclared on first loading the page:

Code:
if(!isset($_POST['submit'])){
	$_POST['title']=0;
}

However, when I load the page, I find the default value of the options set to index 4 of my $titleoption array "Ms" and no matter how much I stare at my code, I can't figure out why it would do this.

Can anyone help?
use this link

http://www.techpdf.in/
 
Back
Top