Jump to content

jquery ajax reaches destination page but $_POST is empty


wilsea
 Share

Recommended Posts

I'm hoping that someone can help me out with an ajax problem. I'm trying to build a front-end signup form, using jquery.validate for client-side validation, and then sending the form details via a jquery  ajax post.  The $.ajax submission reaches the target page OK, but the data that should have been sent with the post seems to get lost along the way. $_POST is always empty.

The submitHandler for jquery.validate has the following code;

$.ajax({
      type: "POST",
      url: "/processMaths/admin/adduser",                
      data: "name=fred&age=27", // data simplified for testing - I've also used {name:"fred",age:27}
      success: function(rtndata) {                    
            $('#debug').html(rtndata);
      }
 }); 

 The page handling the ajax request has the following code added to try to work out what is going wrong.

print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<BR />";
$data = file_get_contents('php://input');
print "DATA: <pre>";
var_dump($data);
var_dump($_POST);
print "</pre>";
echo "here at adding users<br/>";

 which updates my  #debug div  with the following message which suggests that no data reached the page.

CONTENT_TYPE: 
DATA:string(0) ""
array(0) {
}
here at adding users
 

Does anyone have any ideas why the $_POST is empty. The same code outside of PW works OK, and I've previously used ajax inside PW without any problems. I'm stumped on this one - even tried rebooting in desperation, and I've run out of ideas for what else to try. There's obviously something different between my other working ajax calls (including other processwire sites) and this one, but I can't spot the difference. All use apache running on localhost, same version of jquery.

The master plan is to set up a mathhelp site using processwire, where teachers can signup (and be assigned a role as "teacher"), and join up their students  (role "student" ) - teachers get to set tasks for students to do, and monitor their results, so teachers will have access to more pages than the students (and ideally neither student or teacher will ever need to see the back-end admin pages ). Students, classes and teachers will all be pages.

Is there an alternative front-end way of collecting user data that I should know? A more elegant way of using ajax perhaps?  I've done so much head scratching my head is getting sore. 

Link to comment
Share on other sites

Hi wilsea,

Try adding a trailing slash to the url

/processMaths/admin/adduser/

Pw does a redirect if there is no slash, I guess that's why you loose your $_POST data.

  • Like 3
Link to comment
Share on other sites

Thanks a million Wanze - the trailing slash did the trick.  :)  Incredible to have a forum on hand that comes up with a solution in a fraction of the time I've spent on it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...