php 以一种形式提交 POST 和 GET 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6918991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit POST and GET variables in one form
提问by SemperFly
I'm working on a query tool that displays data from a MySQL database. The user is presented with a form containing a few dozen dynamically-generated checkboxes so that they can select how to view the data. This data is submitted as a GET request and is (obviously) displayed in the url when the requested page loads.
我正在研究一个显示来自 MySQL 数据库的数据的查询工具。用户将看到一个包含几十个动态生成的复选框的表单,以便他们可以选择如何查看数据。此数据作为 GET 请求提交,并且(显然)在请求的页面加载时显示在 url 中。
On the same page as the input form, I have a php array that I am generating dynamically and that needs to be sent to the same place as the GET request. However, I do not want the values in this array to be displayed in the URL (i'm using them internally) so I'd like to submit them as a POST request instead.
在与输入表单相同的页面上,我有一个动态生成的 php 数组,需要将其发送到与 GET 请求相同的位置。但是,我不希望此数组中的值显示在 URL 中(我在内部使用它们),因此我想将它们作为 POST 请求提交。
Obviously, I can't do both a GET and POST request at the same time. I'm new to web development (computer science guy, otherwise) and have been scratching my head on how to approach this.
显然,我不能同时执行 GET 和 POST 请求。我是网络开发的新手(计算机科学人员,否则)并且一直在摸索如何解决这个问题。
Let me know if the problem isn't clear.
如果问题不明确,请告诉我。
EDIT: Many have suggested I add them to the action variable a la:
编辑:许多人建议我将它们添加到动作变量 a la:
form action="process.php?get1=value...
form action="process.php?get1=value...
All of these inputs are generated dynamically so to put them in the action variable is not feasible.
所有这些输入都是动态生成的,因此将它们放入动作变量中是不可行的。
采纳答案by SemperFly
Endophage:
内噬菌体:
If it's a PHP array, just store it in a session.
如果是 PHP 数组,只需将其存储在会话中。
This worked great. Obviously, I'm showing my web development greenness here as I didn't really consider using a session.
这很好用。显然,我在这里展示了我的 Web 开发绿色,因为我并没有真正考虑使用会话。
回答by Flambino
GET parameters go in the action url, POST parameters in the form's inputs
GET 参数在操作 url 中,POST 参数在表单的输入中
<form method="post" action="/somepage.php?get=parameters&are=here">
<input type="text" name="postParameter" value="this value will be sent as POST">
... etc
</form>
回答by Naftali aka Neal
You cannot do a post and a get in the same formyou even said that in your question.
您不能以您在问题中所说的相同形式进行发布和获取。
You have to either:
您必须:
- Have 2 forms
- Have one thing submit with a post via ajax and then submit the other form with a get
- 有2个表格
- 通过ajax提交一件事,然后用get提交另一个表单
回答by amigura
<form action="page.php?id=15" method="POST">
<input name="ref_code_cust" type="text" value="some data">
<input name="send" type="submit" value="send">
</form>
or
或者
www.mywebsite/page.php?id=15
www.mywebsite/page.php?id=15
<form action="page.php" method="POST">
<input name="id" type="hidden" value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>">
<input name="email" type="text" value="some data">
<input name="send" type="submit" value="send">
</form>
we need to see your code also to make it easier, especial this array thing
我们还需要查看您的代码以使其更容易,尤其是这个数组
回答by John
You can set GET vars by changing URL:
您可以通过更改 URL 来设置 GET 变量:
<form action="foo.php?getvar=15" method="POST">
<input name="postvar">
</form>
For dynamic GET vars I believe you need Javascript.
对于动态 GET 变量,我相信您需要 Javascript。