VelvetMirror Posted March 19, 2011 Share Posted March 19, 2011 So I'm doing an example using a little mysql database and a form.I use the $_POST['submit'] variable in order to check if the submit button has been pressed.If it's pressed, I show the data extracted from the mysql database. But I get this error on the $_POST['submit'] line: Notice: Undefined index: submit in C:\xampp\htdocs\php\mysql\getdata.php on line 2 This is the line: if ($_POST['submit']) Complete code: <?phpif ($_POST['submit']){ require("connect.php"); echo "<br />"; $date = date("Y-m-d"); /* Extract data. */ $query = "SELECT * FROM people ORDER BY id DESC"; $extract = mysql_query($query); while ($row = mysql_fetch_assoc($extract)) { $firstName = $row['firstName']; $lastName = $row['lastName']; $birth = $row['birth']; $gender = $row['gender']; if ($gender == 'F') { $gender = 'Female'; } else { $gender = 'Male'; } echo "$firstName $lastName was born on $birth and is $gender"; echo "<br />"; }}?><form action = "getdata.php" method="post" > First Name: <input type = "text" name="firstName" /> <br /> Last Name: <input type = "text" name="lastName" /> <br /> <input type = "submit" name ="submit" value="Get Data"/></form> Any idea what causes this? Link to comment Share on other sites More sharing options...
jeffman Posted March 19, 2011 Share Posted March 19, 2011 It sounds like you are testing that value to see if you have received a POST request. It might be simpler to test that directly, as in:if ($_SERVER['REQUEST_METHOD'] == "POST") { Link to comment Share on other sites More sharing options...
chokk Posted March 20, 2011 Share Posted March 20, 2011 The problem with this line: if ($_POST['submit']) is that PHP expects the index to be set. It's throwing an error because that index of the array is not defined.All you have to do is change your line into this line: if (isset($_POST['submit'])) the isset() function tests if a variable has been set. That should fix your problem Link to comment Share on other sites More sharing options...
VelvetMirror Posted March 23, 2011 Author Share Posted March 23, 2011 Thanks, that fixed the problem. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.