Donny123
Donny123

Reputation: 105

Ajax not sending request

I have been struggling on this simple problem for pretty long already, yes I tried to google for answer though with no luck, the problem is that my ajax request doesn't even send a thing, so I guess it has something to do with the syntax though it looks perfectly fine to me:

$(window).on('load',function()
{
    $("#unfriend_user > p").click(function()
    {
        $.ajax(
        {
            url: "php_includes/remove_friend.php", 
            type: "POST",
            data: 
            {
                user_1 : <?php echo $logged_id ?> ,
                user_2 : <?php echo $page_user_id ?>,
                user_username : <?php echo $logged_username ?>,
                user_password : <?php echo $logged_password ?>
            },
            success: function(data)
            {
                alert(data);
            }
        });
    });
});

All php echoed variables have values.

The problem came up after I added these two lines:

user_username : <?php echo $logged_username ?>,
user_password : <?php echo $logged_password ?>

If I remove these two lines and "," everything works just fine.

Upvotes: 0

Views: 78

Answers (2)

thecotne
thecotne

Reputation: 478

probably you are braking javascript syntax since username and password are strings you need to quote them

best way will be to use json_encode

json_encode will put quote when needed

$(window).on('load',function()
{
    $("#unfriend_user > p").click(function()
    {
        $.ajax(
        {
            url: "php_includes/remove_friend.php", 
            type: "POST",
            data: 
            {
                user_1 : <?php echo json_encode($logged_id) ?> ,
                user_2 : <?php echo json_encode($page_user_id) ?>,
                user_username : <?php echo json_encode($logged_username) ?>,
                user_password : <?php echo json_encode($logged_password) ?>
            },
            success: function(data)
            {
                alert(data);
            }
        });
    });
});

Upvotes: 1

Joe Enos
Joe Enos

Reputation: 40431

If you look at your rendered HTML ("View Source" in the browser), you'll see that your javascript looks something like:

data: 
    {
        user_1 : 1 ,
        user_2 : 2,
        user_username : someusername,
        user_password : somepassword
   },

Since those are strings (I'm assuming), you need to quote them:

data: 
    {
        user_1 : <?php echo $logged_id ?> ,
        user_2 : <?php echo $page_user_id ?>,
        user_username : "<?php echo $logged_username ?>",
        user_password : "<?php echo $logged_password ?>"
    },

Upvotes: 3

Related Questions