 |
|
|
05-18-2009, 01:25 PM
|
php post to self
|
Posts: 5
Name: james
|
I am new to php. I am using this example below (which I have found on the web) to learn how to post forms to the same page. I cannot get the script to work because I get a parse error and I am not able to figure out the problem. I would be grateful for help because I have been playing around for hours and my head hurts. Many thanks in advance.
Regards,
Saltstein
<?php
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$food = $_POST["food"];
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br />
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
Gender:<br />
Male:<input type="radio" value="Male" name="gender"><br />
Female:<input type="radio" value="Female" name="gender"><br />
Please choose type of residence:<br />
Steak:<input type="checkbox" value="Steak" name="food[]"><br />
Pizza:<input type="checkbox" value="Pizza" name="food[]"><br />
Chicken:<input type="checkbox" value="Chicken" name="food[]"><br />
<textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea><br />
Select a Level of Education:<br />
<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select><br />
Select your favorite time of day:<br />
<select name="TofD" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select><br />
<input type="submit" value="submit" name="submit">
</form>
<?
} else {
echo "Hello, ".$Fname." ".$Lname.".<br />";
echo "You are ".$gender.", and you like ";
foreach ($food as $f) {
echo $f."<br />";
}
echo "<i>".$quote."</i><br />";
echo "You're favorite time is ".$TofD.", and you passed ".$education."!<br />";
}
?>
|
|
|
|
05-18-2009, 01:49 PM
|
Re: php post to self
|
Posts: 2,815
Name: Matt
Location: Irvine, CA
|
What is the error message and line number? Also, $PHP_SELF assumes register globals are on. Change it to $_SERVER['PHP_SELF'] if it isn't working correctly. You shouldn't get in the habbit of relying on register globals because you can't use them if you expect your script to be portable, and they will be removed in PHP 6.
|
|
|
|
05-18-2009, 02:08 PM
|
Re: php post to self
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
- Please use code tags -- makes everything so pretty and easy to help with
- You have more problems with this code than what you've asked -- XSS is a breeze on this, naming a button Submit will cause problems with javascript integration (in some cases), etc.
- Errors matter, so as Matt asked, please include them. Your particular error was on your foreach line b/c $food is not necessarily set and therefore not necessarily able to be passed to foreach. These code changes will help:
PHP Code:
<?php $Fname = $_POST["Fname"]; $Lname = $_POST["Lname"]; $gender = $_POST["gender"]; $food = $_POST["food"]; $quote = $_POST["quote"]; $education = $_POST["education"]; $TofD = $_POST["TofD"]; if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form ?> <html> <head> <title>Personal INFO</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> First Name:<input type="text" size="12" maxlength="12" name="Fname"><br /> Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br /> Gender:<br /> Male:<input type="radio" value="Male" name="gender"><br /> Female:<input type="radio" value="Female" name="gender"><br /> Please choose type of residence:<br /> Steak:<input type="checkbox" value="Steak" name="food[]"><br /> Pizza:<input type="checkbox" value="Pizza" name="food[]"><br /> Chicken:<input type="checkbox" value="Chicken" name="food[]"><br /> <textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea><br /> Select a Level of Education:<br /> <select name="education"> <option value="Jr.High">Jr.High</option> <option value="HighSchool">HighSchool</option> <option value="College">College</option></select><br /> Select your favorite time of day:<br /> <select name="TofD" size="3"> <option value="Morning">Morning</option> <option value="Day">Day</option> <option value="Night">Night</option></select><br /> <input type="submit" value="submit" name="submit"> </form> <? } else { echo "Hello, ".$Fname." ".$Lname.".<br />"; echo "You are ".$gender.", and you like "; if (is_array($food)) { foreach ($food as $f) { echo $f."<br />"; } } echo "<i>".$quote."</i><br />"; echo "You're favorite time is ".$TofD.", and you passed ".$education."!<br />"; } ?> </body> </html>
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-18-2009, 03:17 PM
|
Re: php post to self
|
Posts: 5
Name: james
|
Thanks Matt and Jeremy. However, I am still getting the following error:
Parse error: parse error in C:\wamp\www\formexample.php on line 53
any further help would be greatly appreciated
Regards,
James
Quote:
Originally Posted by JeremyMiller
- Please use code tags -- makes everything so pretty and easy to help with
- You have more problems with this code than what you've asked -- XSS is a breeze on this, naming a button Submit will cause problems with javascript integration (in some cases), etc.
- Errors matter, so as Matt asked, please include them. Your particular error was on your foreach line b/c $food is not necessarily set and therefore not necessarily able to be passed to foreach. These code changes will help:
PHP Code:
<?php $Fname = $_POST["Fname"]; $Lname = $_POST["Lname"]; $gender = $_POST["gender"]; $food = $_POST["food"]; $quote = $_POST["quote"]; $education = $_POST["education"]; $TofD = $_POST["TofD"]; if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form ?> <html> <head> <title>Personal INFO</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> First Name:<input type="text" size="12" maxlength="12" name="Fname"><br /> Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br /> Gender:<br /> Male:<input type="radio" value="Male" name="gender"><br /> Female:<input type="radio" value="Female" name="gender"><br /> Please choose type of residence:<br /> Steak:<input type="checkbox" value="Steak" name="food[]"><br /> Pizza:<input type="checkbox" value="Pizza" name="food[]"><br /> Chicken:<input type="checkbox" value="Chicken" name="food[]"><br /> <textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea><br /> Select a Level of Education:<br /> <select name="education"> <option value="Jr.High">Jr.High</option> <option value="HighSchool">HighSchool</option> <option value="College">College</option></select><br /> Select your favorite time of day:<br /> <select name="TofD" size="3"> <option value="Morning">Morning</option> <option value="Day">Day</option> <option value="Night">Night</option></select><br /> <input type="submit" value="submit" name="submit"> </form> <? } else { echo "Hello, ".$Fname." ".$Lname.".<br />"; echo "You are ".$gender.", and you like "; if (is_array($food)) { foreach ($food as $f) { echo $f."<br />"; } } echo "<i>".$quote."</i><br />"; echo "You're favorite time is ".$TofD.", and you passed ".$education."!<br />"; } ?> </body> </html>
|
|
|
|
|
05-18-2009, 03:23 PM
|
Re: php post to self
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Line 53 is just my closing html tag. I tested this and it worked fine, so make sure you've saved the file correctly.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-18-2009, 03:32 PM
|
Re: php post to self
|
Posts: 5
Name: james
|
Quote:
Originally Posted by JeremyMiller
Line 53 is just my closing html tag. I tested this and it worked fine, so make sure you've saved the file correctly.
|
Thanks for your prompt reply Jeremy. I highlighted your script and copied it into notepad2. I saved it as a .php script. Whilst trying to load on localhost I got the same error:
Parse error: parse error in C:\wamp\www\test.php on line 53

