duangan9251 2015-03-27 11:03
浏览 44
已采纳

如何在php中提交表单后返回同一页面。

I am making an e-commerce website where I have lots of products. If a user goes to any product items page and submits any form there then they should come on the same page.

So, how to come on the same page?

  • 写回答

7条回答 默认 最新

  • doulu4976 2015-03-27 11:21
    关注

    Thank You All. I Got My Answer

    $_SERVER['REQUEST_URI']; will give the current URL with query strings.

    Like my page is 'products.php?Product=20'

    echo $_SERVER['REQUEST_URI']; =>/products.php?Product=20
    

    So, we can directly use this in header location.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongyou5271 2015-03-27 11:07
    关注

    On the formular target page set:

    header('Location: http://www.example.com/same_page');
    
    评论
  • duankuaizhe8257 2015-03-27 11:07
    关注

    Leave action attribute of form blank. Like so:

    <form action="" method="post">
    

    Or

    <form action="#" method="post">
    
    评论
  • donglanzhan7151 2015-03-27 11:09
    关注

    You can use this :

     header('Location: filename.php);
    

    If you get any $_POST errors put it in a condition: if(isset[$_POST])

    评论
  • douzi9211 2015-03-27 11:09
    关注

    On your opening form tag add

    action="submit.php"

    • then once it goes to that page when the submit button is hit add this to the bottom of that php page:

    header("Location: success.html OR success.php");

    评论
  • douzhechi2435 2015-03-27 11:13
    关注

    Pass current url as a hidden input

    <input name="redirect" value="PUT CURRENT URL HERE" />
    

    into your view layer (if MVC). When the form is submitted, read the value like

    $_POST['redirect']
    

    and after doing what you have to do use header location as it was suggested. It's really simple. No rocket science.

    评论
  • douyuanqia665858 2015-03-27 11:14
    关注

    If you want to submit various forms on same page and then go back to the page where the form is submitted, you must also send the form URL of the page where it was sent, preferably in a hidden element. And after processing form redirect to URL stored in hidden.

    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 IDEA构建失败?怎么搞
  • ¥15 求该题的simpson,牛顿科特斯matlab代码,越快越好
  • ¥30 求解,有偿,可商量价格
  • ¥15 编译arm板子的gcc
  • ¥15 C++代码报错问题,c++20协程
  • ¥15 c++图Djikstra算法求最短路径
  • ¥15 Linux操作系统中的,管道通信问题
  • ¥15 ansible tower 卡住
  • ¥15 等间距平面螺旋天线方程式
  • ¥15 通过链接访问,显示514或不是私密连接