doufei1988 2013-09-06 10:39
浏览 19

根据表单结果有选择地将表单发布到不同的页面?

Say I have a simple form on a page called photo/delete.php. This form deletes an image specified by the user. All it is, is this:

<form action="?" method="post">
 <input type="checkbox" name="confirmDelete" value="confirm" />    
 <input type="submit" value="delete" />
</form>

So, this form contains a confirmation check box that must be ticked to ensure the image is deleted. How can I dynamically choose what page to POST this form to, based on its contents?

For example, if the checkbox is not checked, yet the submit button is clicked, I'd like to stay on the same photo/delete.php page and display an error, since its possible they really do want to delete the image and simply forgot to tick the box.

But otherwise, if everything is successful and the checkbox is ticked, I'd like to POST it to another page, say home.php since it makes no sense to stay on the same page of a just-deleted image.

How can I implement this?

  • 写回答

1条回答 默认 最新

  • dsegw3424 2013-09-06 10:59
    关注

    You may try something like this

    HTML:

    <form action="delete.php" method="post">
        <input type="checkbox" name="confirmDelete" value="confirm" />    
        <input id="btn_delete" type="submit" value="delete" />
    </form>
    

    JS:

    window.onload = function(){
        document.getElementById('btn_delete').onclick = function(e){
            var checkBox = document.getElementsByName('confirmDelete')[0];
            if(!checkBox.checked) {
                if(e && e.preventDefault) {
                    e.preventDefault();
                }
                else if(window.event && window.event.returnValue) {
                    window.eventReturnValue = false;
                }
                alert('Please check the checkbox!');
            }
        };
    };
    

    DEMO.

    评论

报告相同问题?

悬赏问题

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