0

最初,当页面加载时,ajax 会填充一个选择菜单。当我输入新的患者信息时,它会添加到数据库中,但患者选择菜单不会在选择新患者时追加/刷新。

我希望有人可以帮助我解决我做错的事情。

阿贾克斯调用

$('#newpatient').click(function() {
    var patientadd = $('#patientaddform').serializeArray();
    $.ajax({
        url: 'patientadd.php',
        type: "POST",
        data: patientadd,
        dataType: 'json',
        success: function(data) {
            var html = '';
            var len = data.length;
            for (var i = 0; i < len; i++) {
                html += '<option value="' + data[i].patient_id + '">' + data[i].patient_firstname + ' ' + data[i].patient_lastname + '</option>';
            }
            $('#patientselect').append(html);
        },
        complete: function() {
            $('#patientselect').selectmenu('refresh', true);
        }
    });
});​

病人添加.php

if (empty($_POST) === false) {
    $update_data = array('company_id' = > $_POST['company_id'], 'addpatient_firstname' = > $_POST['addpatient_firstname'], 'addpatient_lastname' = > $_POST['addpatient_lastname'], 'addpatient_dob' = > $_POST['addpatient_dob'], 'patient_added' = > $_POST['patient_added']);


    $required_fields = array('company_id', 'addpatient_firstname', 'addpatient_lastname', 'addpatient_dob', 'patient_added');
    foreach($update_data as $key = > $value) {
        if (empty($value) && in_array($key, $required_fields) === true) {
            $errors[] = 'Fields marked with an asterisk are required';
            break 1;
        }
    }
}

if (empty($_POST) === false && empty($errors) === true) {

    $company_id = $_POST['company_id'];
    $patient_id = $_POST['addpatient_id'];
    $first_name = $_POST['addpatient_firstname'];
    $last_name = $_POST['addpatient_lastname'];
    $dob = $_POST['addpatient_dob'];
    $updated = $_POST['patient_added'];


    $update = array();
    array_walk($update_data, 'array_sanitize');

    foreach($update_data as $field = > $data) {
        $update[] = '`'.$field.'` = \''.$data.'\'';
    }

    mysql_query("INSERT INTO `lab`.`patients` (`company_id`,  `patient_id`, `patient_firstname`, `patient_lastname`, `patient_dob`, `patient_added`) VALUES ('$company_id', NULL, '$first_name', '$last_name', '$dob', '$updated')");
    $patient_id = mysql_insert_id(); /*echo '<strong>' . $patient_id . ' ' . $first_name . ' ' . $last_name . '</strong> has been added!';*/
}

$data = array('patient_id' = > $patient_id, 'patient_firstname' = > $first_name, 'patient_lastname' = > $last_name);
echo json_encode($data);
4

1 回答 1

1

将您的代码从函数complete移至success函数。

$('#newpatient').click(function() {
    var patientadd = $('#patientaddform').serializeArray();
    $.ajax({
        url: 'patientadd.php',
        type: "POST",
        data: patientadd,
        dataType: 'json',
        success: function(data) {
            var html = '';
            var len = data.length;
            for (var i = 0; i < len; i++) {
                html += '<option value="' + data[i].patient_id + '">' + data[i].patient_firstname + ' ' + data[i].patient_lastname + '</option>';
            }
            $('#patientselect').append(html);
            $('#patientselect').selectmenu('refresh', true);
        },
        complete: function() {
            //$('#patientselect').selectmenu('refresh', true);
        }
    });
});​

Ajax 执行是异步的。该success方法只执行请求返回的那些。但是, onesuccess被触发,complete. 这意味着您最终会陷入一种竞争条件,其中complete执行之前success完成执行。Complete不等待success完成。success并且complete几乎同时执行。

为确保您的刷新调用包含新数据,请在success以下append.

于 2012-09-20T22:38:47.693 回答