Jump to content

[SOLVED] jQuery AJAX wrong message


9three

Recommended Posts

Hey,

 

I have a simple form that inserts a name into a database. The functionality works fine. But the message that is suppose to show is wrong.

 

$(document).ready(function () {
  $('#nameForm').submit(function () {
    $('.msgbox').hide();
    $.post('ajax.Register.php', {name: $('#name').val()}, function (data) {
      if (data) {
        $('.msgbox').show('fast', function () {
          $(this).html('OK!');
        });
      }
      else {
        $('.msgbox').show('fast', function () {
          $(this).html('Failed!');
        });
      }
    });
    return false;
  });
});

 

The form always shows "Failed!" even though the name went through in my database.

 

try {
  $objUser = new PDO("mysql:dbname=ajax;host=localhost", "root", "");
}
catch (PDOException $e) {
  echo 'Unable to connect';
}

$strName = $_POST['name'];
if (empty($strName))
  return false;

$strQuery = "INSERT INTO name (name) VALUES (:name)";
$objStatement = $objUser->prepare($strQuery);
$objStatement->bindParam(':name', $strName, PDO::PARAM_STR);
if ($objStatement->execute())
  return true;
return false;

 

Anyone lend a hand?

Link to comment
https://forums.phpfreaks.com/topic/174830-solved-jquery-ajax-wrong-message/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.