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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器