Jump to content

$_POST Please Help Newbie, Going Crazy...


apglr21

Recommended Posts

I'm a newbie to PHP, and was doing pretty well into I ran into forms. I'm running on IIS 6.0, and using PHP ver 4.3.9, and mysql 4.0.20a-nt for the purpose of testing an old php app that I need to migrate. I've recreated my problem and brought it down to the following code that keeps giving me an error. If I can get this to work I'll probably be in better shape for the purpose of the migration but I've tried everything I can possibly think off and keep getting errors and now need help. Thanks in advance...

MY TEST 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 ";

foreach ($food as $f) {

echo $f."<br />";

}

echo "<i>".$quote."</i><br />";

echo "You're favorite time is ".$TofD.", and you passed ".$education."!<br />";

}

?>

 

My Errors when I run this page as http://localhost/test.php, I get to see the form after these errors:

 

Notice: Undefined index: Fname in c:\inetpub\wwwroot\test.php on line 4

 

Notice: Undefined index: Lname in c:\inetpub\wwwroot\test.php on line 5

 

Notice: Undefined index: gender in c:\inetpub\wwwroot\test.php on line 6

 

Notice: Undefined index: food in c:\inetpub\wwwroot\test.php on line 7

 

Notice: Undefined index: quote in c:\inetpub\wwwroot\test.php on line 8

 

Notice: Undefined index: education in c:\inetpub\wwwroot\test.php on line 9

 

Notice: Undefined index: TofD in c:\inetpub\wwwroot\test.php on line 10

.....

Form comes up with no problem after these error, but what is going on with my post variables?

$Fname = $_POST["Fname"];

$Lname = $_POST["Lname"];

$gender = $_POST["gender"];

$food = $_POST["food"];

$quote = $_POST["quote"];

$education = $_POST["education"];

$TofD = $_POST["TofD"];

your help is greatly appreciated...

 

Link to comment
Share on other sites

Change the first part to:

 

<?php
if(isset($_POST['submit'])) {
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$gender = $_POST["gender"];
$food = $_POST["food"];
$quote = $_POST["quote"];
$education = $_POST["education"];
$TofD = $_POST["TofD"];
}
else {
?>
<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">

Last Name:<input type="text" size="12" maxlength="36" name="Lname">

Gender:

Male:<input type="radio" value="Male" name="gender">

Female:<input type="radio" value="Female" name="gender">

Please choose type of residence:

Steak:<input type="checkbox" value="Steak" name="food[]">

Pizza:<input type="checkbox" value="Pizza" name="food[]">

Chicken:<input type="checkbox" value="Chicken" name="food[]">

<textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea>

Select a Level of Education:

<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select>

Select your favorite time of day:

<select name="TofD" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select>

<input type="submit" value="submit" name="submit">
</form>
<?
} else {
echo "Hello, ".$Fname." ".$Lname.".
";
echo "You are ".$gender.", and you like ";
foreach ($food as $f) {
echo $f."
";
}
echo "".$quote."
";
echo "You're favorite time is ".$TofD.", and you passed ".$education."!
";
}
?>

Link to comment
Share on other sites

Or I was going to say, use this right here. I rewrote it just a bit, but made sure you wont get any errors when you load the page.

 

<html>
<head>
<title>Personal INFO</title>
</head>

<?php

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

echo "Hello, ".$_POST['first_name']." ".$_POST['last_name'].".<br /><br />";

echo "You are ".$_POST['gender'].", and you like ";

echo $_POST['food'].".<br /><br />";

echo "".$_POST['quote'].".<br /<br />";

echo "You're favorite time is ".$_POST['TofD'].", and you passed ".$_POST['education']."!";

}

?>

<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

First Name:<input type="text" size="12" maxlength="12" name="first_name">

Last Name:<input type="text" size="12" maxlength="36" name="last_name">

Gender:

Male:<input type="radio" value="Male" name="gender">

Female:<input type="radio" value="Female" name="gender">

Please choose type of residence:

Steak:<input type="checkbox" value="Steak" name="food">

Pizza:<input type="checkbox" value="Pizza" name="food">

Chicken:<input type="checkbox" value="Chicken" name="food">

<textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea>

Select a Level of Education:

<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select>

Select your favorite time of day:

<select name="TofD" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select>

<input type="submit" value="submit" name="submit">
</form>

</body>
</html>

Link to comment
Share on other sites

use of (isset($_POST['submit'])) for the processing of the form..

take not that the $_POST vars need to be inside this segment of code.

i advise you to use a diffrent page for the processing of the page such as action.php

that way it will not display the form but only display the results you want, as long as you keep the echoing in that file

 

form.php

<html>
<head>
<title>Personal INFO</title>
</head>
<body>
<form method="post" action="action.php">
First Name:<input type="text" size="12" maxlength="12" name="Fname">

Last Name:<input type="text" size="12" maxlength="36" name="Lname">

Gender:

Male:<input type="radio" value="Male" name="gender">

Female:<input type="radio" value="Female" name="gender">

Please choose type of residence:

Steak:<input type="checkbox" name="steak">

Pizza:<input type="checkbox" name="pizza">

Chicken:<input type="checkbox" name="chicken">

<textarea rows="5" cols="20" name="quote" wrap="physical">Enter your favorite quote!</textarea>

Select a Level of Education:

<select name="education">
<option value="Jr.High">Jr.High</option>
<option value="HighSchool">HighSchool</option>
<option value="College">College</option></select>

Select your favorite time of day:

<select name="TofD" size="3">
<option value="Morning">Morning</option>
<option value="Day">Day</option>
<option value="Night">Night</option></select>

<input type="submit" value="submit" name="submit">
</form>

</body>
</html>

 

action.php

<?php

#if the form is submitted:
$food = array();

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

#register form values as vars
$Fname = $_POST['Fname'];
$Lname = $_POST['Lname'];
$gender = $_POST['gender'];
$steak = $_POST['steak'];
$pizza = $_POST['pizza'];
$chicken = $_POST['chicken'];
$quote = $_POST['quote'];
$education = $_POST['education'];
$TofD = $_POST['TofD'];


if ($steak == Steak) { $food[] = 'steak'; }
if ($pizza == Pizza) { $food[] = 'pizza'; }
if ($chicken == Chicken) { $food[] = 'chicken'; }




echo "Hello, " . $Fname . " " . $Lname . ".";

echo "You are ".$gender.", and you like ";


foreach ($food as $value) { 
echo " " . $value . " "; 
}



echo $quote;

echo "You're favorite time is " . $TofD . ", and you passed " . $education . "!";
}








?>

Link to comment
Share on other sites

Whoa.  I completely forgot about the last else.  That would mess everything up.

 

It would have spelled disaster.

 

As for the $PHP_SELF; you were using, try to get used to using a full global variable like so;

<?php $_SERVER['PHP_SELF']; ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.