Jump to content

need help with PHP arrangements


CNT
 Share

Recommended Posts

I did a tutorial guide off a ehow website. I tried my best in understanding the english and create them in a PHP page. Could you look it over and correct what may be wrong. This is all one PHP page, no other index page or anything. Thanks. food.php

<?phpif (isset($_POST['submit'])) {$foodquestion = $_POST['foodquestion'];if (!empty($_POST['foodanswer'])) {$foodanswer = $_POST['foodanswer'];echo "<p>You have chosen: <b>" . $foodanswer . "</b> as your favorite food!</p>";} else {$foodanswer = FALSE;echo "<p><font color='red'>Please tell us your favorite food!</font></p>";}if (!isset($_POST['vegieanswer'])) {$vegieanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$vegieanswer = $_POST['vegieanswer'];echo "<p><font color='navy'>You checked: <b>";foreach ($vegieanswer as $v) {echo $v . " | ";}}echo "</b></font></p>";  $icecreamquestion = $_POST['icecreamquestion'];if (!isset($_POST['icecreamanswer'])) {$icecreamanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$icecreamanswer = $_POST['icecreamanswer'];switch ($icecreamanswer) {case 'Yes':echo "<p><font color='navy'>You said that you <b>do</b> like mint chocolate chip ice cream! </font></p>";break;case 'No':echo "<p><font color='navy'>You said that you <b>DO NOT</b> like mint chocolate chip ice cream!</font></p>";break;}}$careerquestion = $_POST['careerquestion'];$careeranswer = $_POST['careeranswer'];if ($careeranswer == 'none') {$careeranswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$careeranswer = $_POST['careeranswer'];echo "<p><font color='navy'>You answered that kids mostly aspire to be a(n) " . $careeranswer . "!</font></p>";} $experiencequestion = $_POST['experiencequestion'];if (!empty($_POST['experienceanswer'])) {$experienceanswer = $_POST['experienceanswer'];echo "<p><font color='navy'>Your response has been recorded!</font></p>";} else {$experienceanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} } ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>PHP Survery</title></head><body>What is your favorite food?<br/><input type="hidden" name="foodquestion" value="What is your favorite food?" /><input type="text" name="foodanswer" size="60" /><br /><br />Which of the following do you like the best?<br/><input type="hidden" name="vegiequestion" value="Which of the following do you like the best?" /><input type="checkbox" name="vegieanswer[]" value="Cabbage" />Cabbage<br /><input type="checkbox" name="vegieanswer[]" value="Brussell Sprouts" />Brussell Sprouts<br /><input type="checkbox" name="vegieanswer[]" value="Green Beans" />Green Beans<br /><input type="checkbox" name="vegieanswer[]" value="Green Peppers" />Green Peppers<br /><br />Do you like mint chocolate chip flavored ice cream?<br/><input type="hidden" name="icecreamquestion" value="Do you like mint chocolate chip flavored ice cream?" /><input type="radio" name="icecreamanswer" value="Yes" />Yes  <input type="radio" name="icecreamanswer" value="No" />No <br /><br />Select the occupation that you think most kids aspire to be:<br/><input type="hidden" name="careerquestion" value="Select the occupation that you think most kids aspire to be:" /><select name="careeranswer"><option value="none" selected="selected">--Select Occupation--</option><option value="Doctor">Doctor</option><option value="Lawyer">Lawyer</option><option value="Fire Fighter">Fire Fighter</option><option value="Police Officer">Police Officer</option><option value="President of the USA">President of the USA</option><option value="Internet Entrepreneur">Internet Entrepreneur</option></select><br /><br />Describe your happiest experience:<br/><input type="hidden" name="experiencequestion" value="Describe your happiest experience:" /><textarea name="experienceanswer" rows="10" cols="50"></textarea><br /><br /><input type="submit" name="submit" value="Submit Form" /></form>if ($foodanswer != FALSE && $vegieanswer != FALSE && $icecreamanswer != FALSE && $careeranswer != FALSE && $experienceanswer != FALSE) {$newV = implode(' | ',$vegieanswer);$to = 'your@email.com'$subject = 'Poll Results'$body = "<html><head></head><body>Hi Your Name!<br />Your poll has just been completed. Here are the results:<br /><br />" . $foodquestion . "<br />Answer: " . $foodanswer . "<br /><br />" . $vegiequestion . "<br />Answer: " . $newV . "<br /><br />" . $icecreamquestion . "<br />Answer: " . $icecreamanswer . "<br /><br />" . $careerquestion . "<br />Answer: " . $careeranswer . "<br /><br />" . $experiencequestion . "<br />Answer: " . $experienceanswer . "<br /></body></html>";$headers = "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1\n" . "From: Your Survey donotreply@yourdomain.com";mail ($to, $subject, $body, $headers);echo "<h2>Your information has been sent to the owner of the survey. Thank you for participating!";} else {echo "<p><font color='red'><b>Your information could not be sent because of one or more errors. Please try again.</b></font></p>";}</body></html>

