Here’s a typical jQuery AJAX example, and what you need:
$("form").submit(function(e){
e.preventDefault();
var form_data = $(this).serialize();
var form_url = $(this).attr("action"); // ou somente teste.php no seu caso
var form_method = $(this).attr("method").toUpperCase();
$("#loadingimg").show();
$.ajax({
url: form_url,
type: form_method,
data: form_data,
cache: false,
success: function(returnhtml){
$("#loadingimg").hide();
$("#result").html(returnhtml);
}
});
});
The first part of this code does not yet have to do with ajax. It is an "Event Handler" that intercepts the time/event when the form is submitted.
The code e.preventDefault();
stops this action. The e
is the event-object that will be stopped, ie the page will not reload.
The following lines are to prepare respectively the data to send, the server-side url, the type of method (typically POST or GET).
The line $("#loadingimg").show();
is optional and is in case you have an image, or text you want to display while the connection is made so that the user knows there is processing on the server.
With the call of this method $.ajax({
begins the AJAX part.
The ajax is exactly to load content from the server without having to refresh the page. Here the parameters defined in variables above will be used.
The perhaps most important part is success: function(returnhtml){
. Here is the callback call. The function that runs when the AJAX request receives data back from the server. html is passed in the variable returnhtml
.
In this example, the first line hides the image you were showing during the request. The second line is what you are looking for, i.e. $("#result").html(returnhtml);
which will insert the new content into the DOM element with the ID result
Want to re-load the page or open the new content on the same page? In the second case you can put the respective HTML?
– Sergio
@Sergio The complete code did not fit here, so I made this link. http://jsfiddle.net/9w2zW/ Thanks.
– fabricio_wm
Since the button will be enabled by JS, exchange the <a> for a <span> and request it by JS itself, whether AJAX populates some element or a window.Location.
– Bruno Augusto
@fabricio_wm, I will return jsFiddle to the question (next time you can click "edit" to improve the question with more infoJão). However you can answer my question in the first comment?
– Sergio
Poxa @Sergio, I’m sorry but I thought you already understood that it’s for navigation since you participated in the previous post. Thanks.
– fabricio_wm
@fabricio_wm, I realize it’s for navigation. What I still don’t know is if you want to load only one
div
with the new content, or if you want to load the whole page with the new content.– Sergio
I’m sorry. Now it’s clearer. It was page, but if you can load div’s, it’s even better. Thanks.
– fabricio_wm
@Brunoaugusto <br /> How I do the Javascript or Ajax part?
– fabricio_wm
@fabricio_wm, you need to use ajax. You are using some library like Mootools or jQuery?
– Sergio
It can be Jquery. <br /> But I’m a beginner. <br /> Thank you.
– fabricio_wm
@Brunoaugusto or Someone can help me?
– fabricio_wm