简体   繁体   中英

PHP variable to javascript + modal

I want to create modal button for delete a user gets from a foreach in the html body. The modal opens an iframe with a form. The iframe url seems like

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

I want to pass to the iframe src the php variable contains an userid, for example:

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

First code works fine and it shows the view but no variable passed with the second code. I use a javascript for open the modal and get the ID variable. The link:

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

And the js:

        <script>
    $(".myModal").on("click", function(e) {
      var username;

          e.preventDefault();

          username = $(this).data("detail-id");


      });
    </script>

How to get the detailId to the iframe src to pass the variable to the index/page?

Thanks a lot.

EDIT--- I will post full code, the ovjective is to pass a PHP variable got in a foreach to a modal windows in that calls a iframce to other view to use the variable:

<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>

Thanks!!

You are mixing JS and PHP together:

<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 )

In PHP, you append with . operator

Also if you are looking for easy solution to delete smoething (ex. user) you can use something like this in your view:

<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 ?>

and from your user model try somethig like:

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

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM