Why is $_POST[''] not working with <form>
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>
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
PHP: $_POST, $_POST. $HTTP_POST_VARS [deprecated]. (PHP 4 >= 4.1.0, PHP 5, PHP 7). $_ POST --� PHP $_POST. PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.
The purpose of <input type="image" name="keuzen">
is to provide a server side image map. It submits the form as a side effect, but the purpose is to select coordinates on the image.
When you click it, x and y coordinates will be sent to the server.
If I remember correctly, you'll get something like keuzen.y=123&keuzen.x=456
which PHP will use to populate $_POST['keuzen_x']
and $_POST['keuzen_y']
but not $_POST['keuzen']
.
If you do a var_dump($_POST)
you can see the data structure you actually get.
If you want to just have regular submit buttons which look like images, then use regular submit buttons containing images:
<button name="keuzen" value="steen"><img src="steen.jpg" alt="steen"></button>
You can style away the borders and background of the button element with CSS if you like.
PHP $_POST, PHP $_POST. PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". The $_POST Variable. The $_POST variable is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. Example
- Change names of inputs like this:
name="keuzen[steen]"
and remove value attributes. - Read POSTed value like this:
$keuzen = array_keys($_POST['keuzen'])[0];
You can also verify that the POSTed value is not empty.
PHP $_POST, PHP $_POST. PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". The problem is that you're checking if $_POST['send'] is set, but it won't be. The "send" field in your form is the button, but the button doesn't have a value, so it won't be included in $_POST. Hope that helps.
$_GET,$_POST, and $_REQUEST, $_GET, $_POST, and $_REQUEST. $_GET Variable. What is it? The $_GET variable is used to get data from a form that is written in HTML. Also in the url� The $_POST variable is also used to collect data from forms, but the $_POST is slightly different because in $_GET it displayed the data in the url and $_POST does not. The data that $_POST gets, is invisible to others and the amount that can be sent is not limited besides the 8MB max size.
What is the purpose of $_POST?, You may want to read up on the basics of PHP. Try reading some starter tutorials. $_POST is a variable used to grab data sent through a web� POST create an array which holds key/value pairs, where keys are the names of the form controls and values are the input data from the user. $_POST is superglobal, which means that it always accessible, regardless of scope - and you can access the
PHP - GET & POST Methods, The PHP provides $_GET associative array to access all the sent information using GET method. Try out following example by putting the source code in test. php� Hi, I have the following code that I want to run only when the “Submit” button is pressed…but a message is echoed even when it has not been clicked…I can’t work out what I need to amend
Comments
- Replace
action="RockPaperScissor.php"
with simplyaction=""
. Does it work? - Can you change name="keuzen" to name="keuzen[]"
- Start simple debugging with
var_dump($_POST)
… - This did not work for me.
- Just a note. As @Quentin pointed below,
<input type="image">
is sending X/Y coordinates of the image itself upon POST, but not it's value. Bear in mind when playing with such type within a form.