我有一个简单的问题 的 test.php的
<form name ="myForm" method="post" id="back" action="homepage1.php">
Auth:<select name="category" id="category">
<option>1234</option>
<option>4231</option>
<option selected="selected">
</option>
<option></option>
</select>
<input type="submit" id ="submit" name="submit" value = "submit" >
</form>
homepage1.php
<?php
$selectedid = $_POST['category'];
echo $selectedid;
?>
错误:未定义的索引:第121行的E:\ xampp \ htdocs \ website \ homepage1.php中的类别
任何人都可以告诉我我做错了什么。感谢
答案 0 :(得分:1)
添加提交按钮以发布表单,如果您只是重定向或打开homepage1.php而不发布,您将收到此错误,因为没有&#34;类别&#34;在$ _POST表单中。您可以详细了解POST and how prevent empty post here。
此外,您还需要为这些选项添加值:
<option value="4231">4231</option>
更完整的例子:
HTML:
<form name ="myForm" method="post" id="back" action="homepage1.php">
Auth:
<select name="category" id="category">
<option value="1234">1234</option>
<option value="4231">4231</option>
<option value="45698" selected="selected">45698</option>
<option value="9874">9874</option>
</select>
<input type="submit" name="btnSubmit" value = "Go!" >
</form>
PHP:
//Check if its a empty post
if(!empty($_POST)){
$selectedid = $_POST['category'];
echo $selectedid;
}
答案 1 :(得分:1)
1.选择框选项没有任何value
属性,因此请按以下方式添加: -
<form name ="myForm" method="post" id="back" action="homepage1.php">
Auth:<select name="category" id="category">
<option value="1234">1234</option><!-- add option value-->
<option value="4231">4231</option>
</select>
<input type="submit" id ="submit" name="mysubmit" value = "submit" ><!--change button name to something else -->
</form>
2.更改homepage1.php
代码,如下所示: -
<?php
//comment these two error reporting lines when code started working fine
error_reporting(E_ALL);
ini_set('display_errors',1);
if(!empty($_POST['category'])){
$selectedid = $_POST['category'];
echo $selectedid;
}
?>