Undefined index when submitting HTML form via POST method

17,349

Solution 1

You can try it with :

replace

<button type="submit" name="send" class="btn">Sign in</button>

with

<input type="submit" name="send" value="Sign in" />

and

   if(isset($_POST['send']))

    {

    if (isset($_POST['achternaam'])) 
    {
       $anaam = $_POST['achternaam'];
       // do whatever here
    }

    if(isset($_POST['voornaam']))
    {
    $vnaam =  $_POST['voornaam'];

    }

    }

    $anaam = mysql_real_escape_string($anaam);
    $vnaam = mysql_real_escape_string($vnaam);
    [...]

with

if(!isset($_POST['send'])) { 
    echo " No valid Post ";
} 
 else
{

if (isset($_POST['achternaam'])) 
{
   $anaam = $_POST['achternaam'];
   // do whatever here
}

if(isset($_POST['voornaam']))
{
$vnaam =  $_POST['voornaam'];

}

    $anaam = mysql_real_escape_string($anaam);
    $vnaam = mysql_real_escape_string($vnaam);
    [...]
}

clickable buttons

<button type="submit" name="send" class="btn">Sign in</button>

To specify what should happen when the button is clicked, you can use for example the side onclick event handler. The value assigned to the event handler attribute, you can then write JavaScript code.

In other respects, these buttons for the same thing has been said for their conventional counterparts: Without JavaScript they are completely functionless.

Solution 2

Edit:

You need to give your button a value or isset will return false.


It's unclear if you're still having the undefined index error, but if you've made it past that try declaring your variables outside the if statements.

$anaam = '';
$vnaam = '';

if(isset($_POST['send']))    
{

    if (isset($_POST['achternaam'])) 
    {
       $anaam = $_POST['achternaam'];
       // do whatever here
    }

    if(isset($_POST['voornaam']))
    {
        $vnaam =  $_POST['voornaam'];    
    }
}

Solution 3

You could debug by outputting the $_POST variable with var_dump() to see if there is anything at all.

If there is nothing, are you sure you are not redirecting the browser to the page where you try to look up for the values?

If there are something set, then perhaps the isset($_POST["send"]) returns false.

And please use PDO instead of those soon-to-be-deprecated and bad mysql_ functions.

Solution 4

Solved the issue of the undefined index by adjusting the code as seen in the update I wrote in my original post. After that problem was solved the query refused to fire and put the data in the database because of a DUPLICATE of the PRIMARY KEY. After solving this, it worked like a charm. Is this info sufficient enough to help people facing some of the same issues or should I be more thorough?

Anyway, many thanks to all responders!

Share:
17,349
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have been searching days why this piece of code is not working. Adjusted the code multiple times by searching for posts with the same problem.

    Problem: my php file doesn't seem to get the values it needs from a simple HTML form (made w. bootstrap) via POST-method. I use the issit function to check if the submit button has been clicked and thereafter I use to check if the form was filled out completely.

    Please note that I am relatively new to php. I hope I did miss something and it is not just a simple typo.

    PHP code (bootstrap/php/boeking.php)

    <?php
    
    //<!--debug-->
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    
    //<!--connectie-->
    
    mysql_connect("localhost","root","root") or die(mysql_error());
    mysql_select_db("hota") or die(mysql_error());
    
    
    //<!--boeking info verzenden-->
    
    if(isset($_POST['send']))
    
    {
    
    if (isset($_POST['achternaam'])) 
    {
       $anaam = $_POST['achternaam'];
       // do whatever here
    }
    
    if(isset($_POST['voornaam']))
    {
    $vnaam =  $_POST['voornaam'];
    
    }
    
    }
    
    
    
    $anaam = mysql_real_escape_string($anaam);
    $vnaam = mysql_real_escape_string($vnaam);
    
    
    $boek = "INSERT INTO klant (achternaam , voornaam) VALUES ('{$anaam}', '{$vnaam}')";
    
    mysql_query($boek);
    
    ?>
    

    HTML code:

    <form id="newbook" method="POST" action="bootstrap/php/boeking.php" class="form-horizontal">
      <legend>Nieuwe boeking</legend>
       <div class="control-group">
          <label class="control-label" for="voornaam">Voornaam</label>
          <div class="controls">
            <input type="text" name="voornaam" id="voornaam" placeholder="Voornaam">
          </div>
       </div>
       <div class="control-group">
          <label class="control-label" for="achternaam">Achternaam</label>
          <div class="controls">
            <input type="text" name="achternaam" id="achternaam" placeholder="Achternaam">
          </div>
        </div>
       <div class="control-group">
          <div class="controls">
            <button type="submit" name="send" class="btn">Sign in</button>
          </div>
       </div>
      </form>
    

    Thanks in advance!

    UPDATE:

    changed my php-code.

    Not getting the undefined index error anymore.
    Var_dump($vnaam); and var_dump($anaam); do indicate that strings are passed to the php but after that, when the query is fired, things don't seem to get send to the db.

    Might this be a problem in my query ?

    (already thanks for the tips: gave the button a value and used var_dump)

    This is my new code :

    <?php
    
    //<!--debug-->
    
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    
    
    //<!--connectie-->
    
    mysql_connect("localhost","root","root") or die(mysql_error());
    mysql_select_db("hota") or die(mysql_error());
    
    
    $anaam = '';
    $vnaam = '';
    
    
    if(!isset($_POST['send'])) { 
    
    } 
     else
    {
    
    if (isset($_POST['achternaam'])) 
    {
       $anaam = $_POST['achternaam'];
       // do whatever here
    
    }
    
    if(isset($_POST['voornaam']))
    {
    $vnaam =  $_POST['voornaam'];
    
    
    }
    var_dump($vnaam);
    var_dump($anaam);
    
    
        $anaam = mysql_real_escape_string($anaam);
        $vnaam = mysql_real_escape_string($vnaam);
    
    }
    
    var_dump($vnaam);
    
    
    $boek = "INSERT INTO klant (achternaam , voornaam) VALUES ('{$anaam}', '{$vnaam}')";
    
    mysql_query($boek);
    
    ?>