【发布时间】:2014-03-14 20:30:47
【问题描述】:
由于某种原因,我可以使 $_POST 与输入完美配合,但不能与 select 语句配合使用。这是我的代码:
registration.php
<form class="form-horizontal" action="results.php" method="post">
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Type</label>
<div class="col-md-4">
<select id="user_type" name='user_type' class="form-control">
<option selected="" value="<?php if (isset($user_type)) { echo 'admin'; } ?>">Faculty</option>
<option value="<?php if (isset($user_type)) { echo 'registeredUser'; } ?>">Student</option>
</select>
</div>
</div>
<!-- Submit Button -->
<div class="form-group">
<div class="col-md-12">
<button id="submit" name="submit" class="btn btn-primary center-block"> <span class="k-icon k-si-plus"></span>Submit</button>
</div>
</div>
</form>
结果.php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Setting the form inputs to variables
$first_name = trim($_POST["first_name"]);
$last_name = trim($_POST["last_name"]);
$email_address = trim($_POST["email_address"]);
if (isset($_POST["submit"])) {
$user_type = $_POST['user_type'];
echo $user_type . '<br><br>';
}
echo $first_name . '<br><br>';
echo $last_name . '<br><br>';
echo $email_address . '<br><br>';
}
输出:
亚历克斯
科里
alex@mydomain.com
关于为什么这个 $_POST['user_type']; 的任何想法不工作?
【问题讨论】:
-
你的 HTML 中有 吗?我建议你做 var_dump($_POST);查看 $_POST 实际包含的内容。
-
确定 user_type 在 html 中有任何值吗?
-
好吧,我确实尝试过没有值中的 php,又名: 但仍然没有输出。
-
我猜我会说在您的注册脚本中没有设置变量 $user_type 。由于未设置,因此您的两个选项的值设置为空字符串,这可以解释为什么该变量在 results.php 中为空。尝试使用浏览器检查器检查您的选项是否实际包含一个值。
-
这是一个显示检查器结果的屏幕截图。 imageshack.us/a/img546/631/ujpu.png 我觉得 $user_type 变量不起作用的奇怪之处在于,为什么它们不能在
标签: php html forms select post