Why is $_POST[''] not working with <form>

Maki :

This is a Rock Paper Scissors game and I am just starting on it, but it will not display the $_POST['keuzen'].

So I don't get why this is not working. Tried many things but can't find a good solution.

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>RockPaperScissor</title>
    </head>
    <body>
        <form action="RockPaperScissor.php" method="post">
            <input type="image" src="steen.jpg" alt="steen" name="keuzen" value="steen" title="Steen">
            <input type="image" src="papier.jpg" alt="papier" name="keuzen" value="papier" title="Papier">
            <input type="image" src="schaar.jpg" alt="schaar" name="keuzen" value="schaar" title="Schaar">
        </form>
        <?php
            if (isset($_POST['keuzen'])) {
                $keuzen = $_POST['keuzen'];
                $kiezenuit = array("steen", "papier", "schaar");
                $random = rand(0, 2);
                $computer = $kiezenuit[$random];

                echo 'jij koos ' . $keuzen . '<br>';
                echo 'De computer koos ' . $computer . '<br';

                if ($keuzen == $computer) {
                    echo 'Resultaat : Draw ';
                }
            }
        ?>
    </body>
</html>
mitkosoft :

Assuming the form is in the same RockPaperScissor.php page, better introduce a hidden field that takes the value of a clicked image, but remove the name tag from the images itself:

<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>RockPaperScissor</title>
    </head>
    <body>
        <form action="RockPaperScissor.php" method="post">
            <input type="hidden" name="keuzen" value="">
            <input type="image" src="steen.jpg" alt="steen" value="steen" title="Steen" onclick="document.forms[0].keuzen.value=this.value">
            <input type="image" src="papier.jpg" alt="papier" value="papier" title="Papier" onclick="document.forms[0].keuzen.value=this.value">
            <input type="image" src="schaar.jpg" alt="schaar" value="schaar" title="Schaar" onclick="document.forms[0].keuzen.value=this.value">
        </form>
        <?php
            if (!empty($_POST['keuzen'])) {
                $keuzen = $_POST['keuzen'];
                $kiezenuit = array("steen", "papier", "schaar");
                $random = rand(0, 2);
                $computer = $kiezenuit[$random];

                echo 'jij koos ' . $keuzen . '<br>';
                echo 'De computer koos ' . $computer . '<br>';

                if ($keuzen == $computer) {
                    echo 'Resultaat : Draw ';
                }
            }
        ?>
    </body>
</html>

Upon POST, you will get the following:

Array
(
    [keuzen] => schaar
    [x] => 7
    [y] => 3
)

jij koos schaar
De computer koos schaar
Resultaat : Draw    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=9806&siteId=1