I have a Joomla website and I am trying to automatically open a modal window with a web page whose parameters are passed in the URL.
The website capability goes like this: A trip leader looks up a trip and he can schedule to lead that trip by clicking on a button. When the button is clicked I have working Php code that saves the trip data to a calendar database and retrieves the id number of the row. The next step is to edit the entry in the calendar component after passing the information in the URL. I created the correct URL in a Php variable but I am stuck on how to automatically open a modal window with the page for editing .
Here is some of the code I have:
<form action="" method="post">
<input type="submit" name="submit" id="submit" value="Lead This Trip" />
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {...code to save data to calendar}
?>
At the end I have a variable called $url which contains the correct URL for opening the modal website.
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<style>
/*hide the modal container in the beginning */
#modal {
display: none;
}
</style>
<!-- this is where your html will populate -->
<div id="modal"></div>
<form action="" method="post">
<input type="hidden" name="action" id="submit" value="leadteam" />
<input type="submit" name="submit" value="Lead This Trip" />
</form>
<script>
// When the page has loaded
$(document).ready(function() {
/*
If you need to only have it happen on one or more times, but not every time,
you can use a selector such as id="whatever" in your form:
$('#whatever').submit(....etc
or you want perhaps some but not all forms, use class="whatever" in your form:
$('.whatever').submit(....etc
*/
$('form').submit(function(e) {
// prevent the natural submission of the form
e.preventDefault();
// create an ajax instance
$.ajax({
/*
This is the where your edit page is located
If you were to use different forms, you can use the
attr in jquery to grab the action="/whatever.php" in the form
url: $(this).attr('action'),
This will allow you to use this same code without copy pasting
but will then allow for any the forms in your site
*/
url: '<?php $url ?>',
// This function turns your submission to a post string
data: $(this).serialize(),
// Sends as a post
type: 'post',
// If the ajax is successful
success: function(response) {
// Fade in the modal window
$('#modal').fadeIn(function() {
// Populate with the content on your edit page
$(this).html(response);
});
}
});
});
});
</script>
Here is the relevant code I pasted into my Php file.