如何将data-id变量从modal传递到php页面

时间:2016-04-11 13:22:52

标签: javascript php href bootstrap-modal

我有以下DELETE按钮,我通过data-id

传递$ userid
<a href='#myModal' class='trash' data-id='".$userid."' role='button'data-toggle='modal'>
    Delete</a>

我有以下模式

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
            </div>
            <div class="modal-body">
                <p>You are about to delete <b><i class="title"></i></b> record, this procedure is irreversible.</p>
                <p>Do you want to proceed?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a href="#" class="btn btn-danger"  id="modalDelete">Delete</a>
            </div>
        </div>
    </div>
</div>

以下JS获取DELETE按钮的值

$('.trash').click(function(){
   var id=$(this).data('id');   
   $('#modalDelete').attr('href','delete_user.php?id=' + id);
});

我正在尝试设置&#34; href&#34;的值。在模态中,它可以传递给名为delete_user.php的php页面,该页面从数据库中删除用户。谁知道我哪里错了?我无法将href转到delete_user.php

1 个答案:

答案 0 :(得分:1)

你在这里犯了错误data-id='".$userid."'应该是data-id='<?php echo $userid;>'

<a href='#myModal' class='trash' data-id='<?php echo $userid;>' role='button'data-toggle='modal'>
Delete</a>

为了更好的方法,摆脱click函数,使用modal event并让bootstrap处理其余的

$(document).ready(function() {
  $('#myModal').on('show.bs.modal', function(e) {
    var id = $(e.relatedTarget).data('id');
    alert(id);
    $('#modalDelete').attr('href', 'delete_user.php?id=' + id);
  });
});

Fiddle