Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send parameter to Bootstrap modal window?

Tags:

I have a problem, I cannot pass any parameters to a modal window (using Bootstrap 3), I tried using the solution stated in this link, but I cannot make it work:

Dynamically load information to Twitter Bootstrap modal

(Solution kexxcream user).

But pressing the button, the display does not show me anything, apparently blocks, attached a picture below:

http://i.imgur.com/uzRUyf1.png

I have attached the code (same code post above left);

HTML Code:

      <div id="myModal" class="modal hide fade">       <div class="modal-header">         <button class="close" data-dismiss="modal">&times;</button>         <h3>Title</h3>       </div>       <div class="modal-body">                       <div id="modalContent" style="display:none;">            </div>       </div>       <div class="modal-footer">         <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>       </div>   </div>  

JS Code:

    $("a[data-toggle=modal]").click(function()      {            var essay_id = $(this).attr('id');          $.ajax({             cache: false,             type: 'POST',             url: 'backend.php',             data: 'EID='+essay_id,             success: function(data)              {                 $('#myModal').show();                 $('#modalContent').show().html(data);             }         });     }); 

Button:

<a href='#myModal' data-toggle='modal' id='2'> Edit </a> 

PHP Code (backend.php):

<?php $editId = $_POST['EID']; ?>   <div class="jumbotron">    <div class="container">     <h1>The ID Selected is <?php echo $editId ?></h1>    </div>   </div> 
like image 368
user3759455 Avatar asked Jun 20 '14 09:06

user3759455


People also ask

How do I pass data to modal popup?

Data can be passed to the modal body from the HTML document which gets displayed when the modal pops up. To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code.

How can I get dynamic data from Bootstrap modal?

Load Dynamic Content from Database in Bootstrap ModalBy clicking the Open Modal ( . openBtn ) button, the dynamic content is loaded from another PHP file ( getContent. php ) based on the ID and shows on the modal popup ( #myModal ).

How can I show data using a modal when clicking a table row JavaScript?

js: function orderModal(order_id){ var tr = $('#order_' + order_id); // get the current info in html table var customer_id = tr. find('. customer_id'); var status = tr.


1 Answers

There is a better solution than the accepted answer, specifically using data-* attributes. Setting the id to 1 will cause you issues if any other element on the page has id=1. Instead, you can do:

<button class="btn btn-primary" data-toggle="modal" data-target="#yourModalID" data-yourparameter="whateverYouWant">Load</button>  <script> $('#yourModalID').on('show.bs.modal', function(e) {   var yourparameter = e.relatedTarget.dataset.yourparameter;   // Do some stuff w/ it. }); </script> 
like image 52
Joe Avatar answered Oct 12 '22 08:10

Joe