网页提交,点击浏览器返回按钮,网页出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网页提交,点击浏览器返回按钮,网页出错相关的知识,希望对你有一定的参考价值。
这两天公司新项目写一个问卷调查小功能,当时因为问题都是循环进行展示提交,所以写到一个php文件中,但是完成后进行测试,当一路点击提交代码结果显示正常,但是当问卷到第三页时点击浏览器返回按钮,网页直接报错,瞬间懵逼啊!没有任何日志!代码如下:
<?php if(!isset($_SESSION)) { session_start(); } $_SESSION[‘username‘] = "zhangminchao"; $data1 = ‘{"paper":{"paperId":"EECD144E3B9141E590A26F968678BD2F","questionList":[{"questionId":"1","content":"请问您毕业学校为?","type":"2","choiceList":[{"choiceId":"A","choiceValue":"浙江财经大学"},{"choiceId":"B","choiceValue":"清华北大2"},{"choiceId":"C","choiceValue":"上海工程技术大学"},{"choiceId":"D","choiceValue":"清华北大1"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"2","content":"请问您的房贷每月还款日?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"22日"},{"choiceId":"B","choiceValue":"11日"},{"choiceId":"C","choiceValue":"30日"},{"choiceId":"D","choiceValue":"7日"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"3","content":"请问您的籍贯为?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"dddd5"},{"choiceId":"B","choiceValue":"dddd2"},{"choiceId":"C","choiceValue":"dddd6"},{"choiceId":"D","choiceValue":"dddd1"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"4","content":"请问您的配偶为?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"增八"},{"choiceId":"B","choiceValue":"邢紫丝"},{"choiceId":"C","choiceValue":"姜思娴"},{"choiceId":"D","choiceValue":"左书文"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"5","content":"请问您的消费贷款月还款额?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"500"},{"choiceId":"B","choiceValue":"7500"},{"choiceId":"C","choiceValue":"1000"},{"choiceId":"D","choiceValue":"4000"},{"choiceId":"E","choiceValue":"以上皆否"}]}]}}‘; $data = json_decode($data1,true); $tem = array_keys($data[‘paper‘][‘questionList‘]); $sum = count($tem); $time = time(); $username = $_SESSION[‘username‘]; if($_SERVER[‘REQUEST_METHOD‘] == ‘POST‘) { $result[‘answer‘] = $_POST[‘answer‘]; $result[‘paperId‘] = $_POST[‘paperid‘]; $time1 = $_POST[‘time‘]; $result[‘time‘] = $time - $time1; //echo $result[‘time‘]; $result[‘id‘] = $_POST[‘id‘]; $result[‘truck‘] = $_SESSION[$username.$result[‘id‘]]; $param = $_SESSION[‘param‘]; $n = $result[‘id‘]-1; $param[‘answer‘][$n] = $result; if($result[‘id‘] == $sum) { $param = json_encode($param); var_dump($param); //结束 //header("Location:../shouye/index.php"); exit; }else{ $_SESSION[‘param‘] = $param; $i = $result[‘id‘]; } }else{ //首次进入 $_SESSION[‘param‘] = array(); $i = 0; for($l=0;$l<=$sum;$l++) { if(isset($_SESSION[$username.$l])) { $_SESSION[$username.$l] = ""; } } } ?> <!DOCTYPE html> <html> <head> <meta charset=utf-8" /> <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;"> <meta name="format-detection" content="telephone=no"> <meta http-equiv="Cache-Control" content="no-cache"/> <title></title> <link href="./css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="whole"> <!--头部信息--> <div class="head"> <div class="t_pic"> <img src="img/head.png" /> </div> <div class="t_tit"> <p>小邑金服问卷调查活动</p> </div> </div> <!--选择单选题目--> <div class="t_con"> <div class="con_tit"> <p> 请选择</p> <p><?php echo ($i+1)."、";echo $data[‘paper‘][‘questionList‘][$i][‘content‘];?></p> </div> <div class="con_con"> <ul> <form id="formid1" action="quest.php" class="mui-input-group" method="post"> <?php if($data[‘paper‘][‘questionList‘][$i][‘type‘] == "1") { foreach($data[‘paper‘][‘questionList‘][$i][‘choiceList‘] as $val) { echo " <li> <div class=‘checkboxFour‘> <input type=‘radio‘ value=‘$val[choiceId]‘ id=‘questid‘ name=‘answer‘ onblur=‘getTruck(this.value);‘/> <span>$val[choiceValue]</span> </div> </li> "; } }else{ foreach($data[‘paper‘][‘questionList‘][$i][‘choiceList‘] as $val) { echo " <li> <div class=‘checkboxFour‘> <input type=‘checkbox‘ value=‘$val[choiceId]‘ id=‘questid‘ name=‘answer[]‘ onblur=‘getTruck(this.value);‘/> <span>$val[choiceValue]</span> </div> "; } } ?> </li> <input type="hidden" id="paperId" name="paperid" class="mui-input-clear" value="<?php echo $data[‘paper‘][‘paperId‘];?>" > <input type="hidden" id="time" name="time" class="mui-input-clear" value="<?php echo $time;?>" > <input type="hidden" id="type" name="type" class="mui-input-clear" value="<?php echo $data[‘paper‘][‘questionList‘][$i][‘type‘]?>" > <input type="hidden" id="id" name="id" class="mui-input-clear" value="<?php echo ($i+1);?>" > </form> </ul> </div> </div> <!--上提下题按钮--> <div class="t_btn"> <button onclick="huankuan();" type="button">立即提交</button> </div> <!--提示遮罩层--> <div class="ts_mask"> <div class="tishi"> <!-- <div class="qd"></div> --> <img src="img/mxz_ts.png" /> </div> </div> </div> <script language="javascript" type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script> function huankuan() { var type = document.getElementById("questid").type; if(type == "radio") { var returnType = $(‘input:radio:checked‘).val(); }else{ var returnType = $("input:checkbox:checked").val(); } //alert(returnType); if(typeof(returnType) == "undefined") { alert("请您选择答案"); return false; } document.getElementById("formid1").submit(); } function getTruck(data) { var checkid = data; var questid = $("#id").val(); $.ajax({ ‘url‘:"getTruck.php", ‘type‘:"POST", ‘dataType‘:"json", ‘data‘:{questid:questid,checkid:checkid}, ‘success‘:function(data){ $data = JSON.stringify(data); alert($data); } }); } </script> </body> </html>
没办法,还是存在问题,然后百度,网上找相关的问题解决方案,但是一无所获。还是自己定位吧。把文件分开处理,还是无果,最后想到是否是php处理代码和html代码在同一页面导致的。所以赶紧进行分开。最终成功。如下代码:
<?php if(!isset($_SESSION)) { session_start(); } $_SESSION[‘username‘] = "zhangminchao"; $data1 = ‘{"paper":{"paperId":"EECD144E3B9141E590A26F968678BD2F","questionList":[{"questionId":"1","content":"请问您毕业学校为?","type":"2","choiceList":[{"choiceId":"A","choiceValue":"浙江财经大学"},{"choiceId":"B","choiceValue":"清华北大2"},{"choiceId":"C","choiceValue":"上海工程技术大学"},{"choiceId":"D","choiceValue":"清华北大1"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"2","content":"请问您的房贷每月还款日?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"22日"},{"choiceId":"B","choiceValue":"11日"},{"choiceId":"C","choiceValue":"30日"},{"choiceId":"D","choiceValue":"7日"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"3","content":"请问您的籍贯为?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"dddd5"},{"choiceId":"B","choiceValue":"dddd2"},{"choiceId":"C","choiceValue":"dddd6"},{"choiceId":"D","choiceValue":"dddd1"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"4","content":"请问您的配偶为?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"增八"},{"choiceId":"B","choiceValue":"邢紫丝"},{"choiceId":"C","choiceValue":"姜思娴"},{"choiceId":"D","choiceValue":"左书文"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"5","content":"请问您的消费贷款月还款额?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"500"},{"choiceId":"B","choiceValue":"7500"},{"choiceId":"C","choiceValue":"1000"},{"choiceId":"D","choiceValue":"4000"},{"choiceId":"E","choiceValue":"以上皆否"}]}]}}‘; $data = json_decode($data1,true); $tem = array_keys($data[‘paper‘][‘questionList‘]); $sum = count($tem); $time = time(); $username = $_SESSION[‘username‘]; $result[‘answer‘] = $_POST[‘answer‘]; $time1 = $_POST[‘time‘]; $result[‘time‘] = $time - $time1; //echo $result[‘time‘]; $result[‘id‘] = $_POST[‘id‘]; $result[‘truck‘] = $_SESSION[$username.$result[‘id‘]]; $param = $_SESSION[‘param‘]; $n = $result[‘id‘]-1; $param[‘answer‘][$n] = $result; if($result[‘id‘] == $sum) { $param = json_encode($param); var_dump($param); //结束 //header("Location:../shouye/index.php"); exit; }else{ $_SESSION[‘param‘] = $param; header("location:quest1h.php?questid=1"); exit; } ?>
<?php if(!isset($_SESSION)) { session_start(); } $_SESSION[‘username‘] = "zhangminchao"; $data1 = ‘{"paper":{"paperId":"EECD144E3B9141E590A26F968678BD2F","questionList":[{"questionId":"1","content":"请问您毕业学校为?","type":"2","choiceList":[{"choiceId":"A","choiceValue":"浙江财经大学"},{"choiceId":"B","choiceValue":"清华北大2"},{"choiceId":"C","choiceValue":"上>海工程技术大学"},{"choiceId":"D","choiceValue":"清华北大1"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"2","content":"请问您的房贷每月还款日?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"22日"},{"choiceId":"B","choiceValue":"11日"},{"choiceId":"C","choiceValue":"30日"},{"choiceId":"D","choiceValue":"7日"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"3","content":"请问您的籍贯为>?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"dddd5"},{"choiceId":"B","choiceValue":"dddd2"},{"choiceId":"C","choiceValue":"dddd6"},{"choiceId":"D","choiceValue":"dddd1"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"4","content":"请问您的配偶为?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"增八"},{"choiceId":"B","choiceValue":"邢紫丝"},{"choiceId":"C","choiceValue":"姜思娴"},{"choiceId":"D","choiceValue":"左书文"},{"choiceId":"E","choiceValue":"以上皆否"}]},{"questionId":"5","content":"请问您的消费贷款月还款额?","type":"1","choiceList":[{"choiceId":"A","choiceValue":"500"},{"choiceId":"B","choiceValue":"7500"},{"choiceId":"C","choiceValue":"1000"},{"choiceId":"D","choiceValue":"4000"},{"choiceId":"E","choiceValue":"以上皆否"}]}]}}‘; $data = json_decode($data1,true); $time = time(); $i = $_GET[‘questid‘]; ?> <!DOCTYPE html> <html> <head> <meta charset=utf-8" /> <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;"> <meta name="format-detection" content="telephone=no"> <meta http-equiv="Cache-Control" content="no-cache"/> <title></title> <link href="./css/style.css" rel="stylesheet" type="text/css" /> </head> <body> <div class="whole"> <!--头部信息--> <div class="head"> <div class="t_pic"> <img src="img/head.png" /> </div> <div class="t_tit"> <p>小邑金服问卷调查活动</p> </div> </div> <!--选择单选题目--> <div class="t_con"> <div class="con_tit"> <p> 请选择</p> <p><?php echo ($i+1)."、";echo $data[‘paper‘][‘questionList‘][$i][‘content‘];?></p> </div> <div class="con_con"> <ul> <form id="formid1" action="quest2.php" class="mui-input-group" method="post"> <?php if($data[‘paper‘][‘questionList‘][$i][‘type‘] == "1") { foreach($data[‘paper‘][‘questionList‘][$i][‘choiceList‘] as $val) { echo " <li> <div class=‘checkboxFour‘> <input type=‘radio‘ value=‘$val[choiceId]‘ id=‘questid‘ name=‘answer‘ onblur=‘getTruck(this.value);‘/> <span>$val[choiceValue]</span> </div> </li> "; } }else{ foreach($data[‘paper‘][‘questionList‘][$i][‘choiceList‘] as $val) { echo " <li> <div class=‘checkboxFour‘> <input type=‘checkbox‘ value=‘$val[choiceId]‘ id=‘questid‘ name=‘answer[]‘ onblur=‘getTruck(this.value);‘/> <span>$val[choiceValue]</span> </div> "; } } ?> </li> <input type="hidden" id="time" name="time" class="mui-input-clear" value="<?php echo $time;?>" > <input type="hidden" id="type" name="type" class="mui-input-clear" value="<?php echo $data[‘paper‘][‘questionList‘][$i][‘type‘]?>" > <input type="hidden" id="id" name="id" class="mui-input-clear" value="<?php echo ($i+1);?>" > </form> </ul> </div> </div> <!--上提下题按钮--> <div class="t_btn"> <button onclick="huankuan();" type="button">立即提交</button> </div> <!--提示遮罩层--> <div class="ts_mask"> <div class="tishi"> <!-- <div class="qd"></div> --> <img src="img/mxz_ts.png" /> </div> </div> </div> <script language="javascript" type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script> function huankuan() { var type = document.getElementById("questid").type; if(type == "radio") { var returnType = $(‘input:radio:checked‘).val(); }else{ var returnType = $("input:checkbox:checked").val(); } //alert(returnType); if(typeof(returnType) == "undefined") { alert("请您选择答案"); return false; } document.getElementById("formid1").submit(); } function getTruck(data) { var checkid = data; var questid = $("#id").val(); $.ajax({ ‘url‘:"getTruck.php", ‘type‘:"POST", ‘dataType‘:"json", ‘data‘:{questid:questid,checkid:checkid}, ‘success‘:function(data){ $data = JSON.stringify(data); alert($data); } }); } </script> </body> </html>
以上是关于网页提交,点击浏览器返回按钮,网页出错的主要内容,如果未能解决你的问题,请参考以下文章