如何删除一个单选按钮的后置值以呈现新的无线电选择结果

时间:2018-01-31 11:27:34

标签: javascript php jquery html ajax

我正在使用带有3个单选按钮的模块。单击一个单选按钮可列出与其相关的数据,依此类推。我已经在单选按钮上应用了ajax调用,并在响应中返回一个数组。我的问题在于每个单选按钮单击数据继续添加到列表中。对于例如如果我点击“所有用户”#39;收音机显示与" All"相关的数据。但是,如果我点击"未经批准的用户"收音机,该列表加起来所有用户'列在底部。那么每次渲染数据时如何重置$ _POST变量,以便为新的无线电选择显示新列表, 我真的很感激这方面的帮助或建议。

代码为,

 <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 :(得分:1)

每个新请求都会重置$ _POST变量。

这里的问题是你正在使用jquery的append()功能。此功能只会将内容添加到元素的末尾。

EDITED: 要在添加新数据之前清除列表,请使用empty()功能。