Jump to content

Problem with form and POST variable


Recommended Posts

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

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

Edited by chokk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...