Forum Moderators: coopster
<form name="form1" method="post" action="index2.php">
<p>Do you understand that you can withdraw from the survey at any stage?
<select name="withdraw" size="1" id="withdraw">
<option>Yes</option>
<option selected>No</option>
</select>
</p>
<p>Do you understand that you are free to choose not to answer a question without giving a reason why?
<select name="noanswer" size="1" id="noanswer">
<option>Yes</option>
<option selected>No</option>
</select>
</p>
<p>
<input type="submit" name="Submit" value="Do survey">
</p>
</form>
I guess that when it is submitted to index2.php the values are interpreted as $withdraw and $noanswer and these strings will either be assigned the variable "Yes" or "No", all I need is some sort of IF and ELSE statement i'm guessing....
Any help at all would be much appreciated. :-)
Many thanks in advance.
you'll want something like
if(!isset($_POST['withdraw']) OR!isset($_POST['noanswer'])) {
echo 'problem - you probably arrived at this page directly - please go to <a href="survey.php">this page first</a>';
die(); /* above checks to see if the variables are actually 'set' */
} elseif($_POST['withdraw'] == 'Yes' AND $_POST['noanswer'] == 'Yes){
echo 'ok, let\'s take the survey ...';
} elseif(/* some other conditions here */){
/* do something else */
}
else{
/* or else do something else */
}
Stuff between /* and */ is a comment and PHP just ignores it - note that elseif also has a condition after it.
<?php
$message1 = "";
$message2 = "";
if (isset($Submit) && $Submit == "Do survey")
{
if ($_POST[withdraw]!= "Yes")
{
$message1 = "You must select Yes in order to continue";
}
if ($_POST[noanswer]!= "Yes")
{
$message2 = "You must select Yes in order to continue";
}
if ($message1 == "" && $message2 == "")
{
header("Location: index2.php");
}
}
?>
Now you can start with the rest of your page.
Most of the rest of your code can stay the same with a few minor changes.
<form name="form1" method="post" action="[b]$PHP_SELF[/b]">
<p>Do you understand that you can withdraw from the survey at any stage?
<select name="withdraw" size="1" id="withdraw">
<option>Yes</option>
<option selected>No</option>
</select>
</p>
[b]<?php
if ($message1!= "")
{
print "<p>$message1</p>";
}
?>[/b]
<p>Do you understand that you are free to choose not to answer a question without giving a reason why?
<select name="noanswer" size="1" id="noanswer">
<option>Yes</option>
<option selected>No</option>
</select>
</p>
[b]<?php
if ($message2!= "")
{
print "<p>$message2</p>";
}
?>[/b]
<p>
<input type="submit" name="Submit" value="Do survey">
</p>
</form>
<added>More elegant solutions appear while I am trying to talk to a collegue on the phone and post at the same time</added>