jQuery ajax不传输所有数据对象

时间:2017-03-07 10:15:54

标签: php jquery ajax

目前我遇到了AJAX-Request的问题。但是我想我只是错过了一些东西,我会给出一些掌控时刻。

所以无论如何:我试图用json-element和两个数据对象执行一个AJAX-Request。请求代码对我来说似乎没问题。并且还可以执行请求。但每次我尝试,第二个数据都没有传输。所以在第一次PHP查询时,一切都会失败。而且正如我可能会看到的那样,警报随即出现"参数不完整"。

任何建议?

这就是我的Ajax-Request

function callmedaddy()
{
    var value_id = document.getElementById('user_id_div').value;
    var new_playlist_name = document.getElementById('new_playlist_input').value;

    $.ajax({
        url: "/ajax/ajax.php",
        type: 'POST',
        dataType: 'json',
        data: 
        {
           "playlist_name" : new_playlist_name, "username" : value_id
        },
        success: function(data) 
        {
           alert(data[0]);
        }
     });
}

那就是我的PHP-File(ajax.php)部分,它失败了。

/* There might be some Typos in here, but they shouldnt influence the problem I am facing here, i guess .. */
<?php include '../inc/db_connect.php';

/* Header Informationen */
header('Content-Type: application/json');

$arrayPlaylistState = array();

if(isset($_POST['playlist_name']) && isset($_POST['username']))
{
    $playlist_name = $_POST['playlist_name'];
    $user_name = $_POST['user_id'];

    $select_playlist_check = query("SELECT * FROM playlist WHERE name='$playlist_name';");

    /* Keine Redundante Playlist-Namen (Check) */
    if($select_playlist_check->num_rows==0)
    {
        /* Datenbank nach überliefertem Benutzer nachfragen */
        $select_user_id = query("SELECT iduser FROM user WHERE username='$user_name';");

        /* Falls der Benutzer besteht */
        if($select_user_id->num_rows!=0)
        {
            $fetch_user_id = $select_user_id->fetch_assoc();
            $user_id = $fetch_user_id['iduser'];

            query("INSERT INTO playlist (name) VALUES ('$playlist_name');");

            $select_new_playlist_id = query("SELECT idplaylist FROM playlist WHERE name='$playlist_name'");
            $fetch_new_playlist_id = $select_new_playlist_id->fetch_assoc();
            $playlist_id = $fetch_new_playlist_id['idplaylist'];

            query("INSERT INTO user_playlist (user_id, playlist_id) VALUES ('$user_id', '$playlist_id');");
        }
        else
        {
            $arrayPlaylistState[0] = 'HTTP-Header manipulation';
        }
    }
    else
    {
        $arrayPlaylistState[0] = 'Playlist Name bereits vergeben!';
    }

}
else
{
    $arrayPlaylistState[0] = print_r($_POST);
}

echo json_encode($arrayPlaylistState);
?>

1 个答案:

答案 0 :(得分:0)

您似乎正在使用未定义的变量:您通过Ajax传递'username',但您在if语句中使用'user_id'。 (正如@CBroe所说)