2

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);

?>
7
  • Is your PHP script definitely being reached when the form is submit? Commented Apr 23, 2013 at 1:01
  • Which line is the error happening on, and what is the undefined index in the error message? Commented Apr 23, 2013 at 1:04
  • @elclanrs Even using mysql_real_escape_string? Commented Apr 23, 2013 at 1:07
  • I noticed that, I think that must be the most unfortunate and confusing function name. lol. Is it real or not real? Commented Apr 23, 2013 at 1:09
  • @elclanrs There's another function mysql_escape_string that's slightly different. mysql_real_escape_string is the one you should really use. Commented Apr 23, 2013 at 1:17

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Just tried using var_dump(), put it just behind the post-methods for the form contents and it DOES return the strings. So this means the form contents are in fact reaching the php via the post method, am I right?
Also, when hitting the submit button, I go to a blank page with link: localhost:8888/bootstrap/php/boeking.php. Only thing on it are the var_dump() strings, so no returning to the original form what so ever.
1

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'];    
    }
}

Comments

1

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!

Comments

0

Try this....

if($_SERVER['REQUEST_METHOD'] == 'POST'){

   $voornaam = $_POST['voornaam'];
   $achternaam = $_POST['achternaam'];

   // Validate post input here

   // Please use PDO or MySQLi at least. It's easier than it looks...
   try {
       $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass, array(PDO::ATTR_PERSISTENT => true));
       $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   } catch(Exception $e) {
       echo 'Error connecting to the database.';
   }

     $stmt = $conn->prepare('INSERT INTO klant (achternaam, voornaam) VALUES (:achternaam, :voornaam)');
     $stmt->bindValue(':achternaam', $achternaam);
     $stmt->bindValue(':voornaam', $voornaam);
     $stmt->execute();
}

6 Comments

It's not really a good practice to allow ANY post to go inside of the if statement, and assuming that voornam and achternaam are set.
What is the problem putting them inside the if? I suppose this user is a beginner, but I would also use javascript validation to determine if they are set, as well as php validation on the post variables to sanitize them. I am not here to write a book though...
He should be doing isset($_POST['send']), that is, the identification of the submit button.
The button fails when users submit the form via the enter key in some browsers and certain javascript submission methods. I also do not see the problem if you are validating the fields properly. This also has nothing to do with your first comment, you misdirected. To get back on topic, why can I not use post data inside the if statement?
Just cause a post was made doesn't mean that voornaam and achternaam were posted. Neither could have been posted or just one of them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.