PHP variable to javascript + modal


PHP variable to javascript + modal

我想创建模态按钮,用于删除用户从html正文中的foreach中获取的内容。模态打开一个带有窗体的iframe。iframe的url看起来像

<iframe src=<?php echo base_url('index/page/');?> witdth....

我想传递给iframe src php变量包含一个用户id,例如:

<iframe src=<?php echo base_url('/index/page/'+username);?> witdth....

第一个代码工作得很好,它显示了视图,但没有第二个代码传递的变量。我使用javascript打开模态并获得ID变量。链接:

<a data-toggle="modal" data-target="#myModal" data-detail-id=<?php echo $users->username; ?>>

和js:

        <script>
    $(".myModal").on("click", function(e) {
      var username;
          e.preventDefault();
          username = $(this).data("detail-id");

      });
    </script>

如何获得detailId到iframe src传递变量到索引/页面?

谢谢你。

编辑—我将发布完整的代码,目标是传递一个PHP变量在一个foreach到一个模态窗口在调用一个框架到其他视图使用变量:

<a data-toggle="modal" data-target="#myModal" data-detail-id=<?php echo $users->username; ?>></a>
    <!-- Modal -->
<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"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        <script>
        $(".myModal").on("click", function(e) {
          var username;
              e.preventDefault();
              username= $(this).data("detail-id");

          });
        </script>
        <center><iframe src=<?php echo base_url('/index/page/'+username);?> width="500" height="380" frameborder="0" allowtransparency="true"></iframe></center>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

谢谢! !

你正在混合JS和PHP在一起:

<iframe src=<?php echo base_url('/index/page/'+username);?> width.... ( note the + )
<iframe src=<?php echo base_url('/index/page/'.$username);?> width....  ( append with . operator )

在PHP中,用。操作符

如果你正在寻找简单的解决方案来删除某些东西(例如用户),你可以在你的视图中使用这样的东西:

<script>
function doconfirm()
{
    job=confirm("Are you sure, to delete?");
    if(job!=true)
    {
        return false;
    }
}
</script>
<?php foreach ($users as $user_detail): ?>
    <a href="/users/delete/<?=$user_detail['user_id']; ?> onClick="return doconfirm();">Delete user</a><br>
<?php endforeach ?>

和从你的用户模型尝试如下:

public function delete($user_id)
{
    $query = $this->db->where('user_id', $user_id);
    return $query = $this->db->delete('users');     
}