Jump to content

[SOLVED] Change Page Title With Ajax


fanfavorite

Recommended Posts


I have been thinking about ways to change the page title with Ajax, but have a problem with how I am doing it.  This site uses PHP, AJAX and MySQL.  MySQL has all the page titles and data from the site.  Here is the function called when a new page is clicked: 

 

function loadpage(page,gal,pg) { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
	alert ("Browser does not support HTTP Request");
	return;
}
<? 
include('../includes/connection.php');
$t = mysql_query("select * from Pages"); 
while($g=mysql_fetch_array($t)) {
	if ($g[siteName] != "") {
		$site = $g[siteName];
	}
}
$z = mysql_query("select * from Pages WHERE Filename LIKE '$_GET

'");
$y = mysql_fetch_array($z);
$pgname = $site; 
if($site != "" AND $y[PageName] != "") { 
	$pgname .= " - "; 
} 
$pgname .= $y[PageName]; 
?>document.title = "<? echo $pgname ?>";
var url="js/pageinfo.php";
url=url+"?page="+page;
url=url+"&gallery="+gal;
url=url+"&gstart="+pg;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=function () {
	stateChange("changepage");
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

 

Now this works fine, except I need to be able to get the value of page into the mysql statement where there is a value $_GET


.  I would normally just put http://www.mydomain.com/javascriptpage.php?page=Some Page Name, however I cannot because the page doesn't actually reload to change the value of page in the $_GET.  I know I cannot use a javascript variable in PHP because PHP is server side and javascript is client side, so PHP executes first. 

 

Anyone have any ideas on how I can get the page value to use in my MySQL statement?

Link to comment
Share on other sites

  • 9 months later...

If you never found a solution here is the one i came up with and works fine..

 

For your java:

 

function index_get($link)
    {         
     var req = CreateXmlHttpObject(); // fuction to get xmlhttp object
     if (req)
     {
      req.onreadystatechange = function()
     {
      if (req.readyState == 4) { //data is retrieved from server
       if (req.status == 200) { // which reprents ok status                    
         document.getElementById('content_body').innerHTML=req.responseText;//put the results of the requests in or element
	 var title = document.getElementById('title').innerHTML
	 document.title = title
      }
      else
      { 
         alert("There was a problem while using XMLHTTP:\n");
      }
      }            
      }        
    req.open("POST", "calls/index_get.php?" + $link, true); //open url using get method
    req.send(null);//send the results
     }
    }  

 

here i pass a simple link from my database build menus link field "product=18" or "page=3"

 

my index_get.php explodes that into 'product' and '18' then gets the info for that product from the database containing a title field.. i then have that title asigned to a hidden div

 

echo "<div id='title' style='display: none'>New Title</div>";

 

this div wont be displayed and once the page is loaded the ajax gets the value of the title div and then using javascript replaces the title.. the content of the index_get.php is processed and displayed before the var title is asigned.

 

         document.getElementById('content_body').innerHTML=req.responseText;//put the results of the requests in or element
	 var title = document.getElementById('title').innerHTML
	 document.title = title

 

hope that helps.. its been a while since you posted but i couldnt find the solution either.. had to figure it out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.