[英]Getting value of select option php
I want to get a value of select option php on the same page.我想在同一页面上获取选择选项 php 的值。 I brought selection list from database and listed them on the select.
我从数据库中带来了选择列表并将它们列在了选择中。 But even though I choose something on the list, it's not posted.
但即使我在列表中选择了一些东西,它也没有发布。 Is there any problem in the code below?
下面的代码有问题吗? Thanks in advance.
提前致谢。
<div class="ibox-content m-b-sm border-bottom">
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label class="control-label" for="status">Search by category</label>
<form action="" method="POST" >
<select name="category" class="form-control">
<option value="" selected>All</option>
<?php
while($category = mysqli_fetch_array($result1)) {
// output data from each row
echo "<option value=\"{$category['categoryID']}\">{$category['name']}</option>";
}
?>
</select>
<input class="btn btn-primary" type="submit" name="submit" value="Search">
</input>
</form>
</div>
</div>
</div>
</div>
<?php
if (isset($_POST['submit'])) {
echo "Got it!";
echo $_POST['category'];
}?>
You should specify a name for the option:您应该为该选项指定一个名称:
<option name='category' value=\"{$category['categoryID']}\"...
And try adding action='#'
, and then it works:并尝试添加
action='#'
,然后它就起作用了:
array (size=2)
'category' => string '1' (length=1)
'submit' => string 'Search' (length=6)
Got it!1
And take care, in your code the option for 'all' has no value, and won't be displayed.请注意,在您的代码中,'all' 的选项没有价值,并且不会显示。
You have:你有:
<?php
while($category = mysqli_fetch_array($result1)) {
// output data from each row
echo "<option value=\"{$category['categoryID']}\">{$category['name']}</option>";
}
?>
An array key -> inside a template var -> inside an option -> in a double-quoted string (which will parse vars), -> inside a while loop (which may or may not be running).数组键 -> 模板 var 内 -> 选项内 -> 双引号字符串(将解析变量),-> while 循环内(可能正在运行,也可能未运行)。 That level of nested complexity will certainly decrease your time-to-debug and simplicity-to-debug.
这种级别的嵌套复杂性肯定会减少您的调试时间和调试简单性。
I would first ignore the html entirely and:我首先会完全忽略 html 并且:
$category = mysqli_fetch_array($result1)
var_dump($category, $category['categoryID'], $category['name']);
in naked php, to make sure that you have exactly what you need before you put anything into the html.在裸 php 中,在将任何内容放入 html 之前,确保您拥有所需的内容。
Also, form action='' sometimes causes problems, so make that and everything else you can explicit and fixed just while debugging.此外, form action='' 有时会导致问题,因此请在调试时制作它以及您可以明确和修复的所有其他内容。
I have to say that you're making things harder for yourself by not using a template engine, especially at the early stages.我不得不说,您不使用模板引擎让事情变得更加困难,尤其是在早期阶段。 Your html+php is low-readability, and that decreases the ease of debugging.
您的 html+php 可读性低,这降低了调试的便利性。 Use a clean template and it'll be easier to debug echoing issues, and as a side benefit you'll get easy escaping.
使用干净的模板,调试回显问题会更容易,而且作为附带好处,您可以轻松逃脱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.