【问题标题】:insert query with ajax without reloading whole page使用ajax插入查询而不重新加载整个页面
【发布时间】:2018-01-23 07:59:08
【问题描述】:

我想通过 AJAX 插入数据(无需重新加载页面)。我试过了,但它没有显示数据并且还重新加载页面。

我有一个文件 first.php(其中存在表单)、一个 AJAX 代码和一个 firstcall.php 将在其中执行查询。

我的first.php(html表单)是:

<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="submit" class="pull-right btn btn-warning" value="Submit" id="submit">
 </form>

这里应该显示数据:

<div class="col-md-5">
 <div class="panel panel-primary" id="showdata">

</div>      
</div>

AJAX 是:

<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 是:

<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());
}
?>  

【问题讨论】:

  • 首先,将您的submit 按钮类型更改为button,因为提交会自动重新加载页面!
  • 由于提交按钮,页面正在重新加载,尝试将其更改为简单按钮,然后将数据发布到php文件。
  • 除非你preventDefault()
  • 是的,另一个问题,我认为在firstcall.php 中应该只有一个echo 吃了脚本的结尾?
  • chech ) abd } 在 js 脚本的末尾

标签: javascript php jquery mysql ajax


【解决方案1】:

$("#submit").click(function(event){之后添加命令

event.preventDefault();

而且你的页面不会被重新加载

【讨论】:

  • 这是一个快速发现问题
  • 您在$("#submit").click(function(event){ 中插入了event 吗?
  • 不,它在标题中。
  • 在 footer.php 中?
  • 就在页脚之前
【解决方案2】:

提交按钮将在点击时自动重新加载页面,因此解决方案是将按钮类型更改为button 或将preventDefault 添加到点击事件中

$("#submit1,#submit2").click(function() {
  alert('form submited')
})
$("#submit3").click(function(e) {
  e.preventDefault();
  alert('form submited')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  this will reload the page
  <input type="submit" class="pull-right btn btn-warning" value="Submit" id="submit1">
</form>

<form>
  this will not reload the page
  <input type="button" value="Submit" id="submit2">
</form>


<form>
  this will not reload the page
  <input type="submit" value="Submit" id="submit3">
</form>

【讨论】:

  • 感谢您的回复...但我仍然有问题..如果我手动重新加载浏览器,则 ajax 显示的结果将消失。我怎样才能防止结果?
猜你喜欢
  • 2014-09-06
  • 1970-01-01
  • 2015-03-05
  • 2013-09-18
  • 2016-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
相关资源
最近更新 更多