|
|
|
|
05-18-2009, 03:39 PM
|
Re: php post to self
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Very intriguing. Here's my file.
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-18-2009, 03:45 PM
|
Re: php post to self
|
Posts: 5
Name: james
|
Thanks for your continued help but still getting the same error with your file. I am using wamp as my server, do I need to do anything with the settings for this to work do you know?
Cheers,
James
Quote:
Originally Posted by JeremyMiller
Very intriguing. Here's my file.
|
|
|
|
|
05-18-2009, 03:49 PM
|
Re: php post to self
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
No clue. I just installed PHP, MySQL, and Apache myself and there's nothing sophisticated about this code. Maybe you have some extra hidden characters in there? Notepad++ gives you the option of changing the format (e.g. Windows, Unix, and Mac). Maybe try tweaking that setting? Otherwise, I only added 2 lines: The if() and it's close (well, and the missing HTML at the bottom).
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
05-18-2009, 08:18 PM
|
Re: php post to self
|
Posts: 5
Name: james
|
Jeremy just a note to say thanks. I logically thought out the problem and traced it back to my localhost server wamp. under php settings I turned on short open tag and the script worked. Thanks again for helping me along the way.
Regards,
james
Quote:
Originally Posted by JeremyMiller
No clue. I just installed PHP, MySQL, and Apache myself and there's nothing sophisticated about this code. Maybe you have some extra hidden characters in there? Notepad++ gives you the option of changing the format (e.g. Windows, Unix, and Mac). Maybe try tweaking that setting? Otherwise, I only added 2 lines: The if() and it's close (well, and the missing HTML at the bottom).
|
|
|
|
|
05-18-2009, 08:23 PM
|
Re: php post to self
|
Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
|
Instead of enabling short open tags, you could replace all <? that are by themselves with <?php . I didn't realize how that would affect the code, so thanks for letting us know. Next time I can point that out much faster!
__________________
Jeremy Miller
Please login or register to view this content. Registration is FREE
|
|
|
|
|
« Reply to php post to self
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|