At the moment I am facing a problem with an AJAX-Request. But I guess I am just missing something, what will me give the face palm moment.
So anyway: Im Trying to execute a AJAX-Request with a json-element and two data-objects. The Code for the Request seems ok to me. And also the request can be executed. But everytime I try, the second data isn't transmitted. So everything always fails at the first PHP-query. And as i might see there is always the alert coming up with "Parameter incomplete".
Any Suggetions?
So that is my 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]);
}
});
}
And thats the section of my PHP-File (ajax.php), where it fails.
/* 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);
?>