PDA

View Full Version : PHP Help: Undefined Index



Tuts
08-06-2012, 11:46 AM
I'm currently using XAMPP and practising some PHP, however I've stumbled across a small problem.

I won't post all of my code as it's extremely long, so I'll post a section of it. We're dealing with buttons and performing actions after a button has been pressed.


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

<?php
if($_POST['submit'])
{
echo 'You\'ve pressed the submit button!';
}
?>

This piece of code throws this error, Notice: Undefined index: submit in C:\xampp\htdocs\test.php on line 4 (Line 4 is this code - if($_POST['submit'])). Does anyone know the problem with this code? When I click on the submit button, it doesn't echo out "You've pressed the submit button!".

Help would be appreciated.

iAdam
08-06-2012, 11:55 AM
Undefined index means you're trying to evaluate a variable, or in this case an element of an array, that doesn't exist at that point in the run of the code. Also, please say that button is in <form> tags.

Recursion
08-06-2012, 12:55 PM
Post the rest of your code. $_POST['submit'] isn't set.

Chippiewill
08-06-2012, 04:36 PM
you may may want to use if(isset($_POST['submit'])) instead

Dentafrice
12-06-2012, 01:36 AM
$_POST is an array, 'submit' is the index that you're trying to pull out of the array. You're trying to get and check it when it's not there.

Want to hide these adverts? Register an account for free!