dqwh1202 2018-07-25 16:30
浏览 55

在AJAX调用中显示来自PHP的消息

I have a favourites button calling an ajax request to update a MySQL database.

I would like to have a alert if there are duplicate additions or too many additions.

Can anybody see a way that I could show an alert if there is a duplicate addition? My code is below:

AJAX REQUEST

$.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;}
  • 写回答

2条回答 默认 最新

  • dongshi3605 2018-07-25 16:32
    关注

    Just return the text to alert to your 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 request:

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

    Additionally, you should consider using JSON, to pass back entire objects to your javascript, and parse it there:

    $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
       ]);
    }
    

    And in your ajax, make sure you set dataType: 'json', which will automatically parse it into an object:

    $.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);
            }
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路