Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML HTML Basics

<select> <option> using php - Help needed

Discussing Html . Code , Software , other

Moderators: macek, egami, gesf

<select> <option> using php - Help needed

Postby JohnPringle83 » Tue May 03, 2011 12:47 pm

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 all
<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: Select all
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?
JohnPringle83
New php-forum User
New php-forum User
 
Posts: 8
Joined: Tue May 03, 2011 5:56 am

Re: <select> <option> using php - Help needed

Postby Prensil » Sat May 14, 2011 1:01 am

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
Prensil
New php-forum User
New php-forum User
 
Posts: 81
Joined: Sat May 07, 2011 12:05 am

Re: <select> <option> using php - Help needed

Postby cmstutorial » Mon Jun 27, 2011 9:09 am

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 all
<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: Select all
if(!isset($_POST['submit'])){
   $title = 0;
}


and normally numeric values do not require quotes.
cmstutorial
New php-forum User
New php-forum User
 
Posts: 5
Joined: Sun Jun 26, 2011 9:06 am

Re: <select> <option> using php - Help needed

Postby azhagu » Tue Jun 28, 2011 3:34 am

JohnPringle83 wrote: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 all
<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: Select all
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/
azhagu
New php-forum User
New php-forum User
 
Posts: 13
Joined: Mon Jun 27, 2011 4:17 am


Return to HTML Basics

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.