<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.7.2.custom.min.js"></script>
Your jQuery UI download should have also included a base theme for the library. It'd be under the css directory in the zip file. Let's go ahead and move that into our site too, and add import the stylesheet onto the page.
<link type="text/css" href="/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
I'm also going to create one last JS file, I'll call it ajax-dialog-forms.js, in my js folder, and include that on my page.
<script type="text/javascript" src="/js/ajax-dialog-forms.js"></script>
<div>
<form action="saveData.php" method="post">
<p>Task: <input type="text" name="task" /></p>
<p><input type="submit" name="submit" value="Add Task" /></p>
</form>
</div>
Simple form - just has one input and a submit button. For the purpose of this tutorial I'm not going to go through the whole process of actually saving this to the database, etc.; I'll assume that you can do that on your own or find other examples on how to do so - it's outside the scope of this article. But, before continuing, you should make sure that this form works properly.
$(document).ready(dialogForms);
function dialogForms() {
$('a.dialog-form').click(function() {
var a = $(this);
$.get(a.attr('href'),function(resp){
var dialog = $('<div>').attr('id','formDialog').html(resp);
$('body').append(dialog);
dialog.find(':submit').hide();
dialog.dialog({
title: a.attr('title') ? a.attr('title') : '',
modal: true,
buttons: {
'Save': function() {submitFormWithAjax($(this).find('form'));},
'Cancel': function() {$(this).dialog('close');}
},
close: function() {$(this).remove();},
width: 'auto'
});
}, 'html');
return false;
});
}
function submitFormWithAjax(form) {
form = $(form);
$.ajax({
url: form.attr('action'),
data: form.serialize(),
type: (form.attr('method')),
dataType: 'script'
});
return false;
}
OK, so, little by little.
$(document).ready(dialogForms);
This line is responsible for telling the browser that when the DOM is ready, we want to run the dialogForms function (see
http://docs.jquery.com/Events/ready).
$('a.dialog-form').click(function() {
Next, we will execute an AJAX GET request using jQuery's built-in $.get function. We will request the url specified by the link's href attribute, and when the request completes we will execute a function and pass it the response (which is the HTML of the page).
$this->_request->isXmlHttpRequest()
to detect if it's an AJAX request. Luckily, however, jQuery automatically sets the X-Requested-With header when it makes a request, so you always have a fall back. I'll check this header in my PHP script to see how to handle the response:
<?php
// Check the X-Requested-With header to see if this is an AJAX request
$isAJAX = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ? true : false;
// We've got access to our $_POST variables as usual
$task = $_POST['task'];
// manipulate, save to database, whatever
if ($isAJAX) {
echo "alert('the task '" . $task . "' was saved!');";
} else {
header("Location:saved.html");
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (12)
Commented:
for file upload. Please Give me any resource
Thankfully
Anes
Commented:
Best,
Mike
Commented:
Commented:
instead of the return false you have now
Commented:
View More