簡體   English   中英

PHP變量轉換為javascript +模式

[英]PHP variable to javascript + modal

我想創建模式按鈕以刪除用戶從html正文中的foreach獲取的內容。 模態會打開帶有表單的iframe。 iframe網址似乎是

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

我想將php變量包含一個userid傳遞給iframe src,例如:

<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,以將變量傳遞到索引/頁面?

非常感謝。

編輯---我將發布完整代碼,最主要的是將在foreach中獲取的PHP變量傳遞給模式窗口,在該窗口中調用iframce到其他視圖以使用該變量:

<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');     
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM