[英]How can I place a drop-down list made in php in a specific HTML form?
I have some php that connects to a database and creates a drop down list. 我有一些PHP连接到数据库并创建一个下拉列表。 I have a specific form in the HTML that I'd like to put the list in. 我想将列表放入HTML中的特定表格。
<body>
<form>
// some text inputs
// where i'd like the drop down to go
<?php makeList(parameter1, parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList(arg1, arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option">;
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>
The only languages I'm allowed to use (apart from the sql) are php, html and javascript. 我被允许使用的唯一语言(除了sql)是php,html和javascript。 As it is right now, makeList() returns an empty list. 现在,makeList()返回一个空列表。 When I include opening and closing form tags in the function it returns a fully functional list, but then it acts as it's own form and I need to to be a part of the original form. 当我在函数中包括打开和关闭表单标签时,它会返回一个功能完整的列表,但随后它将作为其自己的表单运行,因此我需要成为原始表单的一部分。 Thanks. 谢谢。
EDIT: Sorry, forgot to mention the makeList function works fine when called within the php tags. 编辑:对不起,忘记提及makeList函数在php标记内调用时效果很好。 It's when I call it in the HTML that it returns an empty list. 当我在HTML中调用它时,它返回一个空列表。
Depending on what your parameters ($parameter1 and $parameter2) are this should work. 根据您的参数($ parameter1和$ parameter2),这应该可以工作。
<body>
<form>
// some text inputs
<?php echo makeList($parameter1, $parameter2); ?>
// submit button
</form>
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option>";
echo $row[$column];
echo "</option>";
echo '</select>';
}
</body>
Firstly, you have some syntax issues with your script. 首先,您的脚本存在语法问题。 It's not a valid HTML file, not a valid PHP file, and not a valid JS file. 这不是有效的HTML文件,不是有效的PHP文件,也不是有效的JS文件。
If it were up to me, I'd define the PHP function at the stop of my script. 如果由我决定,我将在脚本的结尾定义PHP函数。 Be careful to balance your opening and closing PHP tags. 请注意平衡您的打开和关闭PHP标签。 Something like this: 像这样:
<?php
// connect to database
function makeList($arg1, $arg2) {
echo '<select>';
while ($row = mysqli_fetch_array($result)){
echo "<option">;
echo $row[$column];
echo "</option>";
echo '</select>';
}
?>
And only after that would I start to output my HTML. 而且只有在那之后,我才开始输出我的HTML。
Now there are a couple of important things to note about that script I just posted: 关于我刚刚发布的脚本,现在有几件重要的事情要注意:
So after that script above, you might have something like this: 因此,在执行完上述脚本之后,您可能会遇到以下情况:
<body>
<form>
// some text inputs
<?php makeList($parameter1, $parameter2); ?>
// submit button
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.