I have a jQuery with Ajax post, one of them is working from a <select>
, the other form a <input>
. In case of both the output file is travel_data.php
.
I would like to send once the result of <select>
and <input>
together, used the php file once.
data: {f_sz1: f_sz, f_ar1: f_ar, gy_sz1: gy_sz, gy_kor1_1: gy_kor1, gy_kor2_2: gy_kor2, gy_kor3_3: gy_kor3, gy_kor4_4: gy_kor4, gy_kor5_5: gy_kor5}
Could you help me?
Thanks all of you for help in advence, Atti
$( "input" ).change(function () {
var gy_kor1 = "";
var gy_kor2 = "";
var gy_kor3 = "";
var gy_kor4 = "";
var gy_kor5 = "";
$( "input#gy1.gyk1" ).each(function() {
gy_kor1 += $( this ).val();
});
$( "input#gy2.gyk2" ).each(function() {
gy_kor2 += $( this ).val();
});
$( "input#gy3.gyk3" ).each(function() {
gy_kor3 += $( this ).val();
});
$( "input#gy4.gyk4" ).each(function() {
gy_kor4 += $( this ).val();
});
$( "input#gy5.gyk5" ).each(function() {
gy_kor5 += $( this ).val();
});
$.ajax({
type: "POST",
url: "travel_data.php",
data: {gy_kor1_1: gy_kor1, gy_kor2_2: gy_kor2, gy_kor3_3: gy_kor3, gy_kor4_4: gy_kor4, gy_kor5_5: gy_kor5},
success: function(records){
$("#eredmeny").html((records));
}
});
})
$( "select" ).change(function () {
var f_sz = "";
var gy_sz = "";
var gy_kor1 = 3;
var f_ar = "<?php echo $f_ar; ?>";
$( "#select_f option:selected" ).each(function() {
f_sz += $( this ).text();
});
$( "#select_gy option:selected" ).each(function() {
gy_sz += $( this ).text();
});
$.ajax({
type: "POST",
url: "travel_data.php",
data: {f_sz1: f_sz, f_ar1: f_ar, gy_sz1: gy_sz, gy_kor1_1: gy_kor1},
success: function(records){
$("#result").html((records));
}
});
})
.change();
What you need to do is to make sure that all your data you want to send will be stored in one object variable. You can do this by specifying a 'global' variable which can be accessed by the functions you are calling. Put (push) all the data in this variable and then execute the ajax just once. This means that you will have to adjust you functions and make sure that all the data is captured within the variable. After this is done you can call ajax. You could wrap all this code in a new function and call that function. I hope you understand what I mean.