How to solve a Notice: Undefined index?

12

Starting in PHP and following a tutorial I used the above code and ended up getting this error:

  

Notice: Undefined index: submit in C: \ wamp \ www \ mezzo-com \ reservas.php on line 3

How to solve it?

    
asked by anonymous 18.06.2014 / 04:07

3 answers

19

How is the form that calls C:\wamp\www\mezzo-com\reservas.php ?

It needs to have a button being posted that sends information to the script PHP. It should have something like:

<input type="submit" value="Submit" name="submit">

In fact it is possible that you have problems with other fields ( name , mail , age , people , etc.) Without knowing the page that calls script more accurate information. But it is possible that you are even submitting a form and calling PHP script directly. Or it's calling but sending with GET instead of POST . There should be something like:

<form action="reservas.php" method="post">

But if you are calling the script in a context where this data is not available, then you need to make access to the element not happen by switching its if on line 3 to:

if (isset($_POST["submit"])) {

In this way you check the existence of the variable and its index without trying to access its value, because the value of it is not important, but its existence. You'll be checking to see if the operation can be done before letting the crash occur.

    
18.06.2014 / 04:36
11

This error indicates that there is no value in your $_POST variable with the name "submit" . Or your form did not include this field, or the request is not a POST (I'm not sure about this second possibility as I do not have any practical experience with PHP).

Check the form that calls this URL. But if you want to make your code more robust, you can check if the key exists before using it through the function array_key_exists or isset (the second also check if the value is null):

if (array_key_exists("submit", $_POST)){    // Entra se "submit" existe

if (isset($_POST["submit"])){               // Entra se "submit" existe e não é null
    
18.06.2014 / 04:35
1

The problem is the lack of the submit field in your form.

Make sure the form method is POST, otherwise html by default will use GET.

    
15.06.2016 / 15:02