0

I am getting following errors on my Webpage

Notice: Undefined index: name in C:\xampp\htdocs\CWH\index.php on line 14

Notice: Undefined index: gender in C:\xampp\htdocs\CWH\index.php on line 15

Notice: Undefined index: age in C:\xampp\htdocs\CWH\index.php on line 16

Notice: Undefined index: email in C:\xampp\htdocs\CWH\index.php on line 17

Notice: Undefined index: phone in C:\xampp\htdocs\CWH\index.php on line 18

Notice: Undefined index: other in C:\xampp\htdocs\CWH\index.php on line 19

Below is the corrosponding PHP code

$name = $_POST['name'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$email = $_POST['email'];
$phone= $_POST['phone'];
$other = $_POST['other'];

$sql = "INSERT INTO `trip` (`name`, `age`, `gender`, `email`, `phone`, `other`, `dt`) 
VALUES ('$name', '$age', '$gender', '$email', '$phone', '$other', current_timestamp());";

Below is the html code

<form action="index.php" method="post">
        <input type="text" name="name" id="name" placeholder="Enter your name">
        <input type="text" name="age" id="age" placeholder="Enter your Age">
        <input type="text" name="gender" id="gender" placeholder="Enter your gender">
        <input type="email" name="email" id="email" placeholder="Enter your email">
        <input type="phone" name="phone" id="phone" placeholder="Enter your phone no.">
        <textarea name="other" id="other" cols="30" rows="10" placeholder="Enter any other 
        information here"> </textarea>
   

Can anyone please tell me why I am getting this error.

Andrew Hardiman
  • 929
  • 1
  • 15
  • 30
Maddy
  • 1
  • 1
  • You should check that the array elements for the `$_POST` superglobal array are not empty to prevent this undefined index error. – Robert Oct 12 '20 at 01:24

2 Answers2

1

As i see, you're not checking if the form is submitted, which means the PHP-Code is executed every time the page loads, not only if the form was submitted.

So, if you open the page, the $_POST array is empty, so name, gender, ... are undefined.

To fix this, you can give your submit-button a name and check if this is inside the $_POST array in your PHP-Code.

For example, if your submit button has this name <input type="submit" value="Send" name="send">, put your PHP-Code inside this if-statement:

if(isset($_POST["send"])) {
   $name = $_POST["name"];
   ...
}

If you want to completly remove warnings like this, check this for every single variable in the array with isset($_POST["name"]) for the name and so on.

EinLinuus
  • 625
  • 5
  • 16
1

You can add the submit action through a button. So button html code will be <input type="submit" name="Submit" value="Update"> . add this html code before close the form tag.

Then update the PHP code as like below .

if(isset($_POST['Submit']))
{
$name = $_POST['name'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$email = $_POST['email'];
$phone= $_POST['phone'];
$other = $_POST['other'];

$sql = "INSERT INTO `trip` (`name`, `age`, `gender`, `email`, `phone`, `other`, `dt`) 
VALUES ('$name', '$age', '$gender', '$email', '$phone', '$other', current_timestamp());";
}`
Sazal Ahmed
  • 97
  • 1
  • 10