dsxfa26482 2016-03-31 18:49
浏览 88

PHP MVC表单提交

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,

  • 写回答

1条回答 默认 最新

  • doupao6011 2016-04-01 12:18
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度