How to Easily Create a Javascript Framework part 4
AJAX FORM CODE
//AJAX FORM EXAMPLE
//Onclick in the button process
$$.getById('process').on('click', function(){
//Take the values of the two textbox
var txtname = $$.getById('txtname').getValue();
var txtemail = $$.getById('txtemail').getValue();
var txtmsg = $$.getById('txtmsg').getValue();
//Send the request by AJAX
//processform.php contains an sleep of 3 seconds in order to let loading method work.
/*You can create your own php file with this code ' . ' Email: ' . $_POST['email'] . ' ' . 'Message: ' . $_POST['message'];?> */
$$.AJAX.sendRequest('post', 'processform.php', {
name: txtname,
email: txtemail,
message: txtmsg
}, {
success: function(myxhr){
$$.getById('answer').innerHTML(myxhr.responseText).setStyle({
color: 'green'
});
},
error: function(myxhr){
$$.getById('answer').innerHTML(myxhr.statusText).setStyle({
color: 'red'
});
},
loading: function(){
$$.getById('answer').innerHTML('Loading...').setStyle({
color: 'blue'
});
}
});
});
Use of UN to delete events
UN METHOD CODE:
//SECOND EXAMPLE:
//The use of the "UN" method
//The button should work once because inside the function
//to run we delete it from the element
//Function to be executed
function hello(){
alert('Hellow World');
$$.getById('secondexample').un('click',hello)
}
$$.getById('secondexample').on('click',hello);