没有处理由ajax发布的php查询

时间:2017-08-25 17:53:21

标签: javascript php jquery ajax

我有两页:

第1页:

<input type="hidden" name="rateableUserID" value="<?php echo $rateableUserID;?>"/>
<input type="hidden" name="rateablePictureID" value="<?php echo $rateablePictureID;?>"/>

<script>
var rateableUserID = $('input[name="rateableUserID"]').val();
var rateablePictureID = $('input[name="rateablePictureID"]').val();

$('#mR-RateableFramePicture').dblclick(function () {
    $.ajax({
        type: "POST",
        url: 'moduleRateable/scriptSavedStyle.php',
        data: {"rateableUserID": rateableUserID, "rateablePictureID": rateablePictureID},
        success: function() {
        }
    });
});
</script>

第2页:

<?php
session_start();

$userID = $_SESSION["ID"];

$ratedUserID = $_POST['rateableUserID'];
$ratedPictureID = $_POST['rateablePictureID'];

include '../../scriptMysqli.php';

$sql = $conn->query("UPDATE styles SET savedByUser = '$userID' WHERE userID = '$ratedUserID' AND pictureID = '$ratedPictureID'");

?>

<script>alert("success");</script>

但是$ sql变量永远不会被执行,并且带有警报的部分没有显示在原始页面上(第1页)eiher:/

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

从评论中改变了代码:

<input type="hidden" name="rateableUserID" value="<?php echo $rateableUserID;?>"/>
<input type="hidden" name="rateablePictureID" value="<?php echo $rateablePictureID;?>"/>

<script>    
$('#mR-RateableFramePicture').dblclick(function () {
    var rateableUserID = $('input[name="rateableUserID"]').val();
    var rateablePictureID = $('input[name="rateablePictureID"]').val();

    $.ajax({
        type: "POST",
        url: 'moduleRateable/scriptSavedStyle.php',
        data: {"rateableUserID": rateableUserID, "rateablePictureID": rateablePictureID},
        success: function(scriptCode) { $('body').append(scriptCode); }
    });
});
</script>

重要的是成功处理程序。它接受你的ajax调用的响应(由你的php脚本回应)并将其添加到DOM,因此它将在javascript代码的情况下执行。