将多个值传递给ajax

时间:2018-02-15 07:40:25

标签: javascript jquery json codeigniter-3

这里我想在两个数字之间显示结果,所以我写了这样的代码,但它只显示了最后一个值。

这是我的代码,请看看

<script>
    $("#getdata").on('click', function () {
        var form_data = {
            agent_name: $('#agent_name').val(),
            number: $('#number').val(),
            number_from: $('#number_from').val(),
            number_to: $('#number_to').val(),
            quantity: $('#quantity').val(),
            amount: $('#amount').val(),
            date: $('#date').val(),
            commision: $('#commision').val(),
            profit: $('#profit').val(),
            agent_amount: $('#agent_amount').val(),
            user_id: $('#user_id').val(),
            type: name_type.val(),
        }
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url(); ?>admin_control/ajax_data',
            data: form_data,
            dataType: "json", //to parse string into JSON object,
            success: function (data) {
                var fragment = '';
                for (var i = 0; i < data.json.length; i++) {
                    fragment += '<tr><td><input type="hidden" id="add_type" name="add_type[]" value="super">' + data.json[i].type + '</td><td><input type="hidden" id="add_number" name="add_number[]" value="10">' + data.json[i].number + '</td><td><input type="hidden" id="add_quantity" name="add_quantity[]" value="10">' + data.json[i].quantity + '</td><td><input type="hidden" id="add_amount" name="add_amount[]" value="10">' + data.json[i].amount + '</td><td><input type="checkbox" class="add_checkbox" name="layout" id="add_checkbox" value="1" checked></td></tr>';
                }
                $("#table").append(fragment).removeClass("hidden");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('error: ' + textStatus + ': ' + errorThrown);
            }

        });
    });
</script>

这是我的控制器

<?php
public function ajax_data() {
    if ((!empty($_POST['number_from']) && !empty($_POST['number_to']))) {
        $number_from = $_POST['number_from'];
        $number_to = $_POST['number_to'];
        $numbers = $this->get_all_numbers($number_from, $number_to);
        foreach ($numbers as $num) {
            $array = array(array("agent_name" => $_POST['agent_name'], "number" => $num, "type" => $_POST['type'], "quantity" => $_POST['quantity'], "amount" => $_POST['amount'], "date" => $_POST['date'], "commision" => $_POST['commision'], "profit" => $_POST['profit'], "agent_amount" => $_POST['agent_amount'], "user_id" => $_POST['user_id']));
        }
        $data['json'] = $array;
        echo json_encode($data);
    }
}
?>

这里假设我选择100105之间的值,那么结果需要100105之间的所有值,但此处仅获取{{1}的值}

2 个答案:

答案 0 :(得分:2)

您的PHP控制器文件中有错误。您在循环中分配相同的$数组变量。

替换此行

$array = array(array("agent_name" => $_POST['agent_name'],"number"=>$num,"type"=>$_POST['type'],"quantity"=>$_POST['quantity'],"amount"=>$_POST['amount'],"date"=>$_POST['date'],"commision"=>$_POST['commision'],"profit"=>$_POST['profit'],"agent_amount"=>$_POST['agent_amount'],"user_id"=>$_POST['user_id']));

这一行

$array[] = array(array("agent_name" => $_POST['agent_name'], "number" => $num, "type" => $_POST['type'], "quantity" => $_POST['quantity'], "amount" => $_POST['amount'], "date" => $_POST['date'], "commision" => $_POST['commision'], "profit" => $_POST['profit'], "agent_amount" => $_POST['agent_amount'], "user_id" => $_POST['user_id']));

简而言之,将$array = array( ....替换为$array[] = array( ....

答案 1 :(得分:2)

您每次都在编写$array,因此您将获得最后一行数组。您还在array内添加了array,例如array(array())。在这里,您必须删除一个array

$array[] = array(
    "agent_name" => $_POST['agent_name'],
    "number" => $num,
    "type" => $_POST['type'],
    "quantity" => $_POST['quantity'],
    "amount" => $_POST['amount'],
    "date" => $_POST['date'],
    "commision" => $_POST['commision'],
    "profit" => $_POST['profit'],
    "agent_amount" => $_POST['agent_amount'],
    "user_id" => $_POST['user_id']
);