carnival Posted March 15, 2009 Share Posted March 15, 2009 Hi there, Firstly I will apologise in advance, as I am sure that this question has been asked and answered countless times, as it's a basic implementation of PHP, however I have searched using many varying terms and have come up with nothing... maybe I am searching for the wrong stuff, however hopefully my question and thread title may help others doing similar searches to me in the future, so here goes.... I have a page with a menu (don't we all, you might reply) I want to click the menu links and instead of it linking to another page, I just want the content of the 'content' div to change to the new page. Years ago you used to do this with frames, but I hate 'em. How do you do it with PHP? I have an inkling it's something to do with 'GET' & 'POST' but not entirely sure, as I'm a novice and have plodded along with includes up until now to do basic stuff. I now want to start learning a little bit more if possible!!! I also think it will give me a URL similar to the forum one... i.e. http://www.mysite.com/index.php?page=the-page-i-am-including.html Thanks in advance!!! Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/ Share on other sites More sharing options...
wildteen88 Posted March 15, 2009 Share Posted March 15, 2009 The basics of the code would look like <?php // check that the page var is present in the url // eg index.php?page=home.html if(isset($_GET['page']) && GET['page'] != 'index.php') { // now check to see if the requested file exists $reqFile = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['page']; if(file_exists($reqFile)) { include $reqFile; } // request file does exist. Kill the script display error else die('404 Page Not Found'); } // page is present, display the default page of your choice else include 'your_default_page.php'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/#findComment-785246 Share on other sites More sharing options...
fesan Posted March 15, 2009 Share Posted March 15, 2009 i use a switch for this matter.... switch($_GET['page']){ //List produkter case 'list' : include('source/list.php'); break; default: include('start.php'); } my links looks like this: <a href="index.php?page=list">nameoflink</a> Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/#findComment-785251 Share on other sites More sharing options...
carnival Posted March 15, 2009 Author Share Posted March 15, 2009 Thank you! How fast was that?! I shall give that a try.... I think I can see what's going on there. Just one question - Would this code be used for each of the links in the nav part of the page, or in the content div that is going to change? <div id="wrapper"> <div id="header"></div> <div id="nav"><?php include("includes/nav.php") ?></div> <div id="content"> <?php // check that the page var is present in the url // eg index.php?page=home.html if(isset($_GET['page']) && GET['page'] != 'index.php') { // now check to see if the requested file exists $reqFile = $_SERVER['DOCUMENT_ROOT'].'/'.$_GET['page']; if(file_exists($reqFile)) { include $reqFile; } // request file does exist. Kill the script display error else die('404 Page Not Found'); } // page is present, display the default page of your choice else include 'home.php'; ?> </div> <div id="footer">Copyright info</div> </div> and nav.php would be something like : <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Gallery</a></li> <li><a href="#">Guestbook</a></li> <li><a href="#">Contact</a></li> </ul> So what would the links be?... Similar to fesan's reply above? However... Can you explain this a little more, as I am not too sure what's going on? i use a switch for this matter.... switch($_GET['page']){ //List produkter case 'list' : include('source/list.php'); break; default: include('start.php'); } my links looks like this: <a href="index.php?page=list">nameoflink</a> Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/#findComment-785255 Share on other sites More sharing options...
wildteen88 Posted March 15, 2009 Share Posted March 15, 2009 Just one question - Would this code be used for each of the links in the nav part of the page, or in the content div that is going to change? Yes and nav.php would be something like : <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Gallery</a></li> <li><a href="#">Guestbook</a></li> <li><a href="#">Contact</a></li> </ul> So what would the links be? No, the link will be like you said earlier, eg http://www.mysite.com/index.php?page=the-page-i-am-including.html Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/#findComment-785258 Share on other sites More sharing options...
fesan Posted March 15, 2009 Share Posted March 15, 2009 The switch checks the value of the variable page. It gets it page variable from the header. so the header after clicking this link: <a href="index.php?page=list">nameoflink</a> will look like this: http://www.yourdomain.com/index.php?page=list By changing the value in page= you set the different cases for you switch. I've added another case under so it might be a little more easier to understand. switch($_GET['page']){ case 'list' : include('source/list.php'); // link would have to look like: <a href="index.php?page=list">nameoflink</a> break; case 'home' : include('source/home.php'); //link would have to look like: <a href="index.php?page=home">nameoflink</a> break; default: include('start.php'); // This is the default page that is loaded. If the $_GET['page'] dont get any values. } Hope this helps.... I guess that wildteen has a more complex script for including sites. The result will be the same i guess but i don't know if any of these methods is more preferred then the other... Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/#findComment-785261 Share on other sites More sharing options...
carnival Posted March 15, 2009 Author Share Posted March 15, 2009 Thanks. I will take a look later on, as I have to pop out now. I will report back with my results. I think I understand what's going on... if not, there will be more questions!!! C Quote Link to comment https://forums.phpfreaks.com/topic/149521-changing-content-within-a-page-using-php-instead-of-frames/#findComment-785265 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.