javascript - xmlhttp response Text -
i trying update particular div of web page without reloading complete page. instead on response complete web page gets inserted in targetid. using javascript function so. here code of php javascript function called.
if($check_like==0) { // if user has not liked post $divid_like = $postid.'like'; echo "<div id='$divid_like'><a href='#' onclick=\"ajax_post('ajax.php?action=like-status','postid=$postid','$divid_like',0);\" name='like'> </a> </div>"; //echo "<a href='action.php?action=like-status&postid=$postid' name='unlike'> </a>"; echo "<a href='#' onclick='' > comment </a>"; echo "<a href='#' onclick='' > share </a>"; } here javascript function :
<script language="javascript"> function ajax_post(url , args , targetid, add) { var xmlhttp; if(window.xmlhttprequest) { // works browser abve ie7, chrome , firefox xmlhttp = new xmlhttprequest(); } else { //for ie5 , ie6 don't think nedd handle still xmlhttp = new activexobject("microsoft.xmlhttp"); } if(!xmlhttp) document.getelementbyid(targetid).innerhtml = "somesdlkvnoisnvoanfvnfnerroor"; xmlhttp.open("post",url, false); xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded"); //xmlhttp.setrequestheader("content-length", args.length); //xmlhttp.setrequestheader("connection", "close"); xmlhttp.onreadystatechange=function() { var xhr = event.target; if(xhr.readystate==4 && xhr.status==200) { if(add==0) { document.getelementbyid(targetid).innerhtml = xhr.responsetext; } else { document.getelementbyid(targetid).innerhtml+= xhr.responsetext; } } else { document.getelementbyid(targetid).innerhtml = "somebigbigbigrroor"; } }; xmlhttp.send(args); } ajax.php file being called process data:
<?php include_once("includes/master_login.inc"); include_once("includes/connect_to_database.php"); include_once("includes/users.php"); include_once("includes/friends.php"); include_once("includes/current_page_url.php"); include_once("javascript/xml.js"); include_once("profile_display.php"); include_once("includes/status_likes_comment.php"); include_once("includes/message.php"); include_once("includes/newsfeed.php"); include_once("sendmessage.php"); $action = $_get["action"]; $uid = $_session['uid']; //if(!user_online($uid)){$action=0;} if($action=="add-friend") { add_friend($uid,$_post["uid"]); if($_post["refresh"]="profile") echo display_profile($_post["uid"]); } if($action=="respond-friend-request") {friend_accept($_post["uid"],$uid); if($_post["refresh"]="profile") echo display_profile($_post["uid"]); } if($action=="like-status") { like_status($uid,$_post["postid"]); echo "unlike"; } if($action=="chat-send") {add_message($uid,$_post["to"],$_post["message"]); echo display_message($uid,$_post["to"]);} //if($action=="chat-send") {echo $_post["message"]; delay(1000);} ?> on liking status instead of being replaced unlike complete page gets loaded in div.
Comments
Post a Comment