Hello users!, today i will be teaching you guys how to update a user status using "JavaScript" for example, "Last Doing: On the homepage".Please note you will need to have a understanding of the following...
- function
- var object
- XMLHttpRequest
- setRequestHeader
- object.send();
- object.open();
Below are definition of some of the following listed above.
Function A function is pretty much a blank custom call back function(if someone call it). When we create a function we can give it a name, this also include the following braces as well { }.
function updat()
{
// code insert here
}
Var Object Object is just a kind of "methods" also a "properties". The properties is the value of the Object for example var dObj. The Object is dObj or value. It can also be use for Method(action) that can be performed on Object.
var dObj
XMLHttpRequest XMLHttpRequest is a object that is used to send data behind the scenes.(with out having to perfroming action method to send data to the server).
new XMLHttpRequest();
XMLHttpRequestHeader Its used to tell the server which content type is used for this request.
setRequestHeader("Content-type", "application/x-www-form-urlencoded"); Now that you have a understanding of what some of the following codes listed above do we can move on create our script.
// 1 create a call back function with id. doing is our id
// create object
var dObj;
function updat(doing)
{
// 2 create the HttpRequest() with the following object we just created
dObj = new XMLHttpRequest(),
// 3 create the php update file where we are sending the data to.
file ="../updat_user_status.php";
// 4 open the file where we are sending the data to.
Full script function updat(doing) {
dgObj = new XMLHttpRequest(),
file = "../updat_user_status.php";
dgObj.open("POST",file,true);
dgObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
dgObj.send("updat="+doing);
return true;
}
Now all we have to do is call the function back and the data will be send to the file we created.
<script>
// call back the script we created
function updat("creating a message....");
</script>
Remember to save the script in a file call "function.js" then call that file on even page with the call back function above that we created.
If you feel that this lesson was helpful please +1 and leave a response. I do apologize for the English if any.