duanraotun1674 2014-01-19 10:06 采纳率: 100%
浏览 83

如何使表单仅在按钮点击时提交而不是刷新?

I have a form where i should make the form submit(request to a service) only if the form field is changed, If the field is not changed when the user refresh the page it should not submit the form again.

OBJECTIVE is to reduce the number of form submission as possible, So if the user has the same entered data and he refresh, we shouldn't waste the previous returned datas and try to give new request and show the same datas.

Instead it should be submitted only if the TEXT(searchdirectory) is changed. So there won't be repeating request for same datas.

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">            
        <label>TEXT :</label>
        <input type="text" id="search_dir" name="searchdirectory"
            value="<?php
                    if (isset($_POST['searchdirectory']))
                    {
                        echo($_POST['searchdirectory']); 
                    }
                   ?>"
        />            
        <input type="submit" value="Search" name="submit" id="searchB"/>
        <?php            
            if (isset($_POST['submit'])) 
            {
             //functions  to show results queried from searchdirectory
            }
        ?>
    </form>

I guess this would be to deal with refresh behaviour of browsers, Is there a way to stop the form submission if user refresh the page. It should show the same datas.

  • 写回答

3条回答 默认 最新

  • duanjiyun7391 2014-01-19 10:09
    关注

    Use redirect when form data was sent at first.

    <?php            
        if (isset($_POST['submit'])) 
        {
            //do function
            header("Location: http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
        }
    ?>
    

    Where "http://". $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] - is a link to which the user will be redirected after form submit.

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站