2

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 ).

6
  • 1
    Hmm, strange. Can't see anything wrong according to api.jquery.com/jQuery.ajax Commented Jul 1, 2011 at 20:27
  • Can you post the output of print_r( $_POST )? Commented Jul 1, 2011 at 20:28
  • try printing out the $_POST or $_REQUEST variable to see what's in there. (E.g. print_r($_POST)) Commented Jul 1, 2011 at 20:29
  • I don't know what's wrong with the code, that's weird. Commented Jul 1, 2011 at 20:46
  • 1
    use firebug ... it will let u know whether its wrong with jquery-ajax script or server-side script. I have tested ur code its working fine. Commented Aug 28, 2011 at 11:52

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

});

1 Comment

Try this: '$.post("ajax_more.php", {userid: "1"});' instead of what you have... I tested this and it works.
0

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.

1 Comment

Actually the $_POST array is the first line in ajax_more.php
0

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"}

2 Comments

I'm still getting this error: Undefined index: userid in C:\xampp\htdocs\penpaper\ajax_more.php on line 8
Basically is should look like this:' $.ajax({ type: 'POST', url: "ajax_more.php", data: {userid: "1"} }); I would try it with quotation marks around the userid and without, one of them should work. '

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.