<script type="text/javascript">
$('.next').click(function () {
var radio_check_val = "";
for (i = 0; i < document.getElementsByName('<?php echo $result['id']; ?>').length; i++) {
if (document.getElementsByName('<?php echo $result['id']; ?>')[i].checked) {
radio_check_val = document.getElementsByName('<?php echo $result['id']; ?>')[i].value;
}
}
if (radio_check_val == "1")
{
<?php
$res1 = mysqli_query($con, "INSERT INTO quiz_log (email,question,answer_choose,answer_correct,category,level) VALUES ('$email','$question','$a1','$answerko','$category','$qlevel')");
?>
} else if (radio_check_val == "2"){
<?php
$res2 = mysqli_query($con, "INSERT INTO quiz_log (email,question,answer_choose,answer_correct,category,level) VALUES ('$email','$question','$a2','$answerko','$category','$qlevel')");
?>
}
});
</script>
It insert both SQL query. if the radio button is checked it insert also the other SQL query. I need insert query if the radio button 1 is checked, and if the radio button 2 is checked the sql query will insert.
PHP is executed on the server before the browser executes JavaScript. So both SQL queries will already be done before the visitor can see the result. He could chose for example one of the results, you can show/hide something using JavaScript and send a choice back to the server.
It looks, as you want to depend on visitors choice, wich query will be done. In this case you have to present a form to the browser, send back the form data and then process the query on another PHP-page.
<?php if(array_key_exists('myradio', $_GET)): ?>
<?php if($_GET['myradio'] === '1'): ?>
First option has been selected.
<?php endif ?>
<?php if($_GET['myradio'] === '2'): ?>
Second option has been selected.
<?php endif ?>
<?php endif ?>
<form method="get" action="">
<input type="radio" name="myradio" value="1"> Option 1
<input type="radio" name="myradio" value="2"> Option 2
<input type="submit">
</form>
In this example the form data is sent back to the same PHP file. You can split it into two files and give the URL of the second one into the action
attribute of the <form>
.