Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php form issue 1

Status
Not open for further replies.

jordanking

Programmer
Sep 8, 2005
351
hello,

I have a form that captures user input to insert into a mysql database. I am using php 5 and mysql 5. The form consists of text fields and list menus. I also have a function to capture and validate user input before the insert record function is called. If there are errors the insert function is ignored. The code for handling the user input is also within the same php page. I have inserted code to capture the post values so that if there are errors in the user input the user fields do not get reset to blank on page update. The text fields work great. I am having problems with the list menue values.

I have a block of code that I have inserted in order to capture the $_POST['listmenu'] value and insert a "selected="selected" value within the option tag so that when the form is refreshed after errors are detected the list menu retains the selected value. Shown below

Code:
<select name="city" id="city">
           	      <option value="6"<?php if (isset($_POST['city']) && $_POST['city']==6){echo ' selected="selected"' ;} ?>>Beaumont</option>
           	      <option value="9"<?php if (isset($_POST['city']) && $_POST['city']==9){echo ' selected="selected"' ;} ?>>Devon</option>
           	      <option value="1"<?php if(empty($_POST)){ echo ' selected="selected"' ;}elseif (isset($_POST['city']) && $_POST['city']==1){echo ' selected="selected"' ;} ?>>Edmonton</option>
           	      <option value="8"<?php if (isset($_POST['city']) && $_POST['city']==8){echo ' selected="selected"' ;} ?>>Ft. Saskatchewan</option>
           	      <option value="7"<?php if (isset($_POST['city']) && $_POST['city']==7){echo ' selected="selected"' ;} ?>>Leduc</option>
           	      <option value="2"<?php if (isset($_POST['city']) && $_POST['city']==2){echo ' selected="selected"' ;} ?>>Sherwood Park</option>
           	      <option value="5"<?php if (isset($_POST['city']) && $_POST['city']==5){echo ' selected="selected"' ;} ?>>Spruce Grove</option>
           	      <option value="3"<?php if (isset($_POST['city']) && $_POST['city']==3){echo ' selected="selected"' ;} ?>>St. Albert</option>
           	      <option value="4"<?php if (isset($_POST['city']) && $_POST['city']==4){echo ' selected="selected"' ;} ?>>Stony Plain</option>
       	        </select>
this returns the following html:

Code:
           	    <select name="city" id="city">
 				  <option value=""></option>
           	      <option value="6">Beaumont</option>
           	      <option value="9">Devon</option>
           	      <option value="1" selected="selected">Edmonton</option>
           	      <option value="8">Ft. Saskatchewan</option>
           	      <option value="7">Leduc</option>
           	      <option value="2">Sherwood Park</option>
           	      <option value="5">Spruce Grove</option>
           	      <option value="3">St. Albert</option>
           	      <option value="4">Stony Plain</option>
       	        </select>

the problem is that I am trying to run a conditional statement within anoth list menu's code that inclues an or operator "||". And for some reason it is always evaluating to true. The following is the php code followed by the html output

Code:
           	    <select name="service_id" id="service_id">
				  <option value=""></option>
           	      <option value="1"<?php if(empty($_POST)){ echo ' selected="selected"' ;}elseif (isset($_POST['service_id']) && $_POST['service_id']==1||2||3){echo ' selected="selected"' ;} ?>>Up</option>
           	      <option value="2"<?php if (isset($_POST['service_id']) && $_POST['service_id']==8||9||10){echo ' selected="selected"' ;} ?>>Down</option>
           	      <option value="3"<?php if (isset($_POST['service_id']) && $_POST['service_id']==15||16||17){echo ' selected="selected"' ;} ?>>Fix</option>
           	      <option value="4"<?php if (isset($_POST['service_id']) && $_POST['service_id']==43){echo ' selected="selected"' ;} ?>>Cnd Up</option>
           	      <option value="5"<?php if (isset($_POST['service_id']) && $_POST['service_id']==44){echo ' selected="selected"' ;} ?>>Cnd Down</option>
       	        </select>

Code:
           	    <select name="service_id" id="service_id">
				  <option value=""></option>
           	      <option value="1" selected="selected">Up</option>
           	      <option value="2" selected="selected">Down</option>
           	      <option value="3" selected="selected">Fix</option>
           	      <option value="4">Cnd Up</option>
           	      <option value="5">Cnd Down</option>
       	        </select>

