How to build a simple Ajax

How to create a simple Ajax call to an other PHP file

How it works?

We use one HTML file with a simple JavaScript function wich call an Ajax.

The function sends data to the Ajax and the Ajax returns something else.

In my example the ajax just returns some text.

function load_ajax(){

// Retrieving the input value
var my_text = $('#my_text').val();

// Send the Data, using Post, to the Ajax.php file.
html = $.ajax({
data: "my_text="+my_text,
type: "POST",
url: "ajax.php",
async: false
}).responseText;

// We put the returned text into the Container.
$('#container').html(html);

}


Full HTML

The whole html file with the form and the JavaScript functions.





Example 15 | CodeGround

Example 15 | CodeGround

Write something:

Click here


Go back and leave a Comment


PHP

The PHP wich will return our data has:


About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.
2 comments

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*