Welcome to WebmasterWorld Guest from 54.144.124.152

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Newbie looking for pretty basic help

     

tHatDudeUK

2:58 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



I have the following HTML form. The idea is if the viewer selects "Yes" in each option and then clicks submit they will be allowed to move onto the next section. If they leave the option as "No" they will be asked to answer "Yes" or leave the page. I have tried writing some PHP for index2.php but what I have is very poor (and doesn't work at all) as I'm not very conversant with PHP.

<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.

mincklerstraat

3:25 pm on Nov 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your input will be available in the array $_POST, more specifically as $_POST['withdraw'] and $_POST['noanswer']. The php manual's tutorial has a good section on this: [be2.php.net...] . The tutorial doesn't go into if - but the page on 'if' is pretty good even for beginners: [be2.php.net...]

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.

tHatDudeUK

3:38 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



Many thanks, I'm sure that'll give me enough to go on :)

Paul in South Africa

3:41 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



A very quick and dirty solution that should point you in the right direction.
<?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");
}
}
?>

This part of the code should go right at the top of your page before anything is sent to the browser. What is should do is check to see if the form has been submitted. If it has not it will do nothing and carry on loading the rest of the page. If the form has been submitted it will check to see whether either of the responses were anything other than Yes. If both answers were Yes it will go to index2.php where you can then carry on with your survey. If one of the answers was not Yes you will then have a message which you can use later on in the page.

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>

If they have already submitted the form, but have selected No as one of the answers the message indication they need to select Yes will appear. If they have not yet submitted the form no message will appear.

<added>More elegant solutions appear while I am trying to talk to a collegue on the phone and post at the same time</added>

tHatDudeUK

6:56 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



Many thanks Paul. However, the $PHP_SRELF doesn't appear to work. It tries to go to a URL with $PHP_SELF at the end

tHatDudeUK

7:00 pm on Nov 15, 2004 (gmt 0)

10+ Year Member



Ignore last message, solution = use

<form action = <?=$_SERVER['PHP_SELF']?>

 

Featured Threads

Hot Threads This Week

Hot Threads This Month