Put your name in the form below and click to witness the magic of Ajax.

Client Side Code:
Note that the Ajax call is expecting the return data to be JavaScript. When it comes back the line of generated code is executed. Unlike samples one and two, the eval() function is not needed.


$(document).ready(function(){
	
	$('#btn1').click(function(e) {
        
		e.preventDefault();
		
		$.ajax({
 		type: "POST",
  		url: "test3.php",
  		cache: false,
  		data: "inputName="+$("#inputName").val(),
  		dataType: "script",
  		success: function(script){}//This needs to be here to define and act on the success event,
        //but it can be empty since the return is executable JavaScript
});
		
    });



});

Server Side Code:
Note the header telling the browser this content is JavaScript. You can see the JavaScript code piece being built from the form input and static string contents. It is good practice to have the Content-type header matching the data type the Ajax call is told to expect in line 12 above.