Reputation: 595
hello I would like to post multiple data to a php file with jQuery ajax but when I execute the function it's retuning nothing and the php also doesn't get the data
my function looks like this:
function sendForm(){
jQuery.ajax({
url: <?php echo $path; //this the url where the php file is ?>,
type: 'POST',
data: {
addressOne: jQuery('#Form #table .key :input').serialize(),
addressTwo: jQuery('#Form #table_2 .key :input').serialize(),
additionalData: jQuery('#Form #More :input').serialize(),
preloaded: <?php echo serialize($array); ?>,
action: 'sendIt'
},
async: false,
cache: false,
success: function(data){
alert(data); //or console.log(data);
}
});
}
and in the php I do something like this:
<?php
function handleData() {
parse_str($_POST['addressOne'], $arrayOne);
parse_str($_POST['addressTwo'], $arrayTwo);
parse_str($_POST['additionalData'], $arrayThree);
$preloaded = unserialise($_POST['preloaded']);
//than do some stuf here for example print_r all...
}
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'sendIt' : handleData();
break;
//etc...
}
}
?>
I don't know what I'm doing wrong? Is there a better way to post multiple data?
If I use only one data like I'm serialising the whole form and do not post the serialized php array than it works fine, but I would like to use this four separate data.
Upvotes: 1
Views: 392
Reputation: 7773
You mistyped your ajax query url
with ulr
Consider using a plugin for your browser like Web Developer, I'm pretty sure it would have picked up the error and you wouldn't have needed to ask here.
Edit: if you are still having troubles, validate the data you are going to send with some alert
, validate that your php
script does what you want it to do by navigating to it manually from the browser, since you provide a success
callback, any reason you're not async
, etc... validate everything
Upvotes: 2
Reputation: 5239
You got a typo in the ajax function,
url: <?php echo $path; ?>, //needs to be url, not ulr
You also don't need to serialize
the values from the text fields. Just do this,
jQuery.ajax({
url: <?php echo $path; //this the url where the php file is ?>,
type: 'POST',
data: {
addressOne: $('#Form #table .key :input').val(),
addressTwo: $('#Form #table_2 .key :input').val(),
additionalData: $('#Form #More :input').val(),
preloaded: <?php echo serialize($array); ?>,
action: 'sendIt'
},
async: false,
cache: false,
success: function(data){
alert(data); //or console.log(data);
}
});
Upvotes: 1