Ajax datapost并没有发布到发布的url

时间:2017-08-25 10:44:01

标签: javascript php jquery ajax post

我试图将数据发布到一个php站点,该站点只包含在第一页上点击ID#mR-RateableFramePicture时应该执行的php代码。这是由ajax请求完成的:

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

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

这里是ajax发布的网址:

<?php
// Start the session (enable global $_SESSION variable).
session_start();

// Include database-link ($conn).
include '../../scriptMysqli.php';

// Make global variable to simple variable.
$userID = $_SESSION["ID"];

//Save the rateable style to one owns libary of saved styles.    
$ratedUserID = $_POST['rateableUserID'];
$ratedPictureID = $_POST['rateablePictureID'];


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

?>

我收到以下错误消息:

注意:未定义的索引:第12行的C:\ xampp \ htdocs \ mystyle \ app \ moduleRateable \ scriptSavedStyle.php中的rateableUserID

注意:第13行的C:\ xampp \ htdocs \ mystyle \ app \ moduleRateable \ scriptSavedStyle.php中的未定义索引:rateablePictureID

1 个答案:

答案 0 :(得分:1)

您没有在$.ajax({})调用中传递变量rateableUserID和rateablePictureID的值,如下所示 -

data: { rateableUserID: rateableUserID, rateablePictureID: rateablePictureID}。除非它们是全局定义的,否则您将在PHP端获得未定义的值。在进行调用之前,请确保将值分配给rateableUserID和rateablePictureID。但是,您仍然必须检查是否实际在您的发布请求中传递该变量,因为PHP无法找到密钥名称。

该功能应如下所示

$('#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() {
                $('#DynamicContent').load('moduleRateable/scriptSavedStyle.php');
            }
        });
    });