Mohamed Said
Mohamed Said

Reputation: 4613

Sending Variable value using ajax for php

I'm using the $.ajax call to send data to a PHP page:

$.ajax({
  type: 'POST',
  url: "ajax_more.php",
  data: "userid=1"

});

In ajax_more.php I'm trying to read the value of the userid:

$user_id=$_POST['userid'] ;

However, I'm getting an error as PHP doesn't find a value for the index userid.

What am I doing wrong?

UPDATE

I'm sending another ajax variable in the same manner:

$.ajax({
  type: "POST",
  url: "ajax_more.php",
  data: "lastmsg="+ ID, 
  cache: false,
  success: function(html){
    $("div#listednotes").append(html);
    $("#more"+ID).remove();
  }
});

and it is working fine, so using <?php print_r( $_POST ) ?>, the return value is: Array ( [lastmsg] => 38 ).

Upvotes: 2

Views: 1172

Answers (4)

You might have used some .htaccess redirection such as removing or adding the "www" to all web requests. Any change on those affects your POST request parameters.

To get this solved, assure you enter your Ajax url as it should be according to your .htaccess rules.

Upvotes: 2

YonoRan
YonoRan

Reputation: 1728

Your data should be in Key value pairs. and not the way you specified it. So:

data: "userid=1"

is wrong, should be:

data: {"something" : "value"}

Upvotes: 0

copacetic
copacetic

Reputation: 414

I cut and pasted your code exactly and it works. So it doesn't look like you are doing anything wrong in the code provided. If you are running other code before checking the $_POST array, that code could be altering its contents or unsetting it.

Upvotes: 0

jzilla
jzilla

Reputation: 1703

$.ajax({
  type: 'POST',
  url: "ajax_more.php",
  data: {"userid" :1}

});

Upvotes: 0

Related Questions