skip to Main Content

I’m new to PHP so I am in need of your help.

My table in web is in a loop so it gets values in my mySQL phpMyAdmin database until it reaches the end of the database table.

I have a problem getting the desired value I want to show up in my table when certain button is pressed. Once I pressed a single button, it actually shows up the value of it, BUT, IT SHOWS THE OTHER BUTTON’S VALUE AS WELL.

In my conclusion, I think my $_POST code is missing something or is a complete mistake.

The code is here:

<tr>
   <?php
       while($row = mysqli_fetch_assoc($query)){
           $exerciseid = $row['exerciseid'];
            $question = $row['question'];
            $a = $row['answera'];
            $b = $row['answerb'];
            $c = $row['answerc'];
            $correctanswer = $row['answer'];
    ?>
</tr>
<tr>
    <td><?php echo $question?></td>
    <td><button class="btn btn-primary" value="<?php echo $a?>" name="a-submit">A. <?php echo $a?></button></td>
    <td><button class="btn btn-primary" value="<?php echo $b?>" name="b-submit">B. <?php echo $b?></button></td>
    <td><button class="btn btn-primary" value="<?php echo $c?>" name="c-submit">C. <?php echo $c?></button></td>
    <td><?php 
        if($_SERVER['REQUEST_METHOD'] === 'POST'){
            if(isset($_POST['a-submit'])){
                echo $a;
            }
            else if(isset($_POST['b-submit'])){
                echo $b;
            }
            else if(isset($_POST['c-submit'])){
                echo $c;
            }
            else{
            }
        }
    ?>
    </td>
</tr>
    <?php
    }
    ?>

What do you think is the possible cause of my mistake? Please help

2

Answers


  1. If I am not wrong, all your buttons are in single form that’s why you are getting all the buttons value when you click any button.

    I am not sure why you need post here because, if you want to get the button value you can simply get it using jQuery.

    Check my sample code.

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
    
    <?php
    for ($i = 1; $i < 5; $i++) {
        ?>
        <table>
            <tr>
                <td>Question <?php echo $i; ?></td>
                <td><button class="btn btn-primary btn-answer" value="Answer 1" rel="question<?php echo $i; ?>" name="a-submit">A. Answer 1</button></td>
                <td><button class="btn btn-primary btn-answer" value="Answer 2" rel="question<?php echo $i; ?>" name="b-submit">B. Answer 2</button></td>
                <td><button class="btn btn-primary btn-answer" value="Answer 3" rel="question<?php echo $i; ?>" name="c-submit">C. Answer 3</button></td>
                <td class="question<?php echo $i; ?>-answ"></td>
            </tr>
        </table>
        <?php
    }
    ?>
    
    <script type="text/javascript">
        $(document).ready(function () {
            $('.btn-answer').on('click', function () {
                $('.' + $(this).attr('rel') + '-answ').html($(this).val());
            });
        });
    </script>
    

    class “btn-answer” -> global class for all answer buttons to call jquery click action

    rel -> to identify the question

    question<?php echo $i; ?>-answ -> to identify the answer block the show the user selection

    Login or Signup to reply.
  2. If you still want to use the same variable name within a loop, and display the value that was submitted by user, you could do it like below :

    <tr>
        <td><?php echo $question?></td>
        <td><button class="btn btn-primary" value="<?php echo $a?>" name="a-submit">A. <?php echo $a?></button></td>
        <td><button class="btn btn-primary" value="<?php echo $b?>" name="b-submit">B. <?php echo $b?></button></td>
        <td><button class="btn btn-primary" value="<?php echo $c?>" name="c-submit">C. <?php echo $c?></button></td>
        <td><?php 
            if($_SERVER['REQUEST_METHOD'] === 'POST'){
                if(isset($_POST["a-submit"]) && $_POST["a-submit"] == $a){
                    echo $a;
                }
                else if(isset($_POST["b-submit"]) && $_POST["b-submit"] == $b){
                    echo $b;
                }
                else if(isset($_POST["c-submit"]) && $_POST["c-submit"] == $c){
                    echo $c;
                }
                else{
                }
            }
            ?>
        </td>
    </tr>
    

    But this will not prevent multiple answer from being shown because no unique id been given on each variable (for example if there are question which having the same answer).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search