Cara menghitung semua radio button yang diklik saat klik submit maka skor akan masuk ke database

Maka modal skor akan mengambil skor yang ada di database. bagaimana melakukan itu dikatakan bahwa saya harus menggunakan

JQuery, haruskah saya benar-benar menggunakannya?

Nilai dari 4 radio button adalah 3 salah dan 1 benar cara menghitung semua radio button dengan nilai yang benar saat di klik.

HTML, JS dan PHP di bawah ini

<?php
$Ans1 = $_POST['1'];
$Ans2 = $_POST['2'];
$Ans3 = $_POST['3'];
$Ans4 = $_POST['4'];
$Ans5 = $_POST['5'];
$Ans6 = $_POST['6'];
$Ans7 = $_POST['7'];
$Ans8 = $_POST['8'];
$Ans9 = $_POST['9'];
$Ans10 = $_POST['10'];

$Ans1 = $_POST['1'];

$INSERT = "INSERT Into scores(score) values (?)" ;



?>

<!-- PHP CODE FOR QUESTIONS AND ANSWERS   -->

<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'I have 27 years'";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);


$Quest1 = $row[0];



?>
<?php

$sql = "SELECT * FROM questions WHERE B = 'I have 27 years'";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);



$RDO1a = $row[1];
$RDO2a = $row[3];
$RDO3a = $row[2];
$RDO4a = $row[4];

?>



<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'By Taxi' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$Quest2 = $row[0];


?>


<?php

$sql = "SELECT * FROM questions WHERE B = 'By Taxi' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1b = $row[1];
$RDO2b = $row[2];
$RDO3b = $row[3];
$RDO4b = $row[4];


?>



<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'At least 7 years' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest3 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'At least 7 years' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1c = $row[1];
$RDO2c = $row[2];
$RDO3c = $row[3];
$RDO4c = $row[4];


?>



<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'some' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest4 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'some' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1d = $row[1];
$RDO2d = $row[2];
$RDO3d = $row[3];
$RDO4d = $row[4];


?>



<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'goes to' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest5 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'goes to' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1e = $row[1];
$RDO2e = $row[2];
$RDO3e = $row[3];
$RDO4e = $row[4];

?>



<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'in Wednesdays' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest6 = $row[0];

?>


<?php

$sql = "SELECT * FROM questions WHERE B = 'in Wednesdays' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);


$RDO1f = $row[1];
$RDO2f = $row[2];
$RDO3f = $row[3];
$RDO4f = $row[4];

?>


<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'Do you like?' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest7 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'Do you like?' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);


$RDO1g = $row[1];
$RDO2g = $row[2];
$RDO3g = $row[3];
$RDO4g = $row[4];


?>


<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'one white' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest8 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'one white' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1h = $row[1];
$RDO2h = $row[2];
$RDO3h= $row[3];
$RDO4h = $row[4];


?>




<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'has' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest9 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'has' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1i = $row[1];
$RDO2i = $row[2];
$RDO3i= $row[3];
$RDO4i = $row[4];

?>



<?php

$sql = "SELECT QUESTIONS FROM questions WHERE B = 'brothers or sisters' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$Quest10 = $row[0];

?>

<?php

$sql = "SELECT * FROM questions WHERE B = 'brothers or sisters' ";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);

$RDO1j = $row[1];
$RDO2j = $row[2];
$RDO3j= $row[3];
$RDO4j = $row[4];

?>

<div id = english>

<div class = "Top" > 
        <h3> COLLEGE ENTRANCE EXAMINATION </h3>

<div class = "Question">

    <h3 class = "Instruction"> <i> INSTRUCTION: Choose the right answer from the multiple choices. </h3> </i> </h3>
<center>    <h2> ENGLISH  </h2> </center>
    <center>     <table border = '0' style = "width: 70%">  </center>
    <hr> </hr>
<tr>
    <h3> <?php echo "1." . $Quest1; ?> </h3> 
    <td> <h4> <label class="container"> <?php echo $RDO1a; ?>
         <input type="radio" name="1">
    <span class="checkmark"></span> </hr>
    </label> </td> </h4>

     <td> <h4> <label class="container"> <?php echo $RDO3a; ?>
    <input type="radio" name="1">
     <span class="checkmark"></span>
    </label> </td> </h4>
</tr>
    <tr> 
        <td> <h4> <label class="container"> <?php echo $RDO2a; ?>
         <input type="radio" name="1">
    <span class="checkmark"></span> </hr>
    </label> </td> </h4>

 <td> <h4> <label class="container"> <?php echo $RDO4a; ?>
    <input type="radio" name="1">
     <span class="checkmark"></span>
    </label> </td> </h4>
</tr>
    </table>


    <center> <table border = '0' style = "width: 70%"> </center>
    <hr>
    <tr>
         <h3> <?php echo "2." . $Quest2; ?> </h3> 
         <td> <h4> <label class = "container">  <?php echo $RDO1b; ?>
        <input type = "radio" name="2">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class="container">  <?php echo $RDO2b; ?>
            <input type = "radio" name="2">
            <span class="checkmark"></span> </hr>
            </label> </td> </h4>
        </tr>
    <tr>
         <td> <h4> <label class="container">  <?php echo $RDO3b; ?>
            <input type = "radio" name="2">
            <span class="checkmark"></span> </hr>
            </label> </td> </h4>

         <td> <h4> <label class="container">  <?php echo $RDO4b; ?>
            <input type = "radio" name="2">
            <span class="checkmark"></span> </hr>
            </label> </td>  </h4>
    </tr>
</table>
<center> <table border = '0'  style = "width: 70%"> </center>

    <hr>
<tr>
         <h3> <?php echo "3." . $Quest3; ?> </h3> 
         <td> <h4> <label class = "container"> <?php echo $RDO1c; ?>
        <input type = "radio" name="3">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container"> <?php echo $RDO2c; ?>
        <input type = "radio" name="3">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
</tr>
    <tr>    
         <td> <h4> <label class = "container">  <?php echo $RDO3c; ?>
        <input type = "radio" name="3">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container">  <?php echo $RDO4c; ?>
        <input type = "radio" name="3">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
</tr>
</table> 


<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>
         <h3> <?php echo "4." . $Quest4; ?> </h3> 

     <td> <h4> <label class = "container"> <?php echo $RDO1d; ?>
        <input type = "radio" name="4">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

 <td> <h4> <label class = "container"> <?php echo $RDO2d; ?>
        <input type = "radio" name="4">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
</tr>

        <tr>
            <td> <h4> <label class = "container"><?php echo $RDO3d; ?>
        <input type = "radio" name="4">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

            <td> <h4> <label class = "container"> <?php echo $RDO4d; ?>
        <input type = "radio" name="4">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4> 
</tr>
    </table>

<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>
         <h3> <?php echo "5." . $Quest5; ?> </h3> 

         <td> <h4> <label class = "container"> <?php echo $RDO1e; ?>
        <input type = "radio" name="5">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container"> <?php echo $RDO2e; ?>
        <input type = "radio" name="5">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
    </tr>
    <tr>
         <td> <h4> <label class = "container"> <?php echo $RDO3e; ?>
        <input type = "radio" name="5">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container"> <?php echo $RDO4e; ?>
        <input type = "radio" name="5">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
</tr>
</table> 



<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>
         <h3> <?php echo "6." . $Quest6; ?> </h3> 

         <td> <h4> <label class = "container"> <?php echo $RDO1f; ?>
        <input type = "radio" name="6">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container"> <?php echo $RDO2f; ?>
        <input type = "radio" name="6">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
    </tr>
    <tr>
         <td> <h4> <label class = "container"> <?php echo $RDO3f; ?>
        <input type = "radio" name="6">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container"> <?php echo $RDO4f; ?>
        <input type = "radio" name="6">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
    </tr>
</table>


<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>

         <h3> <?php echo "7." . $Quest7; ?> </h3> 

         <td> <h4> <label class = "container"> <?php echo $RDO1g; ?>
        <input type = "radio" name="7">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>

         <td> <h4> <label class = "container"> <?php echo $RDO2g; ?>
        <input type = "radio" name="7">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>
    </tr>
    <tr>

         <td> <h4> <label class = "container"> <?php echo $RDO3g; ?>
        <input type = "radio" name="7">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>

         <td>  <h4> <label class = "container"> <?php echo $RDO4g; ?>
        <input type = "radio" name="7">
        <span class="checkmark"></span> </hr>
        </label> </td> </h4>


<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>
         <h3> <?php echo "8." . $Quest8; ?> </h3> 

         <td> <h4> <label class = "container"> <?php echo $RDO1h; ?>
        <input type = "radio" name="8">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>

        <td> <h4> <label class = "container"> <?php echo $RDO2h; ?>
        <input type = "radio" name="8">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>
</tr>
<tr>
    <td> <h4> <label class = "container"> <?php echo $RDO3h; ?>
        <input type = "radio" name="8">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>

        <td> <h4> <label class = "container"> <?php echo $RDO4h; ?>
        <input type = "radio" name="8">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>
</tr>
</table>

