I have a very simply registration form. The form is sent with jQuery/Ajax to a PHP page for processing. I use this identical code everywhere throughout my app but for some reason, on this one page, it's just not working and I'm trying to figure out where the problem lies. There's nothing special on this page and it's on the same domain so I'm at a loss here...
When the form is submitted, the following jQuery function processes the form:
var dataString = $(event.target).serialize();
var url = '/post/register.php';
console.log( dataString ); // OUTPUTS firstname=Andrew&email=me@email.com
$.ajax({
type: "POST",
url: url,
data: dataString,
dataType: "text",
error: function(jqXHR,textStatus,errorThrown) {
// ERROR
},
success: handleRegistration
});
The console shows that the data from the form is properly serialized.
On the PHP page (register.php), I simply have the following code:
$newArr[]= array('email'=>$_POST['email'], 'firstname'=>$_POST['firstname']);
ob_start();
print_r($newArr);
$c=ob_get_contents();
ob_end_clean();
$fp=fopen('test.txt', "wb");
fwrite($fp, $c);
fclose($fp);
This just writes the POST variables to a TXT file called "test.txt". The text file shows the variables are empty:
Array
(
[0] => Array
(
[firstname] =>
[email] =>
)
)
I can't for the life of my figure out why it's not receiving the POST vars.
Anyone?
* ANSWER * I changed the url above for demo purposes but in my app the url was:
var url = '/post/register';
and there was an "index.php" file inside the register folder.
The form was getting sent to the PHP page BUT because I was missing the trailing '/', the querystring was not getting sent correctly. So the answer to my problem was to simply change the url to:
var url = '/post/register/';
What a dumb bug on my part - that took way too long to figure out :S