1

I keep getting the following error and can not figure out what is going wrong.

Notice: Undefined index: name in C:\xampp\htdocs\FYP\resProcessing.php on line 99

The error relates to the following line:

$name = $_POST['name'];

The following below is my code:

if ($quick_check != 0){
    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){

        $id = $row['id'];
        $tablenum = $row['tablenum'];
        $avail = $row['avail'];

        $spots .= 'You just reserved table '.$tid.'.<br />';
        $spots .= 'You only have 8 minutes to finish or your reservation will expire and the table will open up to other people.<br />';
    }

    $availNow = $avail - $num;

    $sql = "UPDATE available SET avail='$availNow' WHERE id='$id' LIMIT 1";
    $query = mysqli_query($connect, $sql);

    $sql = "INSERT INTO reserves(tablenumber,numseats,restime) VALUES ('$tablenum','$num',now())";
    $query = mysqli_query($connect, $sql);

    $reserveID = mysqli_insert_id($connect);
    $spots .= '<form name="confirmform" id="confirmform" method="post" onSubmit="return false;">';
    $spots .= 'Full Name: &nbsp;<input type="text" name="name" id="name" required autofocus placeholder="Your Name" pattern="[a-zA-Z]+{3,}" title="Please enter your full name."></br>';

    // hidden field holds the table name
    $spots .= '<input id="tableNumber" type="hidden" value="'.$tablenum.'">';
    // hidden field holds the number of seats
    $spots .= '<input id="numSeats" type="hidden" value="'.$num.'">';
    // hidden field holds the reserve insert id
    $spots .= '<input id="reserveID" type="hidden" value="'.$reserveID.'">';

    // On submit call js function
    $spots .= '<button id="confirmbtn" onClick="confirmSeats();updateInfo();">Make Reservations</button></br>';

    $name = $_POST['name'];

    $sql = "UPDATE available SET name='$name' WHERE id='$id' LIMIT 1";
    $query = mysqli_query($connect, $sql);

    $spots .= '</form>';
    $spots .= '<button id="cancelbtn" onClick="cancelReserve('.$reserveID.')">Cancel Reservation</button>';


} else {
    $spots .= "Sorry, someone just reserved those. Try another table";
    $reserveID = "open";
}           

echo "$spots|$reserveID";
exit();
}

I would really appreciate if anybody could help. Thanks!

bcesars
  • 1,016
  • 1
  • 17
  • 36
Amy
  • 591
  • 3
  • 10
  • 23
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) –  Mar 03 '15 at 19:31
  • where is `html` code with this input? – bcesars Mar 03 '15 at 19:33

2 Answers2

1

The reason is that the POST request does not contain name parameter.

Or HTTP request type is not POST in your case.

Try $name = $_REQUEST['name']; — it would account for GET variables (and cookies) as well. If it doesn't help, fix your client (be it HTML form, JavaScript or something else).

You may also check if POST variable is defined before trying to access it, e.g.:

if (isset($_POST['name']))
{ 
    $name = $_POST['name'];
} 
else 
{ 
    echo "Name is required"; 
    // ...
}
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
  • The POST variable isn't registering as defined. I tried placing the following after the submit button but still had no luck: $name = $_POST['name']; $sql = "UPDATE available SET name='$name' WHERE id='$id' LIMIT 1"; $query = mysqli_query($connect, $sql); Do you have any idea where I am going wrong? I'm relatively new to php. – Amy Mar 03 '15 at 20:03
  • Seems you _just don't send_ the POST variable from HTML form. **1.** Check if form's method is `post`, i.e.: `
    ` **2.** Check that the form contains an input with name `name`, i.e. ``
    – Alex Shesterov Mar 03 '15 at 21:34
0

Info:

It sounds like $_POST['name'] is not defined. You should verify whether or not it is and if it isn't you should have some logic to handle that case. You can use PHP isset() to check to see if $_POST['name'] "is set".

Warning:

Others may suggest using $_REQUEST, in case your variable is possibly set on $_GET as well, but you should understand how a RESTful API works before simply falling back to using $_REQUEST as there may be business logic and/or security considerations that can be affected.

Bonus:

You may also want to take a look at some PHP frameworks such as Laravel.

Resist Design
  • 4,462
  • 3
  • 22
  • 35