dongpo5207 2014-12-17 19:51
浏览 41
已采纳

使用JQuery和PHP响应单击事件

I need to monitor the click of a button in my page, when it is clicked I need to fire a POST request. With this request I need to send some course ID to the PHP so that I can delete that course from the database. To achieve this, I did something like this;

I have a table with course details and a button as following,

<table>
<tr><td>Course ID</td>
<td>Course Name</td>
<td>Avail.  Quota</td>
</tr>
<tr><td>CS101</td>
<td>Programming 1</td>
<td> 2 <input style="margin-left: 320px;"  type="button" id="submit" value="Cancel" cid="CS101"/></td>
</tr><tr><td>CS315</td>
<td>Objects</td>
<td>5<input style="margin-left: 320px;"  type="button" id="submit" value="Cancel" cid="CS315"/></td>
</tr></table>

I have a row of course details and a button at the end to delete the course. I have a JQuery like this set up to handle click event;

$(document).ready(function(e) {
$('#submit').on('click', function(event){
    e.preventDefault();
    $('#submit').fadeOut(300);

    $.ajax({
        url: 'del_course.php',
        type: 'post',
        data: {'action': 'delete', 'cid': '11239528343'},
        success: function(data, status) {
            if(data == "ok") {
                // Change page to indicate that deletion was successful
            }
        },
        error: function(xhr, desc, err) {
            console.log(xhr);
            console.log("Details: " + desc + "
Error:" + err);
        }
    });
)};
)};

At this stage, del_course.php is just redirecting to an empty page to see if click is detected. del_course.php is as follows;

<?php
if($_POST['action'] == "delete") {
    $cid = $_POST['cid'];             
    echo (''.$cid.' will be deleted.'); 
    echo "ok";
    header( 'Location: empty.php' );
}
?>

What am I missing here? Nothing happens when I click to that input element. Also, you can see that for this example, I am just hardcoding course ID as cid in AJAX script. How can I pass cid from the input element so that I will pass respective course ID for clicked buttons?

  • 写回答

4条回答 默认 最新

  • douyakan8924 2014-12-17 20:12
    关注

    You are binding to two elements which have the same ID, I suggest switching the buttons to classes and then using proper scoping through the 'this' object.

    $('.submit').on('click', function(event){
    e.preventDefault();
    $(this).fadeOut(300);
    
    $.ajax({
        url: 'del_course.php',
        type: 'post',
        data: {'action': 'delete', 'cid': '11239528343'},
        success: function(data, status) {
            if(data == "ok") {
                // Change page to indicate that deletion was successful
            }
        },
        error: function(xhr, desc, err) {
            console.log(xhr);
            console.log("Details: " + desc + "
    Error:" + err);
        }
    });
    

    and change the buttons to <input style="margin-left: 320px;" type="button" class="submit" value="Cancel" cid="CS101"/>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!