duannaozhao4626 2018-01-23 12:31
浏览 54

防止结果,重新加载浏览器后通过AJAX显示

I have a insert query through ajax. It is working correctly. But when I reload browser then result disappears from div section and if I insert form through ajax again then result is showing.

I have a file first.php (in which, form is present), a AJAX code and a firstcall.php where query will be execute.

My first.php (html form) is:

<form  class="reservation-form mb-0" action="" method="post"  autocomplete="off">
<input name="name1" id="name1" class="form-control"  type="text" placeholder="Enter Name" required aria-required="true">
<input name="age" id="age" class="form-control" required  type="number" placeholder="Enter Age" aria-required="true">
<input type="checkbox" id="checkbox" class="checkbox1" name="namec[]" value="<?php echo $value['id']; ?>" >
<input type="button" class="pull-right btn btn-warning" value="Submit" id="submit">
 </form>

Here data should be display:

<div class="col-md-5">
 <div class="panel panel-primary" id="showdata">
<!-- Here is the results, but when reload browser then result disapper-->    
</div>      
</div>

AJAX is:

<script type="text/javascript">
$(document).ready(function(){
 $("#submit").click(function(){
    var name1 = $("#name1").val();
    var age = $("#age").val();

    var chkArray=[];
    $('.checkbox1:checked').each( function() {               
    chkArray.push($(this).val());    
    } );
    var selected;
    selected = chkArray.join(',') ;

    if(selected.length > 1){
    $.ajax( {
    url:'firstcall.php',
    type:'POST',
    data:{name1: name1,age: age,namec: chkArray},
     }).done(function(data){
      $("#showdata").html(data);
         });

        }
            else{
    alert("Please at least one of the checkbox");   
 }
 });    
 });

</script>

firstcall.php is:

<div class="panel panel-primary" id="showdata">
<?php 
foreach($_POST['namec'] as $selected){
echo $selected;
$_SESSION['name1']=$_POST["name1"];
$_SESSION['age']=$_POST["age"];

echo $name1=$_SESSION['name1'];
echo $age=$_SESSION['age'];

$query=mysql_query("insert into patient_details (p_name,p_age,g_number) values ('$name1','$age','$selected')") or die(mysql_error());
}
?>  

  • 写回答

2条回答 默认 最新

  • dongse5528 2018-01-23 12:39
    关注

    If I understand your question correctly, you want the Ajax result to also show on page load?

    Right now you only execute the JS after a click (so on page load/relaod you will just see the html), you might want to execute it after page load aswell (so execute the script without the .click)

    You could create a function once, and call it when the page is ready and on click.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测