faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 38 people (45%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I find out which option a user selected in a SELECT form?

Jul 8th, 1999 21:46
Nathan Wallace, Troels Arvin


Try playing around with this script to see how things work:

    <html><head><title>Option test</title></head>
    <body>

    <form action="<?php print $PHP_SELF ?>">
    <select name="var">  <!-- Note: Variable name defined here -->
    <option value="">Please select</option>
    <option>a</option>
    <option>b</option>
    <option>c</option>
    </select>
    <input type="submit" name="submit">
    </form>

    <?php

    // If the user did not submit the form then output no more
    if ($submit) {
        print "<hr>Value of <code>var</code>: ";

        if (empty($var))
            print "(empty)";
        else
            print $var;
    }

    ?>

    </body></html>