homepage Welcome to WebmasterWorld Guest from 54.227.146.68
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Newbie looking for pretty basic help
tHatDudeUK




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

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




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

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




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

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

Paul in South Africa




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

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




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

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




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

Ignore last message, solution = use

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved