I was wondering if I can do this jquery ajax on my code:
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_add.php", "functions/ajaxNca_update.php",
data: dataString,
cache: false,
success: function(response){
// show success
alert(response.a);
}
The code above is just an example and I knew that it's not working. How can I call 2 php script in just one ajax request in jquery ? Can someone help ?
No you can not do this. If you will look at jquery ajax documentation, you will see that url accepts only string, not an array of stings.
You should either make two requests, or create another *.php
entry point which will combine both php scripts and make a call to this one.
You can't call 2 url simultaneously.but you can call one after another.
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_add.php",
data: dataString,
cache: false,
success: function(response){
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_update.php",
data: dataString,
cache: false,
success: function(response){
//responce
}
});
}
});
or
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_add.php",
data: dataString,
cache: false,
async: true,
success: function(response){
//responce
}});
$.ajax({
type: "POST",
dataType: 'json',
url: "functions/ajaxNca_update.php",
data: dataString,
cache: false,
async: true,
success: function(response){
//responce
}});
async: true - synchronizes your ajax calls
More simple way :
You can also do one ajax call to functions/ajaxNca_add.php
file, and include second file functions/ajaxNca_update.php
in first ajax file.
So from one ajax call, you can access 2 files at a time.
You can define the object first then you need to call ajax two times by changing the url.
var ajaxObj = {
type: "POST",
data: thisForm.serialize(),
success: function (response) {
if(response.status=="success"){
dataLayer.push({'event' : 'requestdemo_form_submitted', 'request_demo_submit_event_gtm' : 'request_demo_submit_event_gtm'});
$('.sidebar-req-sec').addClass('hide');
$('.sidebar-req-sec-thnk').removeClass('hide');
$('.sidebar-req-sec-thnk').addClass('show');
}
}
};
//url1 ajax
ajaxObj.url = 'url1';
jQuery.ajax(ajaxObj);
//url2 ajax
ajaxObj.url = 'url2';
jQuery.ajax(ajaxObj);