-2

So here's the code. We were assigned to create a simple form. I've read a lot of tutorials about PHP and all the codes here were from the internet. Can you guys please help me with this matter -_- Whenever I try to open it it keeps on displaying PARSE ERROR : SYNTAX ERROR and Notice: Undefined index: fname line 2 up to line 8.

<?php
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$gender = $_POST["gender"];
$food = $_POST["food"];
$textbox = $_POST["textbox"];
$leducation = $_POST["leducation"];
$time = $_POST["time"];
if (!isset($_POST['submit']))
{
?>
<html>
<body>
<head>
<title> Midterm Exam </title>
</head>
<form action"jr.php" method="post">
First Name : <input type="text" size="12" maxLength="15" name="fname">
<br>
Last Name : <input type="text" size="12" maxLength="15" 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 Your Favorite Foods:<br>
Steak:<input type="checkbox" value="Steak" name="food[]"> <br>
Pizza: <input type="checkbox" value="Pizza" name="food[]"> <br>
Pasta : <input type="checkbox" value="Pasta" name="food[]"> <br>
<textarea rows="4" cols="30" name="textbox" > Enter your Favorite Quote!</textarea><br>
Select a Level of Education:<br>
<select name="leducation">
<option value="PS">Pre-School</option>
<option value="E">Elementary</option>
<option value="HS">High School </option>
<option value="C"> College </option>
</select><br>
Select Your Favorite time of the Day:<br>
<select name="time" 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>
<?php
} 
else {
echo "Hello , ".$fname." ".$lname.". <br>";
echo "You are ".$gender." . and you like";
foreach ( $food as $fod)
{
echo $fod."<br>";
echo "<i>".$quote."</i><br>";
echo "You're Favorite time is ".$time.", and you passed ".$leducation."!<br>";
}
}
?>
</body>
</html>
  • Move the lines where you're assigning the variables from $_POST into the `else` part of your statement. At the moment, they're running every time the page is called, and on the first load, $_POST isn't set, so you get the undefined index error. – andrewsi Aug 19 '13 at 14:57
  • `$_POST` vars needs to be inside your `if(!isset($_POST["submit))` statement. – Ben Fortune Aug 19 '13 at 14:58
  • 3
    You need to learn how to [READ and debug](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) error messages. Everything you needed to solve the problem was in the error message. Unless you have POSTed form information then $_POST['blah'] is not going to be set – Anigel Aug 19 '13 at 14:59
  • 1
    Thank you. Its my first time to create such , I'll keep those in mind :) – Samantha Chima Aug 19 '13 at 15:17

3 Answers3

0

When you first load the page, there was no submit.

All those line should be in the else clause of if (!isset($_POST['submit'])) {.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

The first time you run this form, there will be no values in $_POST becasue you have not PRESSED the button to send the POST array.

You need to add some code around the first piece of PHP to decide if this is the first run or the data has been posted i.e. the button was pressed.

Try:-

if (!isset($_POST['submit'])) {       
    $fname = $_POST["fname"];
    $lname = $_POST["lname"];
    $gender = isset( $_POST["gender"] ) ? $_POST["gender"] : '';
    $food = $_POST["food"];
    $textbox = $_POST["textbox"];
    $leducation = $_POST["leducation"];
    $time = $_POST["time"];

?>
<html>
<head>
   <title> Midterm Exam </title>
</head>
<body>
    <form action"jr.php" method="post">
        First Name : <input type="text" size="12" maxLength="15" name="fname"><br>
        Last Name : <input type="text" size="12" maxLength="15" 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 Your Favorite Foods:<br>
        Steak:<input type="checkbox" value="Steak" name="food[]"> <br>
        Pizza: <input type="checkbox" value="Pizza" name="food[]"> <br>
        Pasta : <input type="checkbox" value="Pasta" name="food[]"> <br>
        <textarea rows="4" cols="30" name="textbox" > Enter your Favorite Quote!</textarea><br>
        Select a Level of Education:<br>
        <select name="leducation">
            <option value="PS">Pre-School</option>
            <option value="E">Elementary</option>
            <option value="HS">High School </option>
            <option value="C"> College </option>
        </select><br>
        Select Your Favorite time of the Day:<br>
        <select name="time" 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>
<?php
} else {
    echo "Hello , ".$fname." ".$lname.". <br>";
    echo "You are ".$gender." . and you like";
    foreach ( $food as $fod) {
        echo $fod."<br>";
        echo "<i>".$quote."</i><br>";
        echo "You're Favorite time is ".$time.", and you passed ".$leducation."!<br>";
    }
}
?>
</body>
</html>

Also your <head> tag was in yout <body> so I moved it to the right place.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • It displayed Notice: Undefined index: gender in C:\wamp\www\jana\lala.php on line 5,6,9. Im really sorry. Honestly im a newbie here -_- and our teacher didn't even introduced this to us , that why – Samantha Chima Aug 19 '13 at 15:26
  • Gender is a radio button, and will only exist in the $_POST array if it is Checked. so see my changes – RiggsFolly Aug 19 '13 at 15:34
0

@samantha $_POST variables need to be inside your if(isset($_POST["Submit"])) statement. They are null at first and will get a value after form submission. The isset function will ensure the form was submitted.

working perfect

koubin
  • 579
  • 5
  • 9
  • 30