AJAX-序列化表格

I'm trying to learn how to make a registration form. I was getting an error message: "PDOException : SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'firstname' cannot be null"

Someone told me I could fix it with AJAX, which is something I want to learn anyway. But I'm not sure what I'm doing.

First, I took the PHP code out of my main page (register.php) and put it in a new file (reg-code.php). Then I included reg-code.php in register.php.

Next, I put the following in the head section of register.php:

<script>
submitHandler: function(form) {
  $.post(
    '/test/register/reg-code.php', 
    $(form).serialize(),
    success: function() {
        // display success message or something
        It works!
    }
  );
};
</script>

I fixed one syntax error, but I get another one on the line success: function() {

But I'm not even sure if I'm moving in the right direction. The tutorials are confusing.

This is the PHP code I put in a separate file:

try {
$sql = "INSERT INTO members (firstname, lastname, username, password,  password_confirm, email, age) VALUES (:firstname, :lastname, :username,  :password, :password_confirm, :email, :age)";
$query = $pdo->prepare($sql);
$query->bindParam(':firstname', $_GET['firstname'], PDO::PARAM_STR);
$query->bindParam(':lastname', $_GET['lastname'], PDO::PARAM_STR);
$query->bindParam(':username', $_GET['username'], PDO::PARAM_STR);
$query->bindParam(':password', $_GET['password'], PDO::PARAM_STR);
$query->bindParam(':password_confirm', $_GET['password_confirm'],  PDO::PARAM_STR);
$query->bindParam(':email', $_GET['email'], PDO::PARAM_STR);
$query->bindParam(':age', $_GET['age'], PDO::PARAM_STR);

 $query->execute();
} catch (PDOException $e) {
 echo 'PDOException : '.  $e->getMessage();
}

Do I just have to figure out a syntax error, or do I need to go back to square one?

That error message means that the firstname variable is not being passed in properly. Make sure the name/id of your form field is indeed "firstname".

actually there is no problem with your procedure but

  1. In your members table firstname column is not null and you are passing null value to it

If you use jQuery $.post - then in your php-script you should use $_POST variables:

$query->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);
// etc

Also:

success: function() {
    // display success message or something
    It works!  // this string will cause syntax error
}

Use standard alert() function:

success: function() {
    // display success message or something
    alert('It works!');
}

First of all, Ajax has nothing to do with that error you get! So you might wan't to consider changing your title. But anyway.

This means that your $_GET['firstname'] doesn't have a value, which means that no input from your registration form is send trough to your code. And my suggestion would be that you change all you $_GET varibles to $_POST['inputfieldname'].

Because if you are using a form to send the data, you can't access them trough GET, as GET is used to acces data sent via the URL, so let's say you sent something with the url, and the url was www.yoururl.com/sent.php?something=bla You would get the value from "something" like so $_GET['something'] and that would now have the data "bla", just to clarify.

The reason it says that the column cannot be null, is because that you have set that rule in your database, and if you removed that, i would just be a blank field.

Hope it helps a bit.