Reputation: 31
I have a query which fetches data from the database within a loop, for each instance of loop a form is created, now I want to get the value of each forms data within the loop itself and check if it matches with its corresponding row's column in the database:
for eg:
<?php
$marks = 0;
while ($row= mysqli_fetch_assoc($resut)) {
echo '<form method="POST" action="">
<div class="col-sm-8" id="question">'.$row['question'] .'</div>
<input type="radio" placeholder="" name="answer" value = "true" id=""> True
<input type="radio" placeholder="" name="answer" value = "false" id=""> false
</form>';
if (isset($_POST['save'])){ // this is the submit button present outisde the loop at last
if ($_POST['answer'] == $row['answer']) {
$marks++;
} else {
echo "incorrect";
}
}
}
?>
<form role="form" action="" method= "POST">
<button name ="save" id=" class="btn btn-primary">Save</button>
</form>
So what I am trying to achieve is that I have a column in database which I can get by $row['answer']
within the loop. so I could check the forms data with this row by if( $_POST['answer'] == $row['answer'])
.. .do smth...But the problem is that I have to keep the submit button outside the loop since there should only be only 1 submit button and if I put it outside the loop it is not working.
My attempts:
I tried to keep the form tag before the loop. if I do this then suppose there are 10 rows in database I would be needing 10 forms and 10 values taken within the loop. but then if I press the radio button in one of the rows the other would be deselected. so in my opinion the form tag should be inside the whole loop only.
I tried keeping the form submit button (present at last) inside the while loop within every instance of rows fetched. then again it would echo every button for every row which is not the motive. I just want one button for all the forms to be submitted and get values accordingly.
also I cannot do this check outside or in any other page since I have to check with every instance of the loop i.e. $row['answer']
with every instance of forms radio button data i.e. $_POST['answer']
.
Thank you for every response.
Upvotes: 1
Views: 202
Reputation: 1232
If you want to send all the answers at once, you'll need to use a single form.
To do that, you can change the name
of your input
so that it's different for each question.
$marks = 0;
$idx = 0;
echo '<form method="POST" action="">';
while ($row= mysqli_fetch_assoc($resut)) {
++$idx;
echo
'<div class="col-sm-8" id="question">'.$row['question'].'</div>
<input type="radio" placeholder="" name="answer'.$idx.'" value = "true" id=""> True
<input type="radio" placeholder="" name="answer'.$idx.'" value = "false" id=""> False';
if (isset($_POST['save'])) { // this is the submit button present
outisde the loop at last
if ($_POST['answer'] == $row['answer']) {
$marks++;
} else {
echo "incorrect";
}
}
}
echo '</form>';
You can also inject the $idx
between brackets, which will give you a single $_POST['answer']
entry, but which will be an array instead of a string. That array will contain all your answers assignated to the keys you injected as $idx
:
<input type="radio" placeholder="" name="answer['.$idx.']" value = "true" id=""> True
<input type="radio" placeholder="" name="answer['.$idx.']" value = "false" id=""> False
Upvotes: 1