2015-02-12 77 views
-1

用於使用PHP更新數據庫表項的jQuery。阿賈克斯代碼不工作後,請幫助。jQuery Ajax不工作問題

$("#dynamic-table").on("click", ".submit", function() { 
 
    var rowID = $(this).attr("id"); 
 
    var allottedValue = $(this).parent().find('input').val(); 
 
    alert('Row id = ' + rowID + ' Enrollment no = ' + allottedValue); 
 

 
    var dataString = 'allottedEnroll=' + allottedValue + '&rowid=' + rowID; 
 

 
    // After this line it is not working 
 
    $.ajax({ 
 
     type: "POST", 
 
     url: "request/allot_enrollmentNo_gov.php", 
 
     data: dataString, 
 
     success: function (html) { 
 
      $(this).parents(".success1").replaceWith(html); 
 
     } 
 
    }); 
 
    //$(this).parents(".success1").animate({backgroundColor: "#003"}, "slow").animate({opacity: "hide"}, "slow"); 
 
});
// HTML to Show Multiple Inputbox for multiple upload with link 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="dynamic-table"> 
 
    <div class="success1"> 
 
     <input name="enrollNo" type="text" value="" class="postEnroll"/> 
 
     <br/>&nbsp; 
 
     <a href="#" id="TakeFromDB" class="text-success submit">Allot Enrollment No</a> 
 
    </div> 
 
</div>

+0

請添加更多細節比'它不工作'。你有沒有檢查控制檯的錯誤?服務器代碼是否正在執行? – 2015-02-12 08:55:23

+0

此代碼主要用於向學生分配註冊號。 他們分配的行數很多。這就是爲什麼我顯示許多輸入框與「Allot Enrollment」鏈接使用Ajax分配每行。 – 2015-02-12 09:42:53

+0

如何檢查控制進入Ajax或不是 – 2015-02-12 10:01:11

回答

0

您想通過父母來代替,它必須是孩子。使用下面的代碼

success: function (html) { 
    $("#dynamic-table .success1").replaceWith(html); 
} 

OR

success: function (html) { 
    $("#dynamic-table").find(".success1").replaceWith(html); 
} 
+0

先生我的Ajax請求不在「request/allot_enrollmentNo_gov.php」文件中。什麼是問題我的PHP代碼是 <?php include_once'../includes/connection.php'; if($ _POST ['rowid'] && $ _POST ['allottedEnroll']){ $ allottedEnroll = $ _POST ['allottedEnroll']; $ rowid = $ _POST ['rowid']; $ enrolldate = date('Y-m-d'); $ sqlenr =「UPDATE'gov_student'SET enrollmentid ='$ allottedEnroll',enrollDate ='$ enrolldate'WHERE id ='$ rowid'」; $ resultenr = mysqli_query($ connect,$ sqlenr); if($ resultenr){ echo $ allottedEnroll; } } ?> – 2015-02-12 09:46:12

0

此代碼的偉大工程,我已經使用了類似的....試試這個..

function FormSubmit() {  
    $.ajax(
     type: "POST", 
     url: 'success1.php', 
     data: $("#attend_data").serialize(), 
     async: false 
     }).done(function(data) { 
      $("#attend_response").html(data); 
    }); 
} 
0

我已經更新了下面的代碼片段。 ..離線測試代碼正在工作:

幾個指針:

取而代之的是:request/allot_enrollmentNo_gov.php

試試這個:/request/allot_enrollmentNo_gov.php

通知斜槓(/)這表明阿賈克斯路徑必須從具體取決於您的服務器設置的根目錄開始。

使用此:下面的$(".success1").html(html);代替$(this).parents(".success1").replaceWith(html);

工作代碼:

$("#dynamic-table").on("click", ".submit", function() { 
 
     var rowID = $(this).attr("id"); 
 
     var allottedValue = $(this).parent().find('input').val(); 
 
     alert('Row id = ' + rowID + ' Enrollment no = ' + allottedValue); 
 

 
     var dataString = 'allottedEnroll=' + allottedValue + '&rowid=' + rowID; 
 

 
     // After this line it is not working 
 
     $.ajax({ 
 
      type: "POST", 
 
      url: "/request/allot_enrollmentNo_gov.php", 
 
      data: dataString, 
 
      success: function (html) { 
 
       $(".success1").html(html); 
 
       alert('Response from the POST page = ' + html + '); 
 
      } 
 
     }); 
 
     //$(this).parents(".success1").animate({backgroundColor: "#003"}, "slow").animate({opacity: "hide"}, "slow"); 
 
    });
// HTML to Show Multiple Inputbox for multiple upload with link 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div id="dynamic-table"> 
 
    <div class="success1"> 
 
     <input name="enrollNo" type="text" value="" class="postEnroll"/> 
 
     <br/>&nbsp; 
 
     <a href="#" id="TakeFromDB" class="text-success submit">Allot Enrollment No</a> 
 
    </div> 
 
</div>

0

現在,它是通過設置家長可以正常使用:

$("#dynamic-table").on("click", ".submit", function() { 
    var rowID = $(this).attr("id"); 
    var rowParent = $(this).parent('.success1'); 
    var allottedValue = rowParent.find('input').val(); 

    var dataString = 'allottedEnroll=' + allottedValue + '&rowid=' + rowID; 
    $.ajax({ 
     type: "POST", 
     url: "request/allot_enrollmentNo_gov.php", 
     data: dataString, 
     success: function (html) { 
      rowParent.replaceWith(html); 
     } 
    }); 
    return false; 
});