zecaluis
zecaluis

Reputation: 155

How to capture submitted POST name

I have the following:

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

These inputs are inside a foreach, so the value of the name attribute is always different.

POST is sent as follows:

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

I try to treat the data in this way:

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

The problem is that the value of $key is always 0 for all inputs, while for $value this is ok.

I believe it is due to the [ ] that are in the name of the input, but if I remove them the data is not sent. How to solve this?

I did a test this way and it also didn't work:

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

In this case I added the text "mqc" in the name of the input, but to no avail.

Output for 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
        )

)

Upvotes: 1

Views: 186

Answers (1)

bujals
bujals

Reputation: 278

It seems that you are trying to access the key of the outer array instead of the actual key from the input name. To fix this, you should modify your PHP code like this:

$data_answers = [];
foreach ($_POST as $key => $row) {
    foreach ($row as $value) {
        $data_answer['question_id'] = $key;
        $data_answer['answer'] = $value;
        //I added the line $data_answers[] = $data_answer; for testing purposes.
        $data_answers[] = $data_answer;
        //$this->crud_model->av_insert_answer($data_answer);
    }
}

In this modified code, I added $key => before $row in the first foreach loop. This way, you can access the key (i.e., the question ID) of the outer array directly.

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

This form contains a loop that iterates through question IDs from 1 to 3. Inside the loop, an input element is created for each question ID with a name attribute that includes the question ID followed by square brackets (e.g., 1[], 2[], and 3[]). The input elements have a default value of "answer".

The data with the following values found in the $data_answer variable should be passed to the av_insert_answer() function:

{"question_id":1,"answer":"answer"},
{"question_id":2,"answer":"answer"},
{"question_id":3,"answer":"answer"}

Upvotes: 3

Related Questions