当前位置:网站首页>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;
}
}
边栏推荐
- 最新可用的二维码生成 api
- YOLOv7——论文简述
- ABAP语法基础5
- Idea debug is stuck during startup. Solution
- OWA email system login two factor authentication (SMS authentication) scheme
- MySQL --- 子查询 - 子查询概念、规范、分类
- 关系型数据库数据库基本概念
- How to package your project and let other users install it through pip
- (ROS_Melodic) 使用Rviz进行Boundingbox可视化
- 第五章 传播训练
猜你喜欢
随机推荐
华泰证券股票开户怎么样安全吗
Chapter VIII using time series data
CV语义分割模型小抄(1)
abap ALV总结整理
Difference between SFM and MVs
引擎提示Alias HeroDB跟游戏引擎启动异常怎么解决?
图文并茂演示小程序movable-view的可移动范围
常用正则表达式最强整理速查手册(荣耀典藏版)
电商项目如何解决线上优惠券超发(排错+解决方案)(荣耀典藏)
Codeforces Round #808 (Div. 2) C,D Codeforces Round #809 (Div. 2) C
【JS】ES6-let、const定义变量
Apifox学习记录
Exercises in Chapter II of intensive learning
CV semantic segmentation model sketch (1)
(ROS_Melodic) 使用Rviz进行Boundingbox可视化
如何优雅的统计代码耗时?(荣耀典藏版)
2021-03-01
Installation and login installation
安装和登录安装
Wechat applet development: the first HelloWorld









