简体   繁体   中英

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. I have a specific form in the HTML that I'd like to put the list in.

<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. As it is right now, makeList() returns an empty list. 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. It's when I call it in the HTML that it returns an empty list.

Depending on what your parameters ($parameter1 and $parameter2) are this should work.

 <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.

If it were up to me, I'd define the PHP function at the stop of my script. Be careful to balance your opening and closing PHP tags. 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.

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. 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.
  • Your function refers to a variable, $result , that you have not bothered to define. 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
  • 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

So after that script above, you might have something like this:

 <body>
    <form>
        // some text inputs


        <?php makeList($parameter1, $parameter2); ?>

        // submit button
    </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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