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.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大