未获得AJAX响应

时间:2018-01-31 07:50:57

标签: javascript php jquery mysql ajax

我在点击单选按钮发送AJAX请求的页面上工作。有三个无线电可以列出“未批准”,“已批准”和“全部”的用户。根据无线电选择,我想在表格中显示各自的数据。目前我没有从AJAX函数中的请求页面获得响应。我将非常感谢并感谢任何帮助。 我的代码,

    <script>
        $('#selection').change
        (

            function() 
            {
              var selected_value = $("input[name='users']:checked").val();
              //till here the code works fine.
$.ajax
                ( 
                    {
                        url: "approval_ajax.php",
                        dataType : "json",
                        type: "POST",
                        cache: false,
                        data: { selected_value : selected_value },

                        success: function(response)
                        {
                             console.log(response);
                                var len = response.length;
                           for(var i=0; i<len; i++){
                                 var id = response[i].id;
                                 var email = response[i].email;
                                 var employee_id = response[i].employee_id;
                                 var first_name = response[i].first_name;
                                 var middle_name = response[i].middle_name;
                                 var last_name = response[i].last_name;
                                 var mobile = response[i].mobile;
                                 var created_on = response[i].created_on;
                                 var disabled = response[i].disabled;

                    var tr_str = "<tr>" +
                        "<td>" + (i+1) + "</td>" +
                        "<td>" + email + "</td>" +
                        "<td>" + employee_id + "</td>" +
                        "<td>" + first_name + "&nbsp;" + middle_name + "&nbsp;" + last_name + "</td>" +
                        "<td>" + mobile + "</td>" +
                        "<td>" + created_on + "</td>" +
                        "<td><input type='checkbox' name='check[]'"  + disabled +  "value= '"  + id +  "' class='checkbox' id='select_all' ></td>" +
                          "<input type='hidden' value='"  + id +  "' name='user_id' id='user_id' >" +
                        "</tr>";

                    $("#example").append(tr_str);
                }
                           alert("AJAX was a success");
                        }
                    }
                );
            }
        );
    </script>

现在我的 approval_ajax.php

   <?php


session_start();

require("../includes/config.php"); 
require("../classes/Database.class.php"); 
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);  

$return_arr = array();
$status=''; 

   if($_SERVER['REQUEST_METHOD'] == 'POST')
   {
       $value = filter_input(INPUT_POST, "selected_value");


       if (isset($value))
       {
         $users=$value;

}else{
    $users='';
}

switch ($users)

{

case "all":
  $sqlQuery = "SELECT * FROM tbl_user WHERE type =3";       

break;

case "approved":
  $sqlQuery = "SELECT * FROM tbl_user WHERE type =3 AND status =1";   
break;

case "unapproved":
  $sqlQuery = "SELECT * FROM tbl_user WHERE type =3 AND status =0";   

break;

}

$sq = $db->query($sqlQuery);


if ($db->affected_rows > 0) {

while ($row = mysql_fetch_array($sq)) {
    $disabled = '';
    if ($status == '1') {

        $disabled = "disabled = 'disabled' checked='checked' ";
    }

    $id = $row['id'];
    $email = $row['email'];
    $employee_id = $row['employee_id'];
    $first_name = $row['first_name'];
    $middle_name = $row['middle_name'];
    $last_name = $row['last_name'];
    $mobile = $row['mobile'];
    $created_on1 = $row['created_on'];
    $created_on = date("d-m-Y", strtotime($created_on1));

    $return_arr[] = array("id" => $id,
    "email" => $email,
    "employee_id" => $employee_id,
    "first_name" => $first_name,
    "middle_name" => $middle_name,
    "last_name" => $last_name,
    "mobile" => $mobile,
    "created_on" => $created_on
    "disabled" => $disabled
    );
}
}
header('Content-Type: application/json', true, 200);
echo json_encode($return_arr);

}

1 个答案:

答案 0 :(得分:2)

$return_arr数组中存在语法错误。您应该在created_on => value上使用逗号。将数组修改为此

$return_arr[] = array("id" => $id,
    "email" => $email,
    "employee_id" => $employee_id,
    "first_name" => $first_name,
    "middle_name" => $middle_name,
    "last_name" => $last_name,
    "mobile" => $mobile,
    "created_on" => $created_on, 
    "disabled" => $disabled

另请不要使用mysql_*功能。使用mysqli_*pdo