0

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);
?>
Oliver
  • 151
  • 10
  • Your ajax call seems good. Are you sure the variables new_playlist_name and value_id are defined ? – Guillaume Sainthillier Mar 07 '17 at 10:19
  • Hope so, added the declaration of the variables to the code. But they are both definied (valued) @GuillaumeSTLR – Oliver Mar 07 '17 at 10:25
  • I don't see any mistakes. Can you post the whole ajax.php file content ? – Guillaume Sainthillier Mar 07 '17 at 10:27
  • Do a `print_r($_POST)` to see what's in there. There's a chance that one of the two fields is null (which counts as not being set) – apokryfos Mar 07 '17 at 10:30
  • When I output $_POST['playlist_name'] everything works fine. But if I am trying to output $_POST['username'] there is just nothing. Same with print_r($_POST). Strange! Code updatet @GuillaumeSTLR – Oliver Mar 07 '17 at 10:35
  • It may be worth adding `debugger;` (if on Chrome) or any kind of breakpoint, in the line right before the ajax call to inspect the values of those variables. – apokryfos Mar 07 '17 at 10:39
  • `$user_name = $_POST['user_id']` – well that definitively doesn’t seem to match what you send … – CBroe Mar 07 '17 at 10:41

1 Answers1

0

It seems you're using an undefined variable: you pass 'username' via Ajax, but you are using 'user_id' within the if-statement. (As @CBroe mentioned)

Tobias F.
  • 1,050
  • 1
  • 11
  • 23
  • You are actually right. But it didn't even reach the point where this statement is placed, bcs the first query always failed. But the 2nd. variable is undefined in the ajax-request, that's why it wasn't sent, bcs there was nothing to send. Eventhough the element had the value, the statement `document.getElementById('user_id_div').value` couldn't transmit it to the variable. Now I placed the value innerHTML and don't display it. And it works. – Oliver Mar 07 '17 at 10:50
  • Oh ok, then i understood you wrong, so you don't even get into the first if-statement? @OliverBähler – Tobias F. Mar 07 '17 at 10:52
  • _“the statement document.getElementById('user_id_div').value couldn't transmit it to the variable”_ – might that be because, as the id implies, this is actually a `div` element? Those don’t have a value. You would need to select the actual input field. – CBroe Mar 07 '17 at 11:14
  • Actually I wasn't sure about that. Never really used the value-Tag except in forms. Nice to know! Thank you for your help. @CBroe – Oliver Mar 07 '17 at 11:44