fanfavorite Posted September 26, 2008 Share Posted September 26, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/125898-solved-change-page-title-with-ajax/ Share on other sites More sharing options...
fanfavorite Posted September 30, 2008 Author Share Posted September 30, 2008 I guess I will have to include the page name in my loadpage call on the previous page, so I don't have to recheck the database. Quote Link to comment https://forums.phpfreaks.com/topic/125898-solved-change-page-title-with-ajax/#findComment-653967 Share on other sites More sharing options...
danieliser Posted July 29, 2009 Share Posted July 29, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/125898-solved-change-page-title-with-ajax/#findComment-886369 Share on other sites More sharing options...
fanfavorite Posted July 30, 2009 Author Share Posted July 30, 2009 Didn't really work with it after this, but just tried your solution and it works like a charm. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/125898-solved-change-page-title-with-ajax/#findComment-887183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.