0

我不经常使用表单,因此将表单选择传递到同一页面这样微不足道的事情似乎很有挑战性。我已经检查了一堆stackoverflow关于这个的问题,但他们从来没有说在哪里放置_POST代码或者他们将代码发布到另一个页面。我知道我做错了事,这可能很简单。有人可以帮帮我吗?

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>
4

2 回答 2

1

你错过了给<select>一个名字。用这个:

<select name="here">

表单元素的 name 属性的值将是$_GET数组中的索引。另请注意:

$_POST['here'];

只是毫无意义。我不确定您对此有何期待。删除该行。

如果代码像您展示的那样简单,您还可以删除此行:

session_start();

进一步注意,在任何情况下,您都应该在使用之前检查一个值是否已正确提交:

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.
于 2013-06-10T14:06:57.420 回答
0

你做得对,但你必须做得更多

if(isset($_REQUEST['submit1'])){

$_POST['here'];
$hello = $_GET['here'];

echo $hello;

}

记住一件事,给按钮起个名字

<input type="submit" value="Submit" name="submit1">
于 2013-06-10T14:11:51.507 回答