简体   繁体   English

发布vs获取URL参数?

[英]post vs get for URL params?

I would like to give the user the ability to return to a previous page with pagination controls after submitting a form. 我想让用户能够在提交表格后返回带有分页控件的上一页。 Typically I use post but this will not pass the in the URL. 通常,我使用post,但这不会传递URL。

Is it recommended to use get instead? 建议使用get代替吗?

It depends on a number of things, mainly what content you are passing through the form. 这取决于许多因素,主要取决于您通过表单传递的内容。

Generally if you have a textarea in the form, its a bad idea to send via GET as your much more likely to reach the max character limit of a URL. 通常,如果表单中有文本区域,则通过GET发送是个坏主意,因为您更有可能达到URL的最大字符数限制。 And obviously if your passing a file then it needs to be POST. 而且很显然,如果您传递文件,则它必须是POST。 Oh, and obviously if it's sensitive data (eg username/password) then using GET isn't a good idea. 哦,显然,如果它是敏感数据(例如,用户名/密码),那么使用GET不是一个好主意。

Other than that I see no harm in using GET instead, as long as you are "cleaning" the data before using it anywhere. 除此之外,只要您在任何地方使用它之前“清理”数据,我都认为使用GET不会造成任何危害。

If using GET isn't an option, then one thing you can do instead is use POST, but save everything in a COOKIE or SESSION variable for you to retrieve on the previous page if pagination/breadcrumbs are used after submitting the form. 如果不是使用GET,那么您可以做的一件事情是使用POST,但是如果提交表单后使用了分页/面包屑,则将所有内容保存在COOKIE或SESSION变量中,以便在上一页中检索。

Expanding a little on Alex and tandu's comments: 在Alex和tandu的评论上扩大一点:

You should use POST when you are sending data the will change the state on the server. 发送数据时应使用POST,这将更改服务器上的状态。 You should use GET when you request data or are performing some other action that will leave the server state unchanged. 当您请求数据或正在执行其他一些操作时,应该使用GET来使服务器状态保持不变。

So, Yes you are correct to use POST for submitting your form and GET is not recommended in this case. 因此,是的,使用POST提交表单是正确的,在这种情况下不建议使用GET。

Then after the post redirect the user to their previous page, perhaps after a short delay to view a thank you message. 然后,在发布之后,将用户重定向到他们的上一页,也许是在短暂的延迟后才能查看感谢消息。

Nothing prevents you from saving the pagination information in the form. 没有什么可以阻止您将分页信息保存在表单中。 For example you are at: 例如,您在:

http://mysite/list.php?page=3&maxperpage=50

A link on the page can redirect the user to 页面上的链接可以将用户重定向到

http://mysite/edit.php?id=8475&page=3&maxperpage=50

Save the page and maxperpage as hidden controls into the form reloading them each time you show the form (in case of postback errors) 将页面和maxperpage作为隐藏控件保存到表单中,每次显示表单时都会重新加载它们(以防回发错误)

<input type="hidden" name="page" value="<?php echo $_REQUEST['page']; ?>" />
<input type="hidden" name="maxperpage" value="<?php echo $_REQUEST['maxperpage']; ?>" />

Then when the postback is complete, use the _POST hidden fields to redirect to that previous page: 然后,当回发完成时,使用_POST隐藏字段重定向到该上一页:

header('location: http://mysite/list.php?page='.$_POST['page'].'&maxperpage='.$_POST['maxperpage']);

Obviously, i've skipped a lot of stuff in there such as validation so don't forget to put some in... 显然,我在其中跳过了很多工作,例如验证,所以请不要忘记在其中添加一些内容。

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

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