Sample Script- Profile Wall Of Facebook

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 22, 2009, 03:22 AM
430661 members and 297813 Topics
Latest Member: pstkriz
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderators: OmniPotens, yawa-ti-de)  |  Sample Script- Profile Wall Of Facebook
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Sample Script- Profile Wall Of Facebook  (Read 1621 views)
*dhtml
Sample Script- Profile Wall Of Facebook
« on: January 04, 2009, 08:32 PM »

We want to build a very simple profile wall using ajax.

The download will be found on www.mwebng.net/?net=dl
The demo is http://mwebng.net/demos/profile_wall/

Meanwhile, if you are not familiar with ajax, please lookup my thread on http://www.nairaland.com/nigeria/topic-214605.0.html - that should teach
you fundamentals of ajax.

The codes will follow shortly.

*dhtml
Re: Sample Script- Profile Wall Of Facebook
« #1 on: January 04, 2009, 08:35 PM »

Comprise 3 files:
wall.txt - for holding the data - we are not using dbase in this one.
post.php - for saving entry into wall.txt
index.html - our main file

In the download version, the wall.txt is empty

post.php
Code:
<?php
$text
="<hr align='left'>".date("m:i:s ").$_REQUEST['text'];

$handle fopen("wall.txt""a");
fwrite($handle,$text);
fclose($handle);

echo 
$text;
?>



index.html
Code:
<title>Profile Wall</title>
<script>
function ajaxRequest() {
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i])} catch(e){alert("Failed");}}
} else if (window.XMLHttpRequest) {return new XMLHttpRequest()} else {return false}
return;
}

function download(url) {
var mygetrequest=new ajaxRequest();
url+="?&"+new Date().getTime();
    mygetrequest.onreadystatechange=function(){
     if (mygetrequest.readyState==4){
      if (mygetrequest.status==200) {document.getElementById('display').innerHTML=mygetrequest.responseText;}
  else {
  //alert("Download was not successful");
  }
}
}
mygetrequest.open("POST", url, true);
mygetrequest.send(null);
}


function post2(url,flag) {
var mygetrequest=new ajaxRequest();
url+="&"+new Date().getTime();
    mygetrequest.onreadystatechange=function(){
     if (mygetrequest.readyState==4){
      if (mygetrequest.status==200) {
  var d=document.getElementById('display');
  d.innerHTML=mygetrequest.responseText+d.innerHTML;
  document.getElementById('textbox').value="";
  }
  else {
  alert("Connection failed.");
  }
}
}
mygetrequest.open("POST", url, true);
mygetrequest.send(null);
}


function post(text) {
if(text.value=="") {return;}
post2("post.php?text="+text.value);
}
</script>

<style>
input.text {width:200px;}
hr {width:200px;text-align:left;}
</style>

<body onload="download('wall.txt');">

Type Text Here and press enter:<br>
<input class="text" id="textbox" type="text" style="wdith:200px" onkeypress="if(event.keyCode==13) {post(this);}">

<div id="display"></div>

</body>

Explanations will follow in the next post.
*dhtml
Re: Sample Script- Profile Wall Of Facebook
« #2 on: January 04, 2009, 08:44 PM »

Our PHP file
Code:
<?php
$text
="<hr align='left'>".date("m:i:s ").$_REQUEST['text'];

$handle fopen("wall.txt""a");
fwrite($handle,$text);
fclose($handle);

echo 
$text;
?>


The first line: $text="<hr align='left'>".date("m:i:s ").$_REQUEST['text'];
just appends an HR element, followed by date and the text you entered in the textbox from the main page.

The next functions will append [add to the end of wall.txt]
Code:
$handle = fopen("wall.txt", "a");
fwrite($handle,$text);
fclose($handle);

Then finally the script will spit out the
processed text
echo $text;
which is picked by the same ajax that called it and appended to the begining of the "display" div used in the main page.


index.html
Code:
<title>Profile Wall</title>
<script>
function ajaxRequest() {
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){try{return new ActiveXObject(activexmodes[i])} catch(e){alert("Failed");}}
} else if (window.XMLHttpRequest) {return new XMLHttpRequest()} else {return false}
return;
}

function download(url) {
var mygetrequest=new ajaxRequest();
url+="?&"+new Date().getTime();
    mygetrequest.onreadystatechange=function(){
     if (mygetrequest.readyState==4){
      if (mygetrequest.status==200) {document.getElementById('display').innerHTML=mygetrequest.responseText;}
  else {
  //alert("Download was not successful");
  }
}
}
mygetrequest.open("POST", url, true);
mygetrequest.send(null);
}


function post2(url,flag) {
var mygetrequest=new ajaxRequest();
url+="&"+new Date().getTime();
    mygetrequest.onreadystatechange=function(){
     if (mygetrequest.readyState==4){
      if (mygetrequest.status==200) {
  var d=document.getElementById('display');
  d.innerHTML=mygetrequest.responseText+d.innerHTML;
  document.getElementById('textbox').value="";
  }
  else {
  alert("Connection failed.");
  }
}
}
mygetrequest.open("POST", url, true);
mygetrequest.send(null);
}


function post(text) {
if(text.value=="") {return;}
post2("post.php?text="+text.value);
}
</script>

<style>
input.text {width:200px;}
hr {width:200px;text-align:left;}
</style>

<body onload="download('wall.txt');">

Type Text Here and press enter:<br>
<input class="text" id="textbox" type="text" style="wdith:200px" onkeypress="if(event.keyCode==13) {post(this);}">

<div id="display"></div>

</body>

I have already done justice to most of the functions in this in my ajax thread which i mentioned above.
I will only explain the necessary functions.

When the page loads, it will call the download function which will dump the entire wall.txt into the display div object.
Code:
if (mygetrequest.status==200) {document.getElementById('display').innerHTML=mygetrequest.responseText;}

When u type into the textbox and press enter - it will detect this and call the post function
Code:
function post(text) {
if(text.value=="") {return;}
post2("post.php?text="+text.value);
}

The post function evaluates if you have actually entered any data - if not it exits.
If you have entered data - it calls the post2 function and passes a url like post2.php?text=yo!

post2.php will append it and post back the processed data as reponseText which is now added to the already growing list.

document.getElementById is used to access elements that you have assigned ID for earlier.
innerHTML is used to manipulate the html content being displayed by an object.
*dhtml
Re: Sample Script- Profile Wall Of Facebook
« #3 on: January 04, 2009, 08:45 PM »

To install this on your server, dont forget to change the attribute/permission/chmod the wall.txt to 755
 Setting up a development server for testing purposes.   What Site Have You Designed Best?  Which Web Programming Language Are The Hardest & The Easiest?  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.