Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 05-18-2009, 01:25 PM php post to self
Novice Talker

Posts: 5
Name: james
Trades: 0
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 />";
}
?>
saltstein is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-18-2009, 01:49 PM Re: php post to self
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-18-2009, 02:08 PM Re: php post to self
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
  1. Please use code tags -- makes everything so pretty and easy to help with
  2. 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.
  3. 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
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-18-2009, 03:17 PM Re: php post to self
Novice Talker

Posts: 5
Name: james
Trades: 0
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 View Post
  1. Please use code tags -- makes everything so pretty and easy to help with
  2. 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.
  3. 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>
saltstein is offline
Reply With Quote
View Public Profile
 
Old 05-18-2009, 03:23 PM Re: php post to self
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-18-2009, 03:32 PM Re: php post to self
Novice Talker

Posts: 5
Name: james
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
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


saltstein is offline
Reply With Quote
View Public Profile
 
Old 05-18-2009, 03:39 PM Re: php post to self
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Very intriguing. Here's my file.
Attached Files
File Type: zip index.zip (870 Bytes, 5 views)
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-18-2009, 03:45 PM Re: php post to self
Novice Talker

Posts: 5
Name: james
Trades: 0
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 View Post
Very intriguing. Here's my file.
saltstein is offline
Reply With Quote
View Public Profile
 
Old 05-18-2009, 03:49 PM Re: php post to self
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-18-2009, 08:18 PM Re: php post to self
Novice Talker

Posts: 5
Name: james
Trades: 0
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 View Post
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).
saltstein is offline
Reply With Quote
View Public Profile
 
Old 05-18-2009, 08:23 PM Re: php post to self
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to php post to self
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.17072 seconds with 13 queries