jquery 如何捕获提交的POST名称

x9ybnkn6  于 2022-12-03  发布在  jQuery
关注(0)|答案(1)|浏览(121)

我有以下几点:

<form class="" id="quiz_form" action="" method="post">
    <input type="text" name="<?php echo $question_id; ?>[]" value="answer">
    <input type="text" name="<?php echo $question_id; ?>[]" value="answer">
    <input type="button" onclick="submitQuiz()">
</form>

这些输入位于foreach内,因此name属性的值始终不同。
POST发送如下:

function submitQuiz() {
    $.ajax({
        url: '<?php echo site_url('home/submit_avaliacao'); ?>',
        type: 'post',
        data: $('form#quiz_form').serialize(),
        success: function(response) {
            $('#quiz-body').hide();
            $('#quiz-result').html(response);
        }
    });
}

我试着这样处理数据:

foreach ($this->input->post(NULL, TRUE) as $row){
            foreach ($row as $key=>$value){
                $data_answer['question_id'] = $key;
                $data_answer['answer'] = $value;
                $this->crud_model->av_insert_answer($data_answer);
            }
        }

问题是$key的值对于所有输入总是0,而对于$value这是可以的。
我相信这是由于输入的name中的**[ ]**,但如果我删除它们,数据就不会发送。如何解决这个问题?
我用这种方法做了一个测试,也没有成功:

foreach ($_POST as $row){
            foreach ($row as $key=>$value){
                $param_name = 'mqc';
                if(substr($key, 0, strlen($param_name)) == $param_name) {
                    $data_answer['question_id'] = $key;
                    $data_answer['answer'] = $value;
                }
            }
        }

在本例中,我在输入的名称中添加了文本**“mqc”**,但没有效果。
print_r($this->input->post(NULL, TRUE))的输出:

Array
(
    [8] => Array
        (
            [0] => answer input 1
        )

    [7] => Array
        (
            [0] => answer input 2
        )

    [9] => Array
        (
            [0] => answer input 3
        )

)
Array
(
    [8] => Array
        (
            [0] => answer input 1
        )

    [7] => Array
        (
            [0] => answer input 2
        )

    [9] => Array
        (
            [0] => answer input 3
        )

)
Array
(
    [8] => Array
        (
            [0] => answer input 1
        )

    [7] => Array
        (
            [0] => answer input 2
        )

    [9] => Array
        (
            [0] => answer input 3
        )

)
rmbxnbpk

rmbxnbpk1#

在第一个循环中,我们为键分配一个名称,在第二个循环中,只分配键的索引。

<div id="quiz-result"></div>
<div id="quiz-body">
    <?php
    $question_id = 'question_id';
    ?>
    <form class="" id="quiz_form" action="" method="post">
        <input type="text" name="<?php echo $question_id; ?>[]" value="mqc">
        <input type="text" name="<?php echo $question_id; ?>[]" value="answer">
        <input type="button" onclick="submitQuiz()">
    </form>
</div>
<script>
    function submitQuiz() {
        $.ajax({
            url: '_code.php',
            type: 'post',
            data: $('form#quiz_form').serialize(),
            success: function(response) {
                console.log(response);
                $('#quiz-body').hide();
                $('#quiz-result').html(response);
            }
        });
    }
</script>
foreach ($_POST as $key => $row) {
    foreach ($row as $value) {
        $param_name = 'mqc';
        if (substr($value, 0, strlen($param_name)) == $param_name) {
            $data_answer['question_id'] = $key;
            $data_answer['answer'] = $value;
        }
    }
}
echo json_encode($data_answer);

相关问题