I have a DB, in this DB i have a table called question, then another table called q_options. My code brings the questions and then the options for each question. The options are always radio BTNs and i assign the questionId to the name of the radio BTN.
<input type = "radio" name = "<?php echo $optionsQID; ?>" value = "<?php echo $optionsID; ?>">
That part right there is the one i can't get to my DB. when i print_r my POST i can see the field with the assigned number is been posted but i can't retrieve it from $qAnswer = $_POST[$optionsQID];
after submit.
I have tried with the quotes and without the quotes, i also tried adding and identifier to the radio BTN name along with the variable but i still don't get the value of that field.
I also moved the entire
if (isset($_POST["answerQuestion"])){
$question = $_POST["questionID"];
$qAnswer = $_POST[$optionsQID];
$sql = "INSERT INTO answers (id, q_id, answer)
VALUES (NULL, '$question', '$qAnswer')";
echo "Thanks for your input";
if(!mysqli_query($link, $sql)) {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
inside the second while loop and this sends the correct value of $qAnswer but then also creates a record for each of the questions.
This is my code
<?php
if (isset($_POST["answerQuestion"])){
$question = $_POST["questionID"];
$qAnswer = $_POST[$optionsQID];
$sql = "INSERT INTO answers (id, q_id, answer)
VALUES (NULL, '$question', '$qAnswer')";
echo "Thanks for your input";
if(!mysqli_query($link, $sql)) {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
?>
<?php $questionsResult = $link->query("SELECT * FROM questions");
while($row = mysqli_fetch_array($questionsResult)){
$questionID = $row['id'];
$question = $row['question'];
?>
<div style = "margin: 20px 10%; padding: 50px 10%; width: 60%; border: 1px solid #444444;">
<form role="form" action = "" method="post" id="answerForm" name = "answerForm">
<h3><?php echo $question; ?></h3>
<input type = "hidden" name = "questionID" value = "<?php echo $questionID; ?>">
<?php
$optionsResult = $link->query("SELECT * FROM q_options WHERE q_id = $questionID");
while($optionsRow = mysqli_fetch_array($optionsResult)){
$optionsID = $optionsRow['id'];
$optionsQID = $optionsRow['q_id'];
$option = $optionsRow['options'];
?>
<div style = "padding: 5px 0;">
<input type = "radio" name = "<?php echo $optionsQID; ?>" value = "<?php echo $optionsID; ?>"> <?php echo $option; ?>
</div>
<?php
}
?>
<button type="submit" name = "answerQuestion">Send Answer</button>
</form>
</div>
<?php
}
?>
Here's what i did in order to get this working and move on. At least while i finish the proof of concept.
I moved the if (isset($_POST["answerQuestion"])){
inside the loop that brings my questions, since all variables are defined by then i get my values passed on POST, this creates a problem that it will create a record for every question even if they are not answered by the user. I solve this new problem by deleting those extra records without an answer value. It is a dirty solution but it takes so little time i'm going with it for now until someone give me a better way to do it.
Make sure you are running the script on a server either local (using XAMPP) or on a host online. A few days ago I had a similar issue with POST data from my Ajax request to my PHP file and that resolved it after hours of googling. I could be wrong but just make sure you are running it on a server (if you want to try on a free live host you can use 000webhost)
Links https://www.000webhost.com/ https://www.apachefriends.org/index.html