2016-09-25 101 views
1
  • 你好我在AJAX初学者,在下面的代码不弹出警告消息面临的一个问题。
  • 我尝试使用AJAX来从表单数据插入到数据库中。
  • 此代码成功地将数据存储到数据库中。
  • 但它不会弹出警告消息是主要问题。

警报消息不工作的AJAX

**//index.php** 
 

 
// following is ajax code in which problem occur. i think. 
 
    $(document).ready(function(){ 
 
      $('#submit').click(function(){    
 
       $.ajax({ 
 
        url:"store.php", 
 
        method:"POST", 
 
        data:$('#add_name').serialize(), 
 
        success:function(data) 
 
        { 
 
         alert(data); 
 
         $('#add_name')[0].reset(); 
 
        } 
 
       }); 
 
      }); 
 
    }); 
 

 

 

 
**//store.php** 
 
    
 
    // following is the data base releted query 
 

 
     $conn = mysqli_connect("localhost", "root", "", "prag"); 
 
     
 
    $chapter_name = $_POST["chapter_name"]; 
 
    $class = $_POST["class"]; 
 
    $subjects = $_POST["subjects"]; 
 
    
 
     
 
        $sql = "INSERT INTO chapter(class_id,subject_id,name) VALUES('$class','$subjects','$chapter_name')"; 
 
        mysqli_query($conn, $sql); 
 
    \t \t \t \t echo "Data Inserted"; 
 
    
 
    
 
    </pre>
//HTML CODE 
 
//Following are the input fields through which data will store. 
 

 
<input type="text" name="chapter_name" placeholder="Enter the Chapter name" class="form-control name_list" /> 
 
     
 
<select name="class" id="class" class="form-control"> 
 
    <option>Select Class</option> 
 
</select> 
 
     
 
<select name="subjects" id="subjects" class="form-control"> \t \t \t 
 
</select> 
 
     \t \t \t \t \t \t \t \t \t \t 
 
<button type="submit" name="add_chapter" id="submit">Add </button> 
 
<pre>

+0

错误部分用同样的警告,所以你加入Ajax调用可以检查ajax调用是否真的成功 – RST

+0

该代码示例是否为HTML完整?你的表单标签在哪里?点击提交时你观察到什么? – bcmcfc

+0

,即时通讯面临的问题是唯一的,这不弹出操作成功后的提示框 –

回答

0

下面的代码使用方法:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script type="text/javascript"> 
       $(document).ready(function(){ 
     $('#submit').click(function(e){ 
         e.preventDefault();   
      $.ajax({ 
       url:"data.php", 
       method:"post", 
       data:$('#add_name').serialize(), 
       success:function(data) 
       { 
        alert(data); 
        $('#add_name')[0].reset(); 
       } 
      }); 
     }); 
}); 

    </script> 
    </head> 
<body> 
    <form method="post" id="add_name"> 
      <input type="text" name="chapter_name" placeholder="Enter the Chapter name" class="form-control name_list" /> 

<select name="class" id="class" class="form-control"> 
    <option>Select Class</option> 
</select> 

<select name="subjects" id="subjects" class="form-control">  
</select> 

<button type="submit" name="add_chapter" id="submit">Add </button> 
    </form> 
</body> 
</html> 

检查PHP代码:

<?php 
$conn = mysqli_connect("localhost", "root", "", "prag"); 


    $chapter_name = $_POST["chapter_name"]; 
    $class = $_POST["class_name"]; 
    $subjects = $_POST["subject_name"]; 

    $sql = "INSERT INTO chapter(class_id,subject_id,name) VALUES('$class','$subjects','$chapter_name')"; 
    if(mysqli_query($conn, $sql)) { 
    echo "Data Inserted"; 
    } 
else { 
    echo "Data Not Inserted"; 
} 
    ?>