2015-02-12 122 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; 
});