PHP MVC表单提交

我有一个与在PHP应用程序中完成的表单提交有关的问题,它是在MVC架构(自编写框架)中构建的。 </ p>

到目前为止我看到的所有示例(包括现有的后端框架)都以这种方式工作,一旦提交了向数据库添加记录的表单,就会执行某些控制器方法[比如说] 即addRecord()],它触发适当模型的方法。 如果一切正常,则添加记录,并且控制器的方法[在此示例中为addRecord()]呈现“index”页面的视图,该页面显示包含来自数据库的记录的表。</ p>

我想要什么 实现是使用用于添加记录的表单(与我用于添加第一个记录相同的表单)而不是“索引”来呈现视图。 显然我可以通过从addRecord()渲染适当的视图(使用表单查看)来轻松完成。</ p>

但棘手的一点是,当你检查url时,你会看到以下内容:\ n第一次输入时将是</ p>

  http:// project_name / my_controller / create 
</ code> </ pre>

一次 第一个表单被submietted,你从addRecord()方法返回到视图,然后url将是:</ p>

  http:// project_name / my_controller / addRecord 
</ code> < / pre>

我希望看到的是返回原始网址,即 http:// project_name / my_controller / create </ code> </ p>

< p>不确定这是否清楚?</ p>

PS。 当然我可以使用AJAX调用表单提交(这样我将保持在同一页面)但也许没有AJAX就可以实现相同的效果。</ p>

提前致谢,</ p >
</ div>

展开原文

原文

I have a question related to form submission done in PHP application that's built in MVC architecture (self-written framework).

All examples that I've seen so far (including existing back-end frameworks) work this way that once form for adding record to database is submitted then certain method of controller is executed [say i.e. addRecord()], which triggers method of appropriate model. If everything goes OK then record is added and controller's method [addRecord() in this example] renders view of "index" page that displays table with records from database.

What I would like to achieve is to render view with form used to add records (the same that I used to add first record) instead of "index". Obviously I can do it easily by just rendering appropriate view from addRecord() (view with the form).

But the tricky point is when you check url you'll see the following: The first time you enter it will be i.e.

http://project_name/my_controller/create

Once first form was submietted and you return to the view from addRecord() method then url will be:

http://project_name/my_controller/addRecord

What I would like to see is return to the original url, that is http://project_name/my_controller/create

Not sure if this is clear?

PS. Of course I could use AJAX call for form submission (that way I will stay at the same page) but perhaps it's possible to achieve the same without AJAX.

Thanks in advance,

doutan3371
doutan3371 如果你想接受它,我就回答了它。
4 年多之前 回复
dph87312
dph87312 你好Nerdyist,这是我一直缺少的。谢谢你的提示!
4 年多之前 回复
douzhun8615
douzhun8615 由于这是一个自编写的框架,因此很难说如何实现这一目标,但您只需要重新定向成功以便addRecord创建。我使用slim作为控制器,这是我创建的正常MVC流程。
4 年多之前 回复

1个回答



在控制器上,您需要提交addRecord路由并进行处理。 检查以确保它是成功的,并且在成功提交后,您可以重定向回创建路径。</ p>

由于您使用的是自定义框架,因此很难给出示例。 我使用slim,它有一个路由的重定向方法。 如果您所做的事情没有那样,那么使用就可以做到这一点。</ p>

  header('Location:'。$ createUrl); 
die(); //或退出
</ code> </ pre>
</ div>

展开原文

原文

On the controller you will want to submit to the addRecord route and do the processing. Have a check to make sure it was successful and on successful submission you can redirect back to the create route.

It is hard to give an example since you are using a custom made framework. I use slim which has a redirect method for a route. If what you have made does not have something like that then using should do the trick.

header('Location: '.$createUrl);
die(); //or exit

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