i have two select tags as part of a registration form, city1 city2 where city1 has a list of regions and similar for city2
there are different regions for city1 and city2 so instead of all the regions appearing one after the other i would like to create a blank option followed by the next set of regions for formatting purpose only.
ex=
<select name="city1">
<option <?php if ($city1=="region1"){echo "SELECTED";}?> value="region1">Select region1</option>
<option <?php if ($city1=="nameofregion1"){echo "SELECTED";}?> value="nameofregion1">nameofregion1</option>
<option <?php if ($northisland=="0"){echo "SELECTED";}?> value="0"></option>
<option <?php if ($city1=="nameofregion2"){echo "SELECTED";}?> value="nameofregion2">nameofregion2</option>
<option <?php if ($northisland=="1"){echo "SELECTED";}?> value="1"></option>
</select>
<select name="city2">
<option <?php if ($city2=="region2"){echo "SELECTED";}?> value="region2">Select region2</option>
<option <?php if ($city2=="nameofregion1"){echo "SELECTED";}?> value="nameofregion1">nameofregion1</option>
<option <?php if ($northisland=="2"){echo "SELECTED";}?> value="2"></option>
<option <?php if ($city2=="nameofregion2"){echo "SELECTED";}?> value="nameofregion2">nameofregion2</option>
<option <?php if ($northisland=="3"){echo "SELECTED";}?> value="3"></option>
</select>
from a php validation perspective if a user does not select any of the regions or both the regions i am displaying an error message asking them to either select 1 region from either city1 or city2
as of now there is a blank option being displayed which is working fine, i am having an issue with the php validation.
until i introduced value=0 my rules for validating the select tag were:
1. user cannot leave both the select tags with the default option which is "Select region1" & "Select region2"
2. user cannot select both the regions from city1 & city2 select tags
the code of 2. is
if(!($city1 == "region1") && !($city2 == "region2"))
{
$error.="Please select only 1 Region<br />";
}
now by introducing <option <?php if ($northisland=="0"){echo "SELECTED";}?> value="0"></option> there is a conflict with the above php validation code used in point 2.
1.
is it correct to use 1,2,3 as part of the following <option> tag or should i only use 0 everywhere
<option <?php if ($northisland=="0"){echo "SELECTED";}?> value="0"></option>
<option <?php if ($northisland=="1"){echo "SELECTED";}?> value="1"></option>
2.
how can i get around the conflict that is being created by introducing this value=0 with
if(!($city1 == "region1") && !($city2 == "region2"))
{
$error.="Please select only 1 Region<br />";
}
as i need the above php code and i also need the blank space for formatting purpose
please advice.
thanks.
question about select tag in php
Moderator: General Moderators
-
sudhakararaog
- Forum Newbie
- Posts: 13
- Joined: Thu Jan 31, 2008 1:08 am
Re: question about select tag in php
You have two drop down boxes with different regions in each one. The user can only select a region from ONE of the drop boxes but cannot select a 'blank' area.
Pseudocode:
1 Display both drop boxes with the relevant data including the empty spaces
2 Upon form submission:-
2.1 Get (POST) the values of both drop boxes.
2.2 Ensure only one value is NOT SELECT (default option) OR BLANK
HTML form code:
Code: Select all
<select name='city1'>
<option value='select'>Select...</option>
<option value='region1'>Region 1</option>
<option value='region2'>Region 2</option>
<option value='blank'></option>
<option value='region3'>Region 3</option>
</select>
<br><br>
<select name='city2'>
<option value='select'>Select...</option>
<option value='region1'>Region 1</option>
<option value='region2'>Region 2</option>
<option value='blank'></option>
<option value='region3'>Region 3</option>
</select>Code: Select all
$city1 = $_POST["city1"];
$city2 = $_POST["city2"];
if ( ($city1 != "select") && ($city1 != "blank") )
{
if ( ($city2 != "select") && ($city2 != "blank") )
{
die("You cannot select a region from both cities");
}
else
{
die("You selected a region from city 1");
}
}
elseif ( ($city2 != "select") && ($city2 != "blank") )
{
die("You have selected a region from city 2");
}
else
{
die("You have not selected a region from either city");
}Kind Regards
AMCH