簡體   English   中英

關於成功的警報消息不適用於AJAX

[英]Alert Message on Success not working on 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); 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"> </select> <button type="submit" name="add_chapter" id="submit">Add </button> <pre> 

使用以下代碼:

 <!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";  
}
  ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM