-1
I’m making a <form>
that returns an interaction if the answer is wrong.
The idea is: if the user selects the right answer, the <button>
change the color to green and/or red for wrong.
This using $.ajax
dynamically without the page being updated!
Would anyone have any idea how to catch the value selected in radio button and compare with the correct answer, within this code? Or would have a better way?
<form name="frm.$row[question_id]." role="form" id="submitForm.$row[question_id].">
<input type="hidden" class="form-control" name="question_id" placeholder="Name" value=".$row[question_id]." required="" />
<input type="hidden" class="form-control" name="user_id" placeholder="User" value=".$myid." required="" />
<div class="form-check">
<input class="form-check-input.$row[question_id]." type="radio" name="resposta" id="blankRadio1" value="A" aria-label="..." onClick="mostrar.$row[question_id].()" />
A) .$row[option1].
</div>
<div class="form-check">
<input class="form-check-input.$row[question_id]." type="radio" name="resposta" id="blankRadio1" value="B" aria-label="..." onClick="mostrar.$row[question_id].()" />
B) .$row[option2].
</div>
<div class="form-check">
<input class="form-check-input.$row[question_id]." type="radio" name="resposta" id="blankRadio1" value="C" aria-label="..." onClick="mostrar.$row[question_id].()" />
C) .$row[option3].
</div>
<div class="form-check">
<input class="form-check-input.$row[question_id]." type="radio" name="resposta" id="blankRadio1" value="D" aria-label="..." onClick="mostrar.$row[question_id].()" />
D) .$row[option4].
</div>
<br />
<button type="submit" class="btn btn-info btn btn-block" id="submitbtn.$row[question_id]." name="botao.$row[question_id]." disabled>Responder</button>
</form>
<script type="text/javascript">
$(document).ready(function () {
$("#submitForm.$row[question_id].").on("submit", function (e) {
e.preventDefault();
$("#submitbtn").text("Please wait..");
var userForm = $(this).serialize();
$.ajax({
url: "insertData.php",
type: "POST",
cache: false,
data: userForm,
success: function (response) {
$("#submitbtn.$row[question_id].").attr("disabled", true);
$("#success").show();
$("#success").html("Data inserted successfully");
$("#submitbtn.$row[question_id].").text("Gabarito: .$row[answer].");
$("#submitForm")[0].reset();
},
});
});
});
</script>
Vlw, gleisin-dev. Thanks! I got dps right away by comparing the userFrom variable with the selected value on the radio. It was not working because it is more of a value that this variable takes. I fixed it and it worked. if(userForm == "answer='. $Row['Answer']. '&question_id='. $Row['question_id']. '&user_id='. $myid. '") { $("#submitbtn'. $Row['question_id']. '"). removeClass("btn-Danger"). addClass("btn-Success"); } Else{$("#submitbtn'. $Row['question_id']. '"). removeClass("btn-Success"). addClass("btn-Danger");}
– alan