dps43378 2018-01-31 11:27 采纳率: 0%
浏览 55

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

I am working on a module with 3 radio buttons. Clicking one radio button lists data related to it and so on. I have applied ajax call on radio buttons click and return an array in the response. My problem here is for each radio button click the data keeps on adding into the list. For e.g. if I click on 'All users' radio it displays the data related to "All" but again if I click on "Unapproved Users" radio, the list adds up to the 'All users ' listing at the bottom. So how can I reset the $_POST variable each time the data is rendered so that new list appears for new radio selection, I would really appreciate the help or advice for this.

Code goes as,

 <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>

And now my 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条回答 默认 最新

  • duanpo1821 2018-01-31 11:37
    关注

    The $_POST variable is reset on every new request.

    The issue here is that you're using jquery's append() function. This function just adds up content to the end of your element.

    EDITED: In order to clear your list before adding new data, use the empty() function.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