0

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>
Maresh
  • 4,644
  • 25
  • 30
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • In the good old times, what I used to do is add a hidden field in the form, the test for its existence with isset(); That way you know if you're arriving on the form for the first time, or because of the submit. Also, be aware that POST and GET are 2 different methods... here your form is setup as GET , you'll need to use $_GET['here'] , while $_POST['here'] corresponds to nothing. – Laurent S. Jun 10 '13 at 14:09

2 Answers2

1

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.
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • Wow. That was simple. Question though, when I press submit why does the link show "?user=&here=1"? Why does that happen? – EGHDK Jun 10 '13 at 14:13
  • 2
    Thats because you are submitting the form using the `get` method. get means that values will be appended to the url. you should use post instead where variables will be put to the request body. check [this](http://www.w3schools.com/tags/ref_httpmethods.asp) – hek2mgl Jun 10 '13 at 14:15
  • Oh. This might stem from the fact that I didn't know which values could be used for "method". What do you recommend? Just use post as the method? – EGHDK Jun 10 '13 at 14:16
  • `POST` method is recommended. Nobody see your data in url and probaby `POST` can send more data (ex. very long text). See: [maximum length of HTTP GET request?](http://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request) – furas Jun 10 '13 at 14:22
0

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">
Shahbaz
  • 3,433
  • 1
  • 26
  • 43
  • 1
    OP use GET method in form so `$_POST` is useless in your answer. – furas Jun 10 '13 at 14:14
  • If you add name to button (`submit1`) and check `isset($_GET['submit1'])` it doesn't meen that `isset($_GET['here'])` is `true`. – furas Jun 10 '13 at 14:17