-1

I would like to display count of correct answers, but my page is reloading after submit.Code is comparing data after load, not input data. How I should prevent to not refresh/reload page after submit and just display results. I do not want you use Javascript or Jquery

if use How to avoid page reload in php form submission after submit my page, just redirect to new tab, nothing else happen and I will receive new questions and choice to play the game. Looks like there is problem with foreach loop, that it is looping every time submit is processed.

How I can disable refreshing page after submit?

<?php
   include "sql.php";
?>

<body>
    <form method="POST" >
    <ul>
        <?php foreach($list as $key=>$value) :?>
        <?php 

        $option = [$value['capitol'], $value['choice1'], $value['choice2']];
        shuffle($option);
        ?>
        <li> <h5>What is capitol city of <?php echo $value['country']."?";?></h5> </li>
            <input type="radio" name="<?=$value['id']?>" value="<?=$option[0]?>" required> <?=$option[0]?>
            <br>
            <input type="radio" name="<?=$value['id']?>" value="<?=$option[1]?>" > <?=$option[1]?>
            <br>
            <input type="radio" name="<?=$value['id']?>" value="<?=$option[2]?>" > <?=$option[2]?>
            <br>
        <?php endforeach;?>
    </ul>
    <input type="submit" name='submit'>
    </form>
  </body>
  <?php
        if(isset($_POST['submit'])){
            $correct=0;

            foreach($list as $key=>$value){
                $answer = $_POST[$value['id']];

                if($answer == $value['capitol'] ){
                    $correct++;

                }
            }

            echo "Correct answers " .$correct;

        }

    ?>
</html>


NKU
  • 13
  • 4
  • 1
    Not possible without javascript. The page reload can't be prevented using only PHP. – Muhammad Tashfeen Sep 13 '19 at 14:51
  • @MuhammadTashfeen thanks for answer, what you recommend me to do in this case? – NKU Sep 15 '19 at 12:34
  • 1
    use javascript like he said. – Bijay Regmi Sep 15 '19 at 16:28
  • Possible duplicate of the question [how to avoid page reload in php form submission](https://stackoverflow.com/questions/23196423/how-to-avoid-page-reload-in-php-form-submission) – Parth Manaktala Sep 15 '19 at 18:38
  • Possible duplicate of [How to avoid page reload in php form submission](https://stackoverflow.com/questions/23196423/how-to-avoid-page-reload-in-php-form-submission) – Parth Manaktala Sep 15 '19 at 18:39
  • @NKU you can set a event on submit button to call a javascript function which checks for the answer if it is correct or not. I will share the static version in the answer below. You can adapt it according to your logic. – Muhammad Tashfeen Sep 16 '19 at 08:44

1 Answers1

0

This is how you will stop the form submit and calculate the result.

<form onsubmit="return false">
<input type="radio" name="city" value="toronto"> Toronto<br>
<input type="radio" name="city" value="calgiri"> Calgiri<br>
<button type="submit" onclick="checkAnswer()">Send</button>
</form>

<script>
function checkAnswer() {
    let answer = document.querySelector('input[name=city]:checked').value
    alert(answer);
}
</script>