I'm getting questions from questions table in db.
I'm saving question_id and answer_value with an unique form_id and user_id to answers table in database.
I would like for the user to be able to update the form another day. So how can i get and display the form with answer_value already filled out based on the users earlier answer?
This is how i display questions from db:
<form action="/form/insert.php" method="POST">
$query = "SELECT * FROM questions where active=1 AND question_sort=1 ORDER BY sort_by";
$result = @mysqli_query($con, $query);
if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$body = $row['question_body'];
$question_id = $row['question_id'];
echo '<tr>
<td class="question">'.$body.'</td>
<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="0" ></td>
<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="1" ></td>
<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="2" ></td>
</tr>';
}
</form>
This is how i save every unicqe form to db:
$question_id = mysqli_real_escape_string($con, $_POST['question_id']);
$user_id = mysqli_real_escape_string($con, $_POST['user']);
$form_id = mysqli_real_escape_string($con, $_POST['form_id']);
$form_date = gmdate('Y-m-d H:i:s');
foreach ($_POST['answer_value'] as $question_id => $answer_id){
$sql="INSERT INTO answers (question_id, answer_value, user_id, form_id, form_date)
VALUES ({$question_id}, {$answer_id}, $user_id, {$form_id}, '$form_date')";
you have to loop through the values and match to the answer value you stored
<form action="/form/insert.php" method="POST">
<?php
$vals=array(0,1,2);
$query = "SELECT * FROM questions where active=1 AND question_sort=1 ORDER BY sort_by";
$result = @mysqli_query($con, $query);
if ($result) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$body = $row['question_body'];
$question_id = $row['question_id'];
echo '<tr>
<td class="question">'.$body.'</td>';
foreach($vals as $x){
$s='';
if($x==$row['answer_value']){
$s="selected";
}
echo '<td class="answer"><input type="radio" name="answer_value['.$question_id.']" value="'.$x.'" '.$s.'></td>';
}
echo '</tr>';
}
}
?>
</form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.