简体   繁体   English

表单数据和Button在同一_POST中

[英]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: 在一个php文件中,我有这个:

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). 使表单发出POST请求(默认为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>包裹在所有<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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