<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>
         <h3><?php echo "9." . $Quest9; ?> </h3> 

         <td> <h4> <label class = "container">  <?php echo $RDO1i; ?>
        <input type = "radio" name="9">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>

        <td> <h4> <label class = "container">  <?php echo $RDO2i; ?>
        <input type = "radio" name="9">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>
    </tr>
    <tr>
        <td> <h4> <label class = "container"> <?php echo $RDO3i; ?>
        <input type = "radio" name="9">
        <span class="checkmark"></span> </h4>
        </label> </td> </h4>

        <td> <h4> <label class = "container">  <?php echo $RDO4i; ?>
        <input type = "radio" name="9">
        <span class="checkmark"></span> 
        </label> </h4> </td> </tr>
</table> 


<center> <table border = '0' style = "width: 70%"> </center>
    <hr>
<tr>
         <h3> <?php echo "10." . $Quest10; ?> </h3> 

         <td> <h4> <label class = "container"> <?php echo $RDO1j; ?>
        <input type = "radio" name="10">
        <span class="checkmark"></span> 
        </label> </h4> </td>

        <td> <h4> <label class = "container"> <?php echo $RDO2j; ?>
        <input type = "radio" name="10">
        <span class="checkmark"></span> 
        </label> </h4> </td> </tr>

        <td> <h4> <label class = "container"> <?php echo $RDO3j; ?>
        <input type = "radio" name="10">
        <span class="checkmark"></span> 
        </label> </h4> </td>

        <td> <h4> <label class = "container"> <?php echo $RDO4j; ?>
        <input type = "radio" name="10">
        <span class="checkmark"></span> 
        </label> </h4> </td>
    </tr>
</table>

</div>
<?php

$sql = "SELECT QUESTIONS FROM questionsmath WHERE B = '-5'";
$result = mysqli_query($conn,$sql);
$resultCheck = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);


$Quest1M = $row[0];



?>

<script>
var modal = document.getElementById("myModal");
var modalLast = document.getElementById("myModalLast");
var btn = document.getElementById("submit");
var btnYes = document.getElementById("yes");
var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
  modalLast.style.display = "none";
}
// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
no.onclick = function() {
  modal.style.display = "none";
}
btnYes.onclick = function() {
  modalLast.style.display = "block";
  modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
  else if (event.target == modalLast){
    modalLast.style.display = "false";
  }
}
</script>
</html>
0
eunhell mendoza 29 Maret 2020, 11:23

1 menjawab

Jawaban Terbaik

Anda harus memposting hanya kode yang penting untuk pertanyaan. Memposting kode php/js lengkap hanya akan mengalihkan perhatian kita dan mencegah orang menemukan masalah dan membantu.

Mengatakan itu, dan seperti yang saya tunjukkan dalam komentar saya untuk pertanyaan Anda, jika Anda tidak keberatan membiarkan javascript "tahu" jawaban mana yang benar dan mana yang salah, maka Anda cukup memberikan jawaban yang benar sebuah kelas, seperti correct dan nanti saat formulir dikirimkan, Anda dapat menghitung semua tombol radio yang diperiksa dan ditandai sebagai benar di belakang layar.

Saya tidak yakin mengapa Anda ingin melakukannya di sisi klien, karena kode markup HTML dapat diperiksa, dan orang dapat mengetahui jawaban mana yang benar. Tetapi jika itu bukan masalah bagi Anda, maka inilah salah satu cara untuk mencapainya:

document.getElementById("btn-check").addEventListener("click", function() {
  let numberOfCorrectAnswers = document.querySelectorAll("input[type=radio].correct:checked").length;
  console.log("Correct Answers: " + numberOfCorrectAnswers);
});
.question {
  display: block;
  color: navy;
  background-color: #def;
}
<p>
  <div class='question'>What is the sum of 11+33?</div>
  <label><input class='wrong' type='radio' name='q1' />45</label><br />
  <label><input class='correct' type='radio' name='q1' />44</label><br />
  <label><input class='wrong' type='radio' name='q1' />47</label><br />
  <label><input class='wrong' type='radio' name='q1' />42</label>
</p>

<p>
  <div class='question'>What is the sum of 3+5?</div>
  <label><input class='wrong' type='radio' name='q2' />2</label><br />
  <label><input class='wrong' type='radio' name='q2' />4</label><br />
  <label><input class='wrong' type='radio' name='q2' />6</label><br />
  <label><input class='correct' type='radio' name='q2' />8</label>
</p>

<button id='btn-check'>Check Number of Correct Answers</button>
0
Ahmad 29 Maret 2020, 08:56