簡體   English   中英

如何使用AJAX調用多個功能

[英]How to call multiple functions using AJAX

在這段代碼中,我正在使用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;
    }
?>

您的ajax請求的成功功能似乎重疊了。

用戶Ajax async:false

欲了解更多信息,請點擊這里

嘗試這個 :-

$(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM