如何使用AJAX调用多个函数

时间:2016-05-23 08:15:05

标签: javascript php jquery

在这段代码中我使用的是AJAX。我想在td中附加值。我已经为一列添加了值,之后我想追加第二个td的值。我不知道该怎么做。请参阅下面的代码,我在下面的代码中提到了我的要求。 htmlString += '<td>' + 'castename' + '</td>';我想要调用函数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

<?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;
    }
?>

1 个答案:

答案 0 :(得分:-1)

似乎你的ajax请求的成功函数是重叠的。

用户Ajax async:false

For more information see click on 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);
                    });

                }
            }
        });
    });
});