当前位置:网站首页>PHP 防止或检测页面被刷新 post重复提交问题
PHP 防止或检测页面被刷新 post重复提交问题
2022-07-22 19:23:00 【viqecel】
在解决post重复提交问题时,经过测试.没办法通过php检测是否被刷新.一般是通过js还判断
<script type="text/javascript">if (performance.navigation.type == 1){
alert(1);
window.location.href='?s=/Home/Index/user_order.html';
}else{
alert(0);
}
</script>
但都没法禁止post页面被重复提交
其实最好的办法是用ajax来提交表单,这样的话,只有点击提交按钮,表单才会提交.也就没必要检测刷新问题了.
比如在用户填写收货人的页面,就收集用户的post信息,通过ajax在后台提交订单.代码演示
按钮
<form action="" method="post" id="tijiao">
<input type="hidden" name="cid" id="cid" value="{
$cid}">
<input type="hidden" name="num" id="num" value="{
$num}">
......
</form>
<div class="qr-btn1" onclick="jiao()">提交订单</div>
ajax代码
function jiao(){
var s_xingming = $("[name='s_xingming']").val();
var s_address = $("[name='s_address']").val();
var s_email = $("[name='s_email']").val();
var s_tel = $("[name='s_tel']").val();
var zid = $('#zid').val();
var wid = $('#wid').val();
var cid = $("[name='cid']").val();
var num = $("[name='num']").val();
var beizhu = $("[name='beizhu']").val();
if("{$_SESSION['dluser']['s_xingming']}"==''){
alert('请设置收货人姓名!');
return;
}
if("{$_SESSION['dluser']['s_email']}"==''){
alert('请设置收货人邮箱!');
return;
}
if("{$_SESSION['dluser']['s_address']}"==''){
alert('请设置收货人详细地址!');
return;}
if("{$_SESSION['dluser']['s_tel']}"==''){
alert('请设置收货人手机!');
return;
}
if($('#zid').val()==''){
alert('请选择支付方式!');
return;
}
if($('#wid').val()==''){
alert('请选择配送方式!');
return;
}
var lis="{$list[0]['cid']}";
if(lis==''){
alert('无商品!');
return;
}
$.ajax({
url:'/?a=user_buy_ajax',
type:"post",
data:"s_xingming="+s_xingming+"&s_address="+s_address+"&s_email="+s_email+"&s_tel="+s_tel+"&num="+num+"&zid="+zid+"&wid="+wid+"&cid="+cid+"&beizhu="+beizhu,
success:function(re){
if(re=="2"){
alert('失败');
}else if(re=="3"){
alert('请先登录');
window.location.href='?a=dl';
}else{
window.location.href='?a=user_buyc&id='+re;//ID传到付款二维码页面
}
}
})
//$('#tijiao').submit();
}
PHP代码
//ajax
function user_buy_ajax(){
if($_SESSION['dluser']['name']==''){
echo 3;exit;
}
$uid=$_SESSION['dluser']['id'];
$_POST['ctime']=time();
$_POST['uid']=$uid;
// print_r( $_POST);exit;
$id=M('order')->add($_POST);
//添加商品
$arr['uid']=$uid;
$arr['oid']=$id;
if(I('cid')){
$arr['cid']=I('cid');
$arr['num']=I('num');
$map['allprice']=getzd(I('cid'),'contlist','price')*I('num');
M('order_list')->add($arr);
}else{
$list=M('cart')->where('uid='.$uid)->select();
$allprice=0;
foreach ($list as $k => $v) {
$arr['cid']=$v['cid'];
$arr['guige']=$v['guige'];
$arr['num']=$v['num'];
$map['allprice']+=getzd($v['cid'],'contlist','price')*$v['num'];
M('order_list')->add($arr);
}
M('cart')->where('uid='.$uid)->delete();
}
$code=$_POST['ctime'].$id;
$map['code']=$code;
$is2=M('order')->where('id='.$id)->save($map);
if($is2){
echo $id;exit;
}else{
echo 2;
}
}
边栏推荐
猜你喜欢

事件抽取文献整理(2019)
![Rllib学习 - [4] - AlgorithmConfig详细介绍 [Pytorch版]](/img/1e/95078ad64a17686463547e8ba87cb1.png)
Rllib学习 - [4] - AlgorithmConfig详细介绍 [Pytorch版]

华为昇腾比赛资料

MySQL --- 子查询 - 子查询概念、规范、分类

Interviewer: how to solve the problem of a large number of requests for data that does not exist in redis, which affects the database?

YOLOv7——论文简述
![Swing-[mynote]- realize the function of positioning scroll from cause like idea](/img/ee/53aae922d7a4b3df3871a3e997cc57.png)
Swing-[mynote]- realize the function of positioning scroll from cause like idea

《STL容器篇》-List模拟实现(三种反向迭代器)

第五章 传播训练

Real time face detection using mediapipe and opencv
随机推荐
ABAP ALV步骤
How to calculate the time-consuming code gracefully? (glory Collection Edition)
常用正则表达式最强整理速查手册(荣耀典藏版)
暑假笔记1
时代潮头,华为将风帆对准数字金融的风与海
[machine learning] principle and practice of model selection (performance measurement)
IDEA DEBUG启动一直卡着不动解决办法
MySQL - sub query - sub query concept, specification, classification
abap ALV总结整理
2021-03-01
Li Kou daily question - day 42 -171. Excel table column serial number
【黄啊码】MySQL入门—3、我用select *,老板直接赶我坐火车回家去,买的还是站票
小红书携手HMS Core,畅玩高清视界,种草美好生活
第零章 Encog入门介绍
第六章 更多监督训练
TCP四次挥手
反射(类的加载)
mysql之外键操作_级联操作
强化学习2
锅炉汽包温度控制系统设计(过程控制课程设计matlab/simulink)