apglr21 Posted April 25, 2008 Share Posted April 25, 2008 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... Quote Link to comment Share on other sites More sharing options...
947740 Posted April 25, 2008 Share Posted April 25, 2008 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."! "; } ?> Quote Link to comment Share on other sites More sharing options...
BrianM Posted April 25, 2008 Share Posted April 25, 2008 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> Quote Link to comment Share on other sites More sharing options...
947740 Posted April 25, 2008 Share Posted April 25, 2008 Whoa. I completely forgot about the last else. That would mess everything up. Quote Link to comment Share on other sites More sharing options...
Poddy Posted April 25, 2008 Share Posted April 25, 2008 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 . "!"; } ?> Quote Link to comment Share on other sites More sharing options...
BrianM Posted April 25, 2008 Share Posted April 25, 2008 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']; ?> Quote Link to comment Share on other sites More sharing options...
947740 Posted April 25, 2008 Share Posted April 25, 2008 Way to put it lightly. Quote Link to comment Share on other sites More sharing options...
BrianM Posted April 25, 2008 Share Posted April 25, 2008 Way to put it lightly. I try, I try. Well then again, he's just learning, and two, he didn't write that script himself, so really it was somebody else's mistake. But just a lesson learned in clean/formal code. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted April 25, 2008 Share Posted April 25, 2008 They make the exit(); language construct for a reason. You can exit after displaying the variables from the form you don't display the form again... Quote Link to comment Share on other sites More sharing options...
apglr21 Posted April 25, 2008 Author Share Posted April 25, 2008 If this code(my original code) is working on a remote site, and does not work on my localhost, what can be the problem...? By the way, I'm using the same version of php and the global_variables are both set to ON... Quote Link to comment Share on other sites More sharing options...
apglr21 Posted April 25, 2008 Author Share Posted April 25, 2008 why won't this work??? ??? if (isset($_POST['formsubmitted']!="" )) { Quote Link to comment Share on other sites More sharing options...
dptr1988 Posted April 25, 2008 Share Posted April 25, 2008 It should be like this: if (isset($_POST['formsubmitted']) AND $_POST['formsubmitted'] != "" ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.