[英]How to call multiple functions using AJAX
In this code I am using AJAX. 在这段代码中,我正在使用AJAX。 I want to append the values in a td
. 我想将这些值附加到td
。 I already append the values for one column and after that I want to append the values of second td
. 我已经添加了第一列的值,之后我想添加第二个td
的值。 I don't know how to do this. 我不知道该怎么做。 See below my code what is my requirement i mentioned below my code. 请参阅下面的代码,我在代码下面提到的是什么要求。 Insteed of htmlString += '<td>' + 'castename' + '</td>';
由htmlString += '<td>' + 'castename' + '</td>';
I want call the function castename()
. 我想调用函数castename()
。
$(document).ready(function(){
$("#reservation").on("change", function() {
var reservation = $(this).val();
$.ajax({
type: 'post',
url: 'date-range.php',
data: {
logindate: reservation,
},
async : false,
success: function(data) {
var res = jQuery.parseJSON(data); // convert the json
console.log(res);
if (res['status'] == "success") {
$('#datatable-editable > tbody').empty();//emtpy tbody at the begining
$.each(res['data'], function(key, value) {
var htmlString = ''; //Place declaration inside each
htmlString += '<tr>';
var ssm_id = value.ssm_id; // here i got ssmid
htmlString += '<td>' + value.ssm_id + '</td>';
$.ajax({
type: 'post',
url: 'config/functions.php',
data: {
'ssm_id': ssm_id,
/* 'caste':ssm_id,
'reg_date':ssm_id,
'status':ssm_id,
'source':ssm_id*/
},
async : false,
success: function(fname) {
htmlString += '<td>' + fname + '</td>';
htmlString += '</tr>';
$('#datatable-editable > tbody').append(htmlString);
}
});
$.ajax({
type: 'post',
url: 'config/functions.php',
data: {
'caste': ssm_id,
},
async: false,
success: function(caste) {
htmlString += '<td>' + caste + '</td>';
htmlString += '</tr>';
$('#datatable-editable > tbody').append(htmlString);
}
});
});
}
}
});
});
});
mycode.php mycode.php
<?php
$ssm_id = $_POST['ssm_id'];
$caste_name = $_POST['caste'];
$reg_date = $_POST['reg_date'];
if (!empty($ssm_id))
{
echo firstname($ssm_id);
}
if (!empty($caste_name)) {
echo castename($caste_name);
}
if (!empty($reg_date)) {
echo regdate($reg_date);
}
function firstname($id)
{
$f = "SELECT firstname FROM register WHERE matri_id='$id'";
$rr = mysql_query($f);
while ($row=mysql_fetch_array($rr))
{
$firstname = $row['firstname'];
}
return $firstname;
}
unction castename($id)
{
$f = "select caste_name from caste where caste_id='$id'";
$rr = mysql_query($f);
while ($row=mysql_fetch_array($rr))
{
$caste_name = $row['caste_name'];
}
return $caste_name;
}
function regdate($id)
{
$f = "SELECT reg_date FROM register WHERE matri_id='$id'";
$rr = mysql_query($f);
while ($row=mysql_fetch_array($rr))
{
$reg_date = $row['reg_date'];
}
return $reg_date;
}
?>
Seems your ajax request's success function are overlapped. 您的ajax请求的成功功能似乎重叠了。
User Ajax async:false
用户Ajax async:false
For more information see click on this 欲了解更多信息,请点击这里
Try this :- 尝试这个 :-
$(document).ready(function() {
$("#reservation").on("change", function() {
var reservation = $(this).val();
$.ajax({
type: 'post',
url: 'date-range.php',
data: {
logindate: reservation,
},
async: false,
success: function(data) {
var res = jQuery.parseJSON(data); // convert the json
console.log(res);
if (res['status'] == "success") {
$('#datatable-editable > tbody').empty(); //emtpy tbody at the begining
$.each(res['data'], function(key, value) {
var htmlString = ''; //Place declaration inside each
htmlString += '<tr>';
var ssm_id = value.ssm_id; // here i got ssmid
htmlString += '<td>' + value.ssm_id + '</td>';
$.ajax({
type: 'post',
url: 'config/functions.php',
data: {
'ssm_id': ssm_id,
/* 'caste':ssm_id,
'reg_date':ssm_id,
'status':ssm_id,
'source':ssm_id*/
},
async: false,
success: function(fname) {
htmlString += '<td>' + fname + '</td>';
}
});
$.ajax({
type: 'post',
url: 'config/functions.php',
data: {
'caste': ssm_id,
},
async: false,
success: function(caste) {
htmlString += '<td>' + caste + '</td>';
}
});
htmlString += '</tr>';
$('#datatable-editable > tbody').append(htmlString);
});
}
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.