简体   繁体   中英

Form data and Button in the same _POST

Evening!

I'm having a spot of an issue figuring out how to get a particular section of code to work, I have this:

   <form>
        TestCheckBox
        <input type="checkbox" name="TestCheckBox" value="Yes" />
    </form>

    <div id="search">
        <input type="text" id="search_bar" /> 
        <input type="button" value="Search!" />
    </div>

And in a php file, I have this:

if($_POST['TestCheckBox'] == 'Yes'){
echo "TEST";
}
if (isset($_POST['action']) && !empty($_POST['action'])) {
generateHTML($_POST['action']);
}
else {
echo "NOTHING IS HERE";
}

Obviously this is not how to do this. I'm curious as to hwo I can make my search bar submit the post data also included in the checkbox.

(It's for a search bar, and the checkboxes are advanced search options, so naturally I only want one search button).

Thank you!

  1. Give the inputs names (without them they cannot be successful controls)
  2. Put them inside the form (otherwise they are only useful to client side scripts)
  3. Make the form make a POST request (it defaults to GET).
  4. Use a submit button so the form will be submitted (regular buttons are only for client side scripts)

It is also beneficial to include labels (which inform users what controls are for and provide larger click targets (the latter is especially important for checkboxes and radio buttons)).

Such:

<form method="post">
    <label for="TestCheckBox">TestCheckBox</label>
    <input type="checkbox" name="TestCheckBox" id="TestCheckBox" value="Yes" />
    <div id="search">
        <label for="search_bar">Query</label>
        <input type="text" id="search_bar" name='action' /> 
        <input type="submit" value="Search!" />
    </div>
</form>

You have to wrap the <form> around all the <inputs> .

<form>
    TestCheckBox
    <input type="checkbox" name="TestCheckBox" value="Yes" />

    <div id="search">
        <input type="text" id="search_bar" /> 
        <input type="button" value="Search!" />
    </div>
</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