php - How to capture submitted POST name
Get the solution ↓↓↓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 aforeach
, so the value of thename
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 thename
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 forprint_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
)
)
Answer
Solution:
foreach ($_POST as $row){ //first
var_dump($row);
foreach ($row as $key=>$value){
//...
}
}
<form action="test1.php" method="post">
<input type="text" name="34[]">
<input type="text" name="35[]">
<input type="submit">
</form>
I inspected what the variable $row contain:
array(1) { [0]=> string(2) "ww" } array(1) { [0]=> string(2) "ss" }
and it clearly see that the variable $key always contains the value zero.
Add to the first foreach a key and you get yours ids:
foreach ($_POST as $keyRow => $row){
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.