PHP 渲染/重定向 AJAX 调用
我正在开发 PHP 应用程序,缺少 Rails 的一些功能。我目前有一个 AJAX 表单,提交后会访问 my_page_save_ajax.php。处理表单并保存后,我希望将 AJAX 调用重定向到 my_page_show_ajax.php(如果成功)或返回到 my_page_edit_ajax.php(如果发生错误)。
我曾考虑过使用 include my_page_..._ajax.php
,但我总是遇到文件路径问题,并且 PHP 不知道要渲染什么。更不用说,这两个文件都包含utilities.php,我担心可能会发生冲突。我想我可以使用 include_once
但似乎可能有一个更优雅的解决方案。
如何处理表单并返回另一个 PHP 页面的输出?
非常感谢!
I am working on a PHP application and missing some of the functionality that Rails has. I currently have an AJAX form that when submitted accesses my_page_save_ajax.php. After I process the form and save it, I would like to redirect the AJAX call to either my_page_show_ajax.php if successful or back to my_page_edit_ajax.php if an error occurred.
I have thought about using an include my_page_..._ajax.php
, but I have always had problems with the file paths and PHP not knowing what to render. Not to mention, both of those files include utilities.php
and I'm afraid there might be conflicts. I guess I could use include_once
but it seems like there might be a more elegant solution.
How can I process the form and return the output of another PHP page?
Thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您重定向 AJAX 响应,它实际上不会将用户的浏览器重定向到任何地方。它只会影响通过 AJAX 调用返回的数据。这可能是一个不使用 AJAX 的好例子,因为听起来用户可能只选择两条路径之一。
如果您仍然想重定向用户,您可以发回一个 javascript 片段,通过设置 window.location 来重定向用户
If you redirect the AJAX response, it won't actually redirect the user's browser anywhere. It will simply affect what data comes back through the AJAX call. This may be a good instance to simply not use AJAX, since it sounds like the user may go on only one of two paths.
If you still want to redirect the user, you could send back a javascript snippet which redirects the user via setting window.location