Link to comment
Share on other sites

First off, you are using a server to test correct? Like WAMP? Or else you won't be able to use PHP. Now to fix the errors. 1) You didn't put php tags around the php code at the bottom like James stated.2) You didn't put semi colons after a couple statements at the end that require one. So that will cause an error after you put php tags around that bunch of code.3) You didn't put a starting form tag, so in turn, you don't have the action or method statement you need for the form to work. I have then tested all these fixes and had it actually send an email to me.Everything worked, so it should work for you.

<?phpif (isset($_POST['submit'])) {$foodquestion = $_POST['foodquestion'];if (!empty($_POST['foodanswer'])) {$foodanswer = $_POST['foodanswer'];echo "<p><font color='navy'>You have chosen: <b>" . $foodanswer . "</b> as your favorite food!</font></p>";} else {$foodanswer = FALSE;echo "<p><font color='red'>Please tell us your favorite food!</font></p>";}if (!isset($_POST['vegieanswer'])) {$vegieanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$vegieanswer = $_POST['vegieanswer'];echo "<p><font color='navy'>You checked: <b>";foreach ($vegieanswer as $v) {echo $v . " | ";}}echo "</b></font></p>";  $icecreamquestion = $_POST['icecreamquestion'];if (!isset($_POST['icecreamanswer'])) {$icecreamanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$icecreamanswer = $_POST['icecreamanswer'];switch ($icecreamanswer) {case 'Yes':echo "<p><font color='navy'>You said that you <b>do</b> like mint chocolate chip ice cream! </font></p>";break;case 'No':echo "<p><font color='navy'>You said that you <b>DO NOT</b> like mint chocolate chip ice cream!</font></p>";break;}}$careerquestion = $_POST['careerquestion'];$careeranswer = $_POST['careeranswer'];if ($careeranswer == 'none') {$careeranswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$careeranswer = $_POST['careeranswer'];echo "<p><font color='navy'>You answered that kids mostly aspire to be a(n) " . $careeranswer . "!</font></p>";} $experiencequestion = $_POST['experiencequestion'];if (!empty($_POST['experienceanswer'])) {$experienceanswer = $_POST['experienceanswer'];echo "<p><font color='navy'>Your response has been recorded!</font></p>";} else {$experienceanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} } ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>PHP Survery</title></head><body><form action="test.php" method="post">What is your favorite food?<br/><input type="hidden" name="foodquestion" value="What is your favorite food?" /><input type="text" name="foodanswer" size="60" /><br /><br />Which of the following do you like the best?<br/><input type="hidden" name="vegiequestion" value="Which of the following do you like the best?" /><input type="checkbox" name="vegieanswer[]" value="Cabbage" />Cabbage<br /><input type="checkbox" name="vegieanswer[]" value="Brussell Sprouts" />Brussell Sprouts<br /><input type="checkbox" name="vegieanswer[]" value="Green Beans" />Green Beans<br /><input type="checkbox" name="vegieanswer[]" value="Green Peppers" />Green Peppers<br /><br />Do you like mint chocolate chip flavored ice cream?<br/><input type="hidden" name="icecreamquestion" value="Do you like mint chocolate chip flavored ice cream?" /><input type="radio" name="icecreamanswer" value="Yes" />Yes<input type="radio" name="icecreamanswer" value="No" />No <br /><br />Select the occupation that you think most kids aspire to be:<br/><input type="hidden" name="careerquestion" value="Select the occupation that you think most kids aspire to be:" /><select name="careeranswer"><option value="none" selected="selected">--Select Occupation--</option><option value="Doctor">Doctor</option><option value="Lawyer">Lawyer</option><option value="Fire Fighter">Fire Fighter</option><option value="Police Officer">Police Officer</option><option value="President of the USA">President of the USA</option><option value="Internet Entrepreneur">Internet Entrepreneur</option></select><br /><br />Describe your happiest experience:<br/><input type="hidden" name="experiencequestion" value="Describe your happiest experience:" /><textarea name="experienceanswer" rows="10" cols="50"></textarea><br /><br /><input type="submit" name="submit" value="Submit Form" /></form><?phpif(isset($_POST['submit'])){if ($foodanswer != FALSE && $vegieanswer != FALSE && $icecreamanswer != FALSE && $careeranswer != FALSE && $experienceanswer != FALSE) {$newV = implode(' | ',$vegieanswer);$to = 'your@email.com';$subject = 'Poll Results';$body = "<html><head></head><body>Hi Your Name!<br />Your poll has just been completed. Here are the results:<br /><br />" . $foodquestion . "<br />Answer: " . $foodanswer . "<br /><br />" . $vegiequestion . "<br />Answer: " . $newV . "<br /><br />" . $icecreamquestion . "<br />Answer: " . $icecreamanswer . "<br /><br />" . $careerquestion . "<br />Answer: " . $careeranswer . "<br /><br />" . $experiencequestion . "<br />Answer: " . $experienceanswer . "<br /></body></html>";$headers = "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1\n" . "From: donotreply@email.com";mail ($to, $subject, $body, $headers);echo "<h2>Your information has been sent to the owner of the survey. Thank you for participating!";} else {echo "<p><font color='red'><b>Your information could not be sent because of one or more errors. Please try again.</b></font></p>";}}?></body></html>

