简体   繁体   中英

Passing two variables via GET to PHP using HTML form

I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.

Obviously this method doesn't work:

<h3>Search for a Customer</h3>
<form action="search.php" method="get">
    <input type="text" id="sString">
    <select name="sField">
        <option value="Name">Name</option>
        <option value="HostID">Host ID</option>
        <option value="OrderNumber">Order Number</option>
        <option value="Theme">Theme</option>
    </select>
    <input type="submit" value="submit"/>
</form>

You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.

You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:

<input type="text" id="sString" name="sString">

and then in PHP

$search = $_GET['sString'];

You need to use attribute name instead of value .

<h3>Search for a Customer</h3>
<form action="search.php" method="get">
    <input type="text" id="sString" name="sString"/>
    <select name="sField">
        <option name="Name">Name</option>
        <option name="HostID">Host ID</option>
        <option name="OrderNumber">Order Number</option>
        <option name="Theme">Theme</option>
    </select>
    <input type="submit" value="submit"/>
</form>

Try:

<form action="search.php" method="get">
    <input type="text" name="sString" id="sString" value="">
    <select name="sField">
        <option value="Name">Name</option>
        <option value="HostID">Host ID</option>
        <option value="OrderNumber">Order Number</option>
        <option value="Theme">Theme</option>
    </select>
    <input type="submit" value="submit"/>
</form>

You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);

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