将多个值传递给ajax

时间:2022-09-25 15:55:49

Here I want to display the results in between two numbers so I wrote my code like this but its only displaying the last value.

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

Here is my code please have a look

这是我的代码请看看

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

here is my controller

这是我的控制器

<?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);
    }
}
?>

Here suppose if I choose value between 100 and 105 then the result needs all values between 100 and 105 but here getting only the value of 105

这里假设如果我选择介于100和105之间的值,那么结果需要100到105之间的所有值,但这里只得到105的值

2 个解决方案

#1


2  

You have mistake in your PHP Controller file. You are assigning the same $array variable in the loop.

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

Replace this line

替换此行

$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']));

with this line

用这条线

$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']));

In short replace $array = array( .... with $array[] = array( ....

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

#2


2  

You are over writing $array every time so you will get the last row array. Also you have added array inside array like array(array()). Here you have to remove one 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']
);

#1


2  

You have mistake in your PHP Controller file. You are assigning the same $array variable in the loop.

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

Replace this line

替换此行

$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']));

with this line

用这条线

$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']));

In short replace $array = array( .... with $array[] = array( ....

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

#2


2  

You are over writing $array every time so you will get the last row array. Also you have added array inside array like array(array()). Here you have to remove one 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']
);