Reputation: 629
The AJAX-PHP bug has bit me again.Now here's the problem
I am to send some form data from HTML to PHP using AJAX,(I'm using jQuery for this) and then use that data, play with it in PHP and give some result.
The Ajax call is made successfully, but the issue is that not all data is being sent to PHP that is some data goes missing if I interpreted it correctly
The jQuery/Ajax Code
$(document).ready(function(){
$("#button").click(function(e){
e.preventDefault();
var data=$("#Form").serialize();
console.log(data);
$.ajax({
type:'POST',
url:('php/api.php'),
data:"datastring="+data,
success: function(d){
console.log("php response "+d);
}
});
});
});
And the PHP
<?php
$data=$_POST['datastring'];
echo($data);
?>
Now here's the output from the console
first+name=first&last+name=second&some+detail=third&comments=fourth //output from 1st console.log() statement
php response first name=first //output from php
As you can see from the above statement only the first value is echoed why? Does it mean it did not recieve the full value from AJAX?
Thanks
Upvotes: 0
Views: 138
Reputation: 6629
Why are you assigning it to datastring?
Just add the datastring without the predecessor to it.
$.ajax({
type:'POST',
url:'php/api.php',
data:data,
success: function(d){
console.log("php response "+d);
}
});
Then in your php:
<?php print_r($_POST); ?>
Edit: fixed the php side. Thanks! unfixed it though!
Upvotes: 2
Reputation: 74738
I suggest you, don't put any ()
in the url
:
$.ajax({
type:'POST',
url:'php/api.php',
data:{data : $("#Form").serialize()}, //<---You can put the directly here
success: function(d){
console.log("php response "+d);
}
});
and in php
file again remove the ()
from echo($data);
in use $_POST['data'];
:
<?php
$data=$_POST['data'];
echo $data;
?>
Try this and see if helps.
Upvotes: 0