在ajax调用中显示来自php的消息

zujrkrfu  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(398)

我有一个favorites按钮调用ajax请求来更新mysql数据库。
我想有一个警报,如果有重复的补充或太多的补充。
有人能想到一种方法,如果有重复的加法,我可以显示一个警报吗?我的代码如下:
ajax请求

$.ajax({
    type: 'post',
    url: 'favaddDB.php',
    data: $('#addfaveform').serialize(),
    success: function () {
      alert('Added To Favourites');
    }
});

PHP

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1="SELECT * FROM `$email` ";
$stat1=$db->prepare($query1);
$stat1->execute();// IMPORTANT add PDO variables here to make safe

//Check if fave adds >9 
$count = $stat1->rowCount();
$fave=$count;
if ($fave>9) {die(); exit();} // HERE I WISH TO RUN AN ALERT OR SEND BACK A MESSAGE TO DISPLAY
else {$fave=$fave+1;}
qnakjoqk

qnakjoqk1#

只需返回文本以提醒您的javascript:

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1="Query here ($email/similar should NOT BE HERE! Add them via execute/prepare.";
$stat1=$db->prepare($query1);
$stat1->execute();// IMPORTANT add PDO variables here to make safe

//Check if fave adds >9 
$count = $stat1->rowCount();
$fave=$count;
if ($fave>9) {die("Here is a message");} // HERE I WISH TO RUN AN ALERT OR SEND BACK A MESSAGE TO DISPLAY
else {$fave=$fave+1; die("Here is another message"); }

ajax请求:

$.ajax({
  type: 'post',
  url: 'favaddDB.php',
  data: $('#addfaveform').serialize(),
  success: function (message) {
    alert(message);
  }
});

此外,您应该考虑使用json,将整个对象传递回javascript,并在那里进行解析:

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1 = "Query here ($email/similar should NOT BE HERE! Add them via execute/prepare.";
$stat1 = $db->prepare($query1);
$result = $stat1->execute();// IMPORTANT add PDO variables here to make safe

// Tell javascript we're giving json.
header('Content-Type: application/json');

if (!$result) {
    echo json_encode(['error' => true, 'message' => 'A database error has occurred. Please try again later']);
    exit;
}

//Check if fave adds >9 
$count = $stat1->rowCount();
$fave = $count;
if ($fave > 9) {
    echo json_encode(['error' => false, 'fave' => $fave, 'message' => 'Fave > 9!']);
} // HERE I WISH TO RUN AN ALERT OR SEND BACK A MESSAGE TO DISPLAY
else {
   $fave = $fave+1;
   echo json_encode([
       'error' => false,
       'fave' => $fave,
       'message' => 'Current fave count: ' . $fave
   ]);
}

在ajax中,确保 dataType: 'json' ,将自动将其解析为对象:

$.ajax({
    type: 'post',
    url: 'favaddDB.php',
    data: $('#addfaveform').serialize(),
    dataType: 'JSON',
    success: function (res) {
        if (res.error) {
            //Display an alert or edit a div with an error message
            alert(res.message);
        } else {
            //Maybe update a div with the fave count
            document.getElementById('#favcount').value = res.fave;
            alert(res.message);
        }
    }
});
n3schb8v

n3schb8v2#

在大多数情况下,简单更好。通过返回 messages ,您可以在前端执行任何操作,具体取决于消息。
PHP:

<?php

$db = new PDO("mysql:host=localhost;dbname=favourites", 'root', '');                                                                  
$query1 = "SELECT * FROM " . $email;
$stat1 = $db->prepare($query1);
$stat1->execute();

$count = $stat1->rowCount();
$fave = $count;

if ($fave > 9) {
  echo "tooMany"; exit();
} else {
  echo "addedFav"; $fave++;
}

js公司:

jQuery.post({
  url: 'favaddDB.php',
  data: jQuery('#addfaveform').serialize()
}).then(function (code) {
  switch (code) {
    case "addedFav":
      alert('Added To Favourites');
      break;
    case "tooMany":
      alert('Too many favourites');
      break;
  }
}).catch(function (error) {
  console.log(error);
});

相关问题