Hi All,
I am having trouble with my IF else statements...
It allways prints "Didn't select..." even when I select an option can anyone see a problem with the code I posted?
I have a HTML page that contains (among other things)
<form action="process.php" method="post">
<select name=list>
<option selected>Please Select</option>
<option value="option1">This is Option 1</option>
<option value="option2">This is Option 2</option>
<option value="option3">This is Option 3</option>
<option value="option4">This is Option 4</option>
</select>
<input name="submit" type="submit" value="Calculate">
</form>
Then I have a process.php page which contains this
<?PHP
$option1 = $_POST['option1'];
$option2 = $_POST['option2'];
$option3 = $_POST['option3'];
$option4 = $_POST['option4'];
$choice = $_POST['list'];
if ($choice == $option1)
{
print "<h1>option 1</h1>";
}
elseif ($choice == $option2)
{
print "<h1>option 2</h1>";
}
elseif ($choice == $option3)
{
print "<h1>option 3</h1>";
}
elseif ($choice == $option4)
{
print "<h1>option 4</h1>";
}
else
{
print "<h1>Didn't select...</h1>";
}
Thanks,
MikeI believe this is because 'option1' etc aren't sent with the form, only the result of the select is sent, stored in 'list'.. Quote the name="list" btw.
So if you want to compare with dynamic values, you can't do it like that.. I guess you'd have to add those values in some extra hidden fields... If you don't require that, just hardcode it.. if ($choice == "option1") etc...Is your register globals set to off?
I had a problem passing variables from page to page a while ago and i needed to turn register globals off to sort it out.
It looks like there is no value coming into the form at all scince you have that last else statement try putting in a statement that says if the value sent is empty then type 'null' etc
Hope this helps
Bonkom imp:rydberg got the problem spot on:
you actually pass the variable "list" through post, which has the values of "option1", "option2" and so on.
your PHP should be:
if ($_POST['list'] == "Option1")
{
do stuff
}
else if ($_POST['list'] == "Option1")
{
do other stuff
}
and so on....
BTW - You really should put ALL attributes in some kind of quote - you <select> tag is technically invalid.
I am having trouble with my IF else statements...
It allways prints "Didn't select..." even when I select an option can anyone see a problem with the code I posted?
I have a HTML page that contains (among other things)
<form action="process.php" method="post">
<select name=list>
<option selected>Please Select</option>
<option value="option1">This is Option 1</option>
<option value="option2">This is Option 2</option>
<option value="option3">This is Option 3</option>
<option value="option4">This is Option 4</option>
</select>
<input name="submit" type="submit" value="Calculate">
</form>
Then I have a process.php page which contains this
<?PHP
$option1 = $_POST['option1'];
$option2 = $_POST['option2'];
$option3 = $_POST['option3'];
$option4 = $_POST['option4'];
$choice = $_POST['list'];
if ($choice == $option1)
{
print "<h1>option 1</h1>";
}
elseif ($choice == $option2)
{
print "<h1>option 2</h1>";
}
elseif ($choice == $option3)
{
print "<h1>option 3</h1>";
}
elseif ($choice == $option4)
{
print "<h1>option 4</h1>";
}
else
{
print "<h1>Didn't select...</h1>";
}
Thanks,
MikeI believe this is because 'option1' etc aren't sent with the form, only the result of the select is sent, stored in 'list'.. Quote the name="list" btw.
So if you want to compare with dynamic values, you can't do it like that.. I guess you'd have to add those values in some extra hidden fields... If you don't require that, just hardcode it.. if ($choice == "option1") etc...Is your register globals set to off?
I had a problem passing variables from page to page a while ago and i needed to turn register globals off to sort it out.
It looks like there is no value coming into the form at all scince you have that last else statement try putting in a statement that says if the value sent is empty then type 'null' etc
Hope this helps
Bonkom imp:rydberg got the problem spot on:
you actually pass the variable "list" through post, which has the values of "option1", "option2" and so on.
your PHP should be:
if ($_POST['list'] == "Option1")
{
do stuff
}
else if ($_POST['list'] == "Option1")
{
do other stuff
}
and so on....
BTW - You really should put ALL attributes in some kind of quote - you <select> tag is technically invalid.