all of the above option line conditional statments that include || are evaluating to true. However i have confirmed that the $_POST['service_id'] is correct and is only a 1 or a 2 etc... If I take out the || operator it works.

Can anyone tell my why these are evaluating to true.

Thanks in advance

JK
 
One point of information on HTML....

When preselecting an option of a select, you should just have present the keyword "selected":

<option value="1" selected>Edmonton</option>




As for your conditional problem, I don't think:

$_POST['service_id']==1||2||3

is what you want to do at all. It will, indeed always evaluate to true.

The expression "1||2||3" will be evaluated first. This expression always evaluates to the boolean TRUE.

Then when performing the comparison

$_POST['service_id'] == TRUE

PHP will have to compare apples to apples, so it will convert $_POST['service_id'] to a boolean, so anything not empty will evaluate to TRUE. So this expression always evaluates to TRUE. The entire condition:

if (isset($_POST['service_id']) && $_POST['service_id']==1||2||3)

is then logically equivalent to:

if (isset($_POST['service_id']))

because "if (X && TRUE)" is the same as saying "if(X)",

If you meant to say "$_POST['service_id'] equal to 1 or $_POST['service_id'] equal to 2 or $_POST['service_id'] equal to 3", then the expression should be:

$_POST['service_id'] == 1 || $_POST['service_id'] == 2 $_POST['service_id'] == 3



Want the best answers? Ask the best questions! TANSTAAFL!
 
thanks for the reply!

it all works, just for reference, here is the finished product

Code:
           	    <select name="city" id="city">
 				  <option value=""></option>
           	      <option value="6"<?php if (isset($_POST['city']) && $_POST['city']==6){echo ' selected="selected"' ;} ?>>Beaumont</option>
           	      <option value="9"<?php if (isset($_POST['city']) && $_POST['city']==9){echo ' selected="selected"' ;} ?>>Devon</option>
           	      <option value="1"<?php if (empty($_POST)){ echo ' selected="selected"' ;}elseif (isset($_POST['city']) && $_POST['city']==1){echo ' selected="selected"' ;} ?>>Edmonton</option>
           	      <option value="8"<?php if (isset($_POST['city']) && $_POST['city']==8){echo ' selected="selected"' ;} ?>>Ft. Saskatchewan</option>
           	      <option value="7"<?php if (isset($_POST['city']) && $_POST['city']==7){echo ' selected="selected"' ;} ?>>Leduc</option>
           	      <option value="2"<?php if (isset($_POST['city']) && $_POST['city']==2){echo ' selected="selected"' ;} ?>>Sherwood Park</option>
           	      <option value="5"<?php if (isset($_POST['city']) && $_POST['city']==5){echo ' selected="selected"' ;} ?>>Spruce Grove</option>
           	      <option value="3"<?php if (isset($_POST['city']) && $_POST['city']==3){echo ' selected="selected"' ;} ?>>St. Albert</option>
           	      <option value="4"<?php if (isset($_POST['city']) && $_POST['city']==4){echo ' selected="selected"' ;} ?>>Stony Plain</option>
       	        </select>
 
my appologies,

here is the actual code block

Code:
           	    <select name="service_id" id="service_id">
				  <option value=""></option>
           	      <option value="1"<?php if (empty($_POST)){ echo ' selected="selected"' ;}elseif (isset($_POST['service_id']) && $_POST['service_id'] == 1 || $_POST['service_id'] == 2 || $_POST['service_id'] == 3){echo ' selected="selected"' ;} ?>>Up</option>
           	      <option value="2"<?php if (empty($_POST)){}elseif (isset($_POST['service_id']) && $_POST['service_id'] == 8 || $_POST['service_id'] == 9 || $_POST['service_id'] == 10){echo ' selected="selected"' ;} ?>>Down</option>
           	      <option value="3"<?php if (empty($_POST)){}elseif (isset($_POST['service_id']) && $_POST['service_id'] == 15 || $_POST['service_id'] == 16 || $_POST['service_id'] == 17){echo ' selected="selected"' ;} ?>>Fix</option>
           	      <option value="4"<?php if (empty($_POST)){}elseif (isset($_POST['service_id']) && $_POST['service_id']==43){echo ' selected="selected"' ;} ?>>Cnd Up</option>
           	      <option value="5"<?php if (empty($_POST)){}elseif (isset($_POST['service_id']) && $_POST['service_id']==44){echo ' selected="selected"' ;} ?>>Cnd Down</option>
       	        </select>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top