简体   繁体   English

PHP发布URL没有任何动作

[英]PHP post url with no action

In the CodeIgniter framework I have a form action that posts the id of a number of checkboxes to a controller. 在CodeIgniter框架中,我有一个表单操作,该操作将多个复选框的ID发布到控制器。 The controller checks each id to ensure it is valid, and then prints off a pdf document. 控制器检查每个ID以确保其有效,然后打印出pdf文档。 This works fine. 这很好。

So, my current method is /items, and I am posting to the /documents method 所以,我当前的方法是/ items,我正在发布到/ documents方法

function documents()
  if ($this->input->post() && validate_documents())
  {
    $this->load->library('Print_docs');
    $this->print_docs->execute($this->input->post());
  }
}

So, if the docs get printed, then this works fine: the user remains on the /items method and pdf is offered as a download in the browser. 因此,如果将文档打印出来,则可以正常工作:用户仍然使用/ items方法,并且在浏览器中提供了pdf下载。

If, however, validate_documents() is false, then I dont want anything to happen - ie the user should remain on the /items method. 但是,如果validate_documents()为false,则我不希望发生任何事情-即用户应保留在/ items方法上。 However, what is actually happening is that the user is somehow being redirected to the /documents method - and the browser is blank. 但是,实际上发生的是,用户以某种方式被重定向到/ documents方法-浏览器为空白。

So, why is the user being redirected to the /documents method? 那么,为什么将用户重定向到/ documents方法? And how do I keep the user on the /items method no matter what? 以及无论如何,如何保持用户使用/ items方法?

A form post is a page refresh. 表单发布是页面刷新。 When the pdf is generated, the page "refresh" is actually you downloading the pdf - so the browser doesn't take you away from the /items method -- it's the same as if you open a link with target="_blank" , the page is opened in a new window and the current page is untouched. 生成pdf时,实际上是在下载“ pdf”页面,实际上是您在下载pdf-这样浏览器就不会使您脱离/items方法了-就像您打开带有target="_blank"的链接一样,该页面将在新窗口中打开,并且当前页面保持不变。

You can do 1 of 2 things here: 您可以在这里做2件事情之一:

  1. Redirect if there is an error in the documents method back to /items 如果documents方法中有错误,请重定向回/items
  2. Submit the form via ajax, so whether there is an error or not, you stay on /items 通过ajax提交表单,因此无论是否有错误,您都停留在/items

It's being redirected, becuse you told it go there (in ). 它已被重定向,因为您告诉它去了那里(在中)。

The only reason it you don't go to documents method, when the validation is ok, is because of the pdf print. 验证正常时,您不使用document方法的唯一原因是pdf打印。 What you can do, is have the code from the documents moved to the "items" method, and set the form's action to "items". 您可以做的是将文档中的代码移至“ items”方法,并将表单的操作设置为“ items”。 That way when the validation fails, the users get "redirected" back to the form, where you should provide feedback on what is wrong (ie. error messages). 这样,当验证失败时,用户将被“重定向”到表单,在表单中您应提供有关错误内容的反馈(即错误消息)。

The form_validation library page has some examples of what I explained: http://codeigniter.com/user_guide/libraries/form_validation.html form_validation库页面上有一些我所解释的示例: http ://codeigniter.com/user_guide/libraries/form_validation.html

So, why is the user being redirected to the /documents method?

是错误的,因为在检查验证时,它已经在document方法中。因此,如果验证为false,则应该基本上重定向到items函数。

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

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