Ken Mayer Ken Mayer - 10 months ago 55
jQuery Question

Pass Values to Bootstrap Modal JavaScript and PHP

I have tried to find this out here and not found what I was looking for.

The Goal: After a process has completed, to display a Bootstrap Modal that notifies the user it is complete (and/or that the admin may need to authorize something, for example, a comment may need to be authorized ...).

I can output a JavaScript script (sounds redundant as I type that) that will open the modal, but I want to pass text to the modal so I can have a reusable dialog. Here's the basic code I am working with:

The modal form:

<!-- language: lang-html -->

<!-- meant to be an alert dialog -->
<div id="myAlert" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg" 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">&times;</span></button>
<h3 class="modal-title" id="descModalLabel"></h3>
</div> <!-- / modal-header -->
<div class="modal-body">
<!-- some text will be inserted here -->
</div><!-- / modal-body -->
<div class="modal-footer" -->
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div><!-- / modal-footer -->
</div><!-- / modal-content -->
</div><!-- /modal-dialog -->
</div> <!--/ modal -->
<!-- end of modal form -->

<!-- end snippet -->

The JavaScript to open the form:

// for the myAlert modal:
// code to open the modal with the caption and description:
$('#myAlert').on('', function (event)
var modal = $(this);
// need to get these values passed somehow
var caption = "Test";
var description = "Test Test Test";
modal.find('.modal-title').text( caption );
modal.find('.modal-body').append( '<strong>Description:</strong> ' + description );

// when modal closes, clear out the body:
$('#myAlert').on('', function ()

The PHP that outputs the script that actually opens it:

echo '<script>';
// how do I pass the variables???
echo '$("#myAlert").modal("show")';
echo '</script>';

I would like to be able to pass the caption and description values to the form, but cannot figure it out. I have a version that works with a button based heavily on the Bootstrap site, but I cannot figure this one out. Thanks in advance!

Answer Source

There are probably better solutions out there but this one works too as I have tested already. It requires to take 3 steps:

  1. You need to declare your variables you want to pass to JavaScript above all events and functions (make them global).

For example:

var global_caption = "Some default caption";
var global_description = "Some default description";

// All scripts now go here
  1. In your modal you assign local variables from global variables (or use global variables directly) for some events, for example:

This will show basically the same thing as in your case so far:

$('#myAlert').on('', function (event)
         var modal = $(this);
         var caption = global_caption;
         var description = global_description;
         modal.find('.modal-title').text( caption );
         modal.find('.modal-body').append( '<strong>Description:</strong> ' + description );
  1. With echo you assign new values to global variables and summon the event again.

Declaration can go as simply as:

echo '<script>';
echo 'global_caption = "This is a new caption"';
echo 'global_description = "This is a new description"';
echo '$("#myAlert").modal("show")';
echo '</script>';