How to solve a Notice: Undefined index?

Asked

Viewed 104,158 times

17

SS

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?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

24

What’s the form that calls the C:\wamp\www\mezzo-com\reservas.php?

It needs to have a button being posted that sends information to the script PHP. 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 the script it is complicated to give more accurate information. But it is possible that you are not even submitting a form and calling the script PHP direct. Or is calling but sending with GET instead of POST. There must be something like:

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

But if you’re calling the script in a context where this data is not available, then you need to make the access to the element does not happen by exchanging your if on line 3 to:

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

I put in the Github for future reference.

This way you check the existence of the variable and its index without trying to access its value, even because its value is not important but its existence. You will be checking if the operation can be done before letting the failure occur.

14

This error indicates that there is no value in your variable $_POST with the name "submit". Either your form did not include this field, or the request is not a POST (I’m not sure about this second possibility, as I have no 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 checks whether 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

1

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.