我需要根据将数据插入mysql表后的结果在ajax调用中自定义成功消息,
$.ajax({
data: dataString,
type: 'POST',
url: 'mypage.php',
success: function(data)
{
if (data === 'success') {
alert("New record created successfull");
}else{alert("something goes wrong"); }
}
});
这是php代码
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
// use exec() because no results are returned
$conn->exec($sql);
echo "success";
}
catch(PDOException $e)
{
echo "fail";
}
$conn = null;
?>
当我尝试检查从php页面返回的消息时,我通过此ajax调用获得正确的警报
$.ajax({
data: dataString,
type: 'POST',
url: 'mypage.php',
success: function(data)
{
alert(data);
}
});
我尝试了很多东西,但我找不到确切的问题,任何帮助都会受到赞赏。
答案 0 :(得分:1)
这将起作用我在另一种情况下使用相同的代码:
if ($.trim(data) === 'success') {
alert("New record created successfull");
}else{alert("something goes wrong"); }