简体   繁体   English

如何以特定的HTML格式放置php制成的下拉列表?

[英]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: 关于我刚刚发布的脚本,现在有几件重要的事情要注意:

  • the database code is not in here...I don't see any connection or query getting run or anything 数据库代码不在这里...我看不到任何连接或查询正在运行
  • In your script, this function doesn't look valid. 在您的脚本中,此功能无效。 arg1 and arg2 need a $ in front of each to be a valid PHP function. arg1和arg2必须在每个$前面加一个$才能成为有效的PHP函数。 If it's a JS function you want then well, you are very confused and probably need to go back and figure out why this is not a valid JS function. 如果您想要的是JS函数,那么您会很困惑,可能需要回过头来弄清楚为什么这不是有效的JS函数。
  • Your function refers to a variable, $result , that you have not bothered to define. 您的函数引用的是变量$ result ,您无需费心定义。 It is not mentioned anywhere else in your script. 在脚本的其他任何地方都没有提到它。 It is most certainly not mentioned anywhere inside your function. 当然,函数内的任何地方都没有提到它。 For $result to be defined inside your function, you either need to pass it in as an array or declare it as a global: global $result 为了在函数中定义$ result,您需要将其作为数组传递或声明为全局:global $ result
  • Your function doesn't return anything at all. 您的函数根本不返回任何东西。 It just echoes stuff. 它只是回声。 This doesn't mean you can't use it, but it does mean that the function has no return value. 这并不意味着您不能使用它,而是意味着该函数没有返回值。 Echoing the result of makeList won't output anything at all 回显makeList的结果根本不会输出任何内容

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM