I don't often work with forms, so something as trivial as passing a form selection into the same page seems quite challenging. I've checked a bunch of stackoverflow's questions on this, but they never say where to place the _POST
code or they post the code to another page. I know I'm doing something the wrong way, that's probably pretty simple. Can anybody help me out?
numbersPage.php
<?php
session_start();
$_POST['here'];
$hello = $_GET['here'];
echo $hello;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<form name="input" action="numbersPage.php" method="get">
Fav Number:
<select id="">
<optgroup label="Numbers">
<option value="1" name="thing">1</option>
<option value="2" name="thing">2</option>
<option value="3" name="thing">3</option>
<option value="4" name="thing">4</option>
<option value="5" name="thing">5</option>
</optgroup>
</select>
<input type="submit" value="Submit">
</form>
</body>
</html>
You missed to give the <select>
a name. use this:
<select name="here">
The value of the name attribute of a form element will be the index in the $_GET
array. Also note that this:
$_POST['here'];
is just pointless. I'm not sure what you are expecting from that. Remove that line.
If the code is simple as you showed you also remove this line:
session_start();
Further note that you should in any case check if a value has been properly submitted before using it:
if(!isset($_GET['here'])) {
die('GET:here is missing');
} else {
$here = $_GET['here'];
// the value has been submitted. Now validate it!
if(!is_valid($here)) {
die('Bad input');
}
}
// further processing.
you are doing right but you have to do more like
if(isset($_REQUEST['submit1'])){
$_POST['here'];
$hello = $_GET['here'];
echo $hello;
}
remember one more thing, give a name to button
<input type="submit" value="Submit" name="submit1">