Snippet from my HTML code.
<td><span data-placement="top" data-toggle="tooltip" title="Show"><a href="#" class="btn btn-primary btn-xs" data-toggle="modal" data-target="#editBox" data-book-id="<?php echo $obj->id;?>"><span class="glyphicon glyphicon-pencil"></span></a></span></td>
The modual that are beeing opened when link is clicked:
<!-- Modal -->
<div class="modal fade" id="editBox" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
<?php var_dump($_GET)?>
</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>
Is there a proper way to pass my id into the modal?
The simple solution to pass
id
, fetch records from database against passedid
and show in modal is;Simple Solution
Modal Call button
Modal HTML
Put following modal HTML outside the
while loop
in page where the above call button is located (preferable at bottom of page)Now Create a PHP file and name it
file.php
This file is called with modal call button
href="file.php?id=<?php echo $obj->id;?>"
To remove the data inside modal or in other words to refresh the modal when open next records without page refresh, use following script
Put it after jQuery and Bootstrap library (Remember jQuery & Bootstrap libraries always come first)
Alternate Solution with Ajax and Bootstrap Modal Event Listener
In Modal Call button replace
href="file.php?id=<?php echo $obj->id;?>
with data attributedata-id="<?php echo $obj->id;?>"
so we pass the id of row to modal using bootstrap modal eventModal HTML
Put following modal HTML outside the
while loop
in page where the above call button is located (preferable at bottom of page)Now Add following script in same page;
Now Create a PHP file and name it
file.php
(same as use in Ajax Method)In this solution, you don't need following script to remove the data inside modal or in other words to refresh the modal
Alternate Solution with Ajax and jQuery Click function
Modal Call button
Put following modal HTML in page where above modal call button located (preferable at bottom of page)
following Ajax method code in same page where Modal HTML & Modal call button located.
And the PHP file
file.php
will be same as the above solution with bootstrap modal eventPass On page information to Modal
In some cases, only need to pass (display) minimal information to modal which already available on page, can be done with just bootstrap modal event without
Ajax Method
withdata-attributes
Modal Event