Let me know if you have any questions about why it is working, what it happening, etc. ~Krewe

Edited by Krewe
Link to comment
Share on other sites

Thanks for the quick reply. I now can contiune work on it tonight. I am testing it off a hosting server. The only "server" I have is the IIS localhost and the real web host server. Besides this one-page PHP survery that I will incrop with the rest of my website, when I run my website (mostly menu, nothing else), it doesn't run on the hosting server. So, I did a live chat with someone there, he says that I can not have HTML and PHP on the same page. I don't understand because it works just right on my localhost. On that website, it's all PHP web page (because I want to have one menu page, and "include" it on every page, so changes can be done on one page, you know). Lastly, does this survery page in "correct" order? I mean doesn't (or should) the first line be

<!DOCTYPE html

That's what I meant in my question, is it in order arrangement (regardless if it doesn't matter). I even find that least few times with CSS. I will have to read it side-by-side with my html editor because copy/paste will put everything on one line. Chuck

Link to comment
Share on other sites

you can definitely mix PHP and HTML in the same page, you just need to have it in PHP page on a server running PHP. Using includes is a very common technique and should not be problem unless the host site doesn't support PHP. And yes, however your page is arranged and you are outputting an HTML document to the page, the very first line should be a DTD (doctype).

Link to comment
Share on other sites

you can definitely mix PHP and HTML in the same page, you just need to have it in PHP page on a server running PHP. Using includes is a very common technique and should not be problem unless the host site doesn't support PHP. And yes, however your page is arranged and you are outputting an HTML document to the page, the very first line should be a DTD (doctype).
I have learned to put the php code that you want to run when you submit a form, before ANY html. So that no headers conflict.
Link to comment
Share on other sites

I have learned to put the php code that you want to run when you submit a form, before ANY html. So that no headers conflict.
I was specifically referring to any output, like markup. It is very common for PHP to be "calculated" at the head of the document and then plugged into the page later down in the document.
Link to comment
Share on other sites

this is a syntex error try to check on line 117 or above that. probably you are missing a semicolon there. the error means there is a variable where it should not suppose to be.

Link to comment
Share on other sites

Yes, it was a missing semicolon. The webpage did appear and right away (before any inputs), the message displayed: Your information could not be sent because of one or more errors. Please try again. So, something is wrong (of course). Is it the PHP arrangements or something is inside of something? The problem is the error correction part. Chuck

Link to comment
Share on other sites

if ($foodanswer != FALSE && $vegieanswer != FALSE && $icecreamanswer != FALSE && $careeranswer != FALSE && $experienceanswer != FALSE) 

This line is evaluating false. try var_dump() the variables to see what it is holding which making it to be false. track that variable(s) first.

Edited by birbal
Link to comment
Share on other sites

Your information could not be sent because of one or more errors. Please try again.
If your questions was relating to this line: $newV = implode(' | ',$vegieanswer);I removed it, test it, it still show the red note. I have no idea what such do. Once again, I followed eHow.com and create this PHP survery webpage to learn. They did some, then explain some, so much was broken. This is my best what I came up with. So, if you can find where I might mistake, let me know. I am not knowledgeable with this, so once this works, I hope to learn from a working one, including "arrangements" or "placement" or even if I am supposed to assign values to variables? Chuck
Link to comment
Share on other sites

... if you didn't just copy and paste my code I know what is wrong. The error message is showing up because you haven't submitted anything yet so the:

if ($foodanswer != FALSE && $vegieanswer != FALSE && $icecreamanswer != FALSE && $careeranswer != FALSE && $experienceanswer != FALSE)

is coming up false, so it is running the else statement.I ran into this when I was testing it before I posted my code, so I already fixed it.The way to fix it is by adding:

if(isset($_POST['submit'])){}

Around the the end php code, that way it won't read it if the form hasn't been submitted.

Link to comment
Share on other sites

I even printout the codes, looked it over, even highlite stuff, I am still confused. According to this, the first section (now I am talking in "arrangements" placement): first section has:

if(isset($_POST['submit'])){}

how come I did the above code and then next line as:

$foodquestion = $_POST['foodquestion'];

and the remaining has:

if(isset($_POST['vegieanswer'])){

if(isset($_POST['icecreamanswer'])){

if(isset($_POST['careeranswer'])){

---------------------------------------------------------------------------------------------- second section has the DOCTYPE and the HTML page layout how come I have:

</form>

without the beginning "form" code? ---------------------------------------------------------------------------------------------- thrid section has another PHP code, is that "right" or "OK" or could the "arrangements" be better so that there one PHP code section? ---------------------------------------------------------------------------------------------- I still really need help with the coding... You could point out the error(s) by line #(s). Chuck

Link to comment
Share on other sites

CNT I address all of these things on my first post... Refer back to my first post about everything, however, he is the code again:

<?phpif (isset($_POST['submit'])) {$foodquestion = $_POST['foodquestion'];if (!empty($_POST['foodanswer'])) {$foodanswer = $_POST['foodanswer'];echo "<p><font color='navy'>You have chosen: <b>" . $foodanswer . "</b> as your favorite food!</font></p>";} else {$foodanswer = FALSE;echo "<p><font color='red'>Please tell us your favorite food!</font></p>";}if (!isset($_POST['vegieanswer'])) {$vegieanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$vegieanswer = $_POST['vegieanswer'];echo "<p><font color='navy'>You checked: <b>";foreach ($vegieanswer as $v) {echo $v . " | ";}}echo "</b></font></p>";$icecreamquestion = $_POST['icecreamquestion'];if (!isset($_POST['icecreamanswer'])) {$icecreamanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$icecreamanswer = $_POST['icecreamanswer'];switch ($icecreamanswer) {case 'Yes':echo "<p><font color='navy'>You said that you <b>do</b> like mint chocolate chip ice cream! </font></p>";break;case 'No':echo "<p><font color='navy'>You said that you <b>DO NOT</b> like mint chocolate chip ice cream!</font></p>";break;}}$careerquestion = $_POST['careerquestion'];$careeranswer = $_POST['careeranswer'];if ($careeranswer == 'none') {$careeranswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";} else {$careeranswer = $_POST['careeranswer'];echo "<p><font color='navy'>You answered that kids mostly aspire to be a(n) " . $careeranswer . "!</font></p>";}$experiencequestion = $_POST['experiencequestion'];if (!empty($_POST['experienceanswer'])) {$experienceanswer = $_POST['experienceanswer'];echo "<p><font color='navy'>Your response has been recorded!</font></p>";} else {$experienceanswer = FALSE;echo "<p><font color='red'><b>Please answer the question!</b></font></p>";}}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>PHP Survery</title></head><body><form action="test.php" method="post">What is your favorite food?<br/><input type="hidden" name="foodquestion" value="What is your favorite food?" /><input type="text" name="foodanswer" size="60" /><br /><br />Which of the following do you like the best?<br/><input type="hidden" name="vegiequestion" value="Which of the following do you like the best?" /><input type="checkbox" name="vegieanswer[]" value="Cabbage" />Cabbage<br /><input type="checkbox" name="vegieanswer[]" value="Brussell Sprouts" />Brussell Sprouts<br /><input type="checkbox" name="vegieanswer[]" value="Green Beans" />Green Beans<br /><input type="checkbox" name="vegieanswer[]" value="Green Peppers" />Green Peppers<br /><br />Do you like mint chocolate chip flavored ice cream?<br/><input type="hidden" name="icecreamquestion" value="Do you like mint chocolate chip flavored ice cream?" /><input type="radio" name="icecreamanswer" value="Yes" />Yes<input type="radio" name="icecreamanswer" value="No" />No <br /><br />Select the occupation that you think most kids aspire to be:<br/><input type="hidden" name="careerquestion" value="Select the occupation that you think most kids aspire to be:" /><select name="careeranswer"><option value="none" selected="selected">--Select Occupation--</option><option value="Doctor">Doctor</option><option value="Lawyer">Lawyer</option><option value="Fire Fighter">Fire Fighter</option><option value="Police Officer">Police Officer</option><option value="President of the USA">President of the USA</option><option value="Internet Entrepreneur">Internet Entrepreneur</option></select><br /><br />Describe your happiest experience:<br/><input type="hidden" name="experiencequestion" value="Describe your happiest experience:" /><textarea name="experienceanswer" rows="10" cols="50"></textarea><br /><br /><input type="submit" name="submit" value="Submit Form" /></form><?phpif(isset($_POST['submit'])){if ($foodanswer != FALSE && $vegieanswer != FALSE && $icecreamanswer != FALSE && $careeranswer != FALSE && $experienceanswer != FALSE) {$newV = implode(' | ',$vegieanswer);$to = 'your@email.com';$subject = 'Poll Results';$body = "<html><head></head><body>Hi Your Name!<br />Your poll has just been completed. Here are the results:<br /><br />" . $foodquestion . "<br />Answer: " . $foodanswer . "<br /><br />" . $vegiequestion . "<br />Answer: " . $newV . "<br /><br />" . $icecreamquestion . "<br />Answer: " . $icecreamanswer . "<br /><br />" . $careerquestion . "<br />Answer: " . $careeranswer . "<br /><br />" . $experiencequestion . "<br />Answer: " . $experienceanswer . "<br /></body></html>";$headers = "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1\n" . "From: donotreply@email.com";mail ($to, $subject, $body, $headers);echo "<h2>Your information has been sent to the owner of the survey. Thank you for participating!";} else {echo "<p><font color='red'><b>Your information could not be sent because of one or more errors. Please try again.</b></font></p>";}}?></body></html>

Link to comment
Share on other sites

I couldn't copy/paste... so, I did a side-by-side (word for word) and found few missing stuff. Then I test it on a web host, it appears to work, but I didn't get the email. I think it's the web host that doesn't offer this feature of PHP. I waited few days to see if it's the host holding, but nothing. Anyway, thanks for the help. It is going to be something I will need to do for my website soon. Chuck

Link to comment
Share on other sites

I couldn't copy/paste... so, I did a side-by-side (word for word) and found few missing stuff. Then I test it on a web host, it appears to work, but I didn't get the email. I think it's the web host that doesn't offer this feature of PHP. I waited few days to see if it's the host holding, but nothing. Anyway, thanks for the help. It is going to be something I will need to do for my website soon. Chuck
There are some conditions with mail().With all the people who use it to spam emails the big email clients have spam filters that may be considering your message spam.I was using my website to send a message to the same domain. (IE Www.codekrewe.com -> Test@codekrewe.com)That tends to help it not get eaten by the spam filter.
Link to comment
Share on other sites

Finally, it worked... lastly was I had to "point my domain to". The tech explained that I had to have a domain in their webhosting for spamming reasons. Anyways, it is working. Now, I can start from here, build up my website. It's going to be a slow year because I am back to school (I have AA degree, then a BA degree, now going for a cute diploma "degree"). Chuck

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...