FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


targeting <div> tags with links





eday2010
I was wondering if there was a way to have links target a specific <div> with an ID the same way you can target a frame. I only want items to load in the one <div> when a user clicks on a link instead of reloading the whole page.

Is there a php method to this? how does that php thing work where the url changes to blah.com/home.html?id=something ? How complicated is it to get a site set up to work like that? Would it have to be a database-driven page for that to work?
smartbei
If I am understanding you correctly, you are trying to repalce a site with frames witha site that uses php to seperate the content from the navbar, banner, etc.

The most basic way to do this would be to have one index.php file, and all the rest as .htm's. Then, in the index file (where you want the content to show up):

Code:

include $_GET['page'] . '.htm';


The URL's would look like: www.so-and-so.com/index.php?page=something.

Of course, this is a major security vulnerability if you do it this way, so certain precautions are recommended. For one, you can check if it matches a basic regular expresion, and also if it exists on your server:
so replace the above code with:

Code:

if (preg_match("/^[A-Z0-9_\.\-]+$/i", $_GET['page']) && file_exists($_GET['page'] . '.htm')) {
 include '$_GET['page']. '.htm';
}


This is more secure, though there is probably a way to bypass it as well.
vln004
would be a good idea to look into using ajax to load data into that div. Erm, i wouldn't know where to begin (i've been putting off learning it) so my best advice would be to search google for a good ajax library. I've been keeping a close eye on jquery because it's a very small package with very little clutter and bloat.


good luck
eday2010
smartbei wrote:
If I am understanding you correctly, you are trying to repalce a site with frames witha site that uses php to seperate the content from the navbar, banner, etc.

The most basic way to do this would be to have one index.php file, and all the rest as .htm's. Then, in the index file (where you want the content to show up):

Code:

include $_GET['page'] . '.htm';


The URL's would look like: www.so-and-so.com/index.php?page=something.



Basically I want the content in the middle div on my page to change while everything else stays the same. Do I understand correctly that I would put the snippet of code you provided into the div I want to change? Thus when someone clicks a link just that content changes? What do I have to put as the link in my nav bar? Does that require some sort of php command? The menu will be done in Flash.
hyhy
eday2010 wrote:
smartbei wrote:
If I am understanding you correctly, you are trying to repalce a site with frames witha site that uses php to seperate the content from the navbar, banner, etc.

The most basic way to do this would be to have one index.php file, and all the rest as .htm's. Then, in the index file (where you want the content to show up):

Code:

include $_GET['page'] . '.htm';


The URL's would look like: www.so-and-so.com/index.php?page=something.



Basically I want the content in the middle div on my page to change while everything else stays the same. Do I understand correctly that I would put the snippet of code you provided into the div I want to change? Thus when someone clicks a link just that content changes? What do I have to put as the link in my nav bar? Does that require some sort of php command? The menu will be done in Flash.


Use php it's ****** (sorry im lil drunk) powerful. And yeah just pick one if or switch in your php script for example:
<?
if (file_exists($_GET['page'] . '.htm') {
include '$_GET['page']. '.htm'; }
else die;
?>

as it been said and that will put those specified htm files on your site. And you will specify which page by www.yourdomian.com/index.php?page=******. PHP ftw!
Marston
Check out this AJAX script - it's really easy to use.

http://dynamicdrive.com/dynamicindex17/ajaxcontent.htm

Hope that helps!
eday2010
I'll definately be trying the ajax script. it does just what i want. i'd like to do it in PHP though since I am wanting to learn how to use PHP. I am still unlcear as to what I have to put into the actual link for it to know that it has to talk to the PHP in the div where I want the content loaded.
Marston
Well, the AJAX script really doesn't require php... But anyways, here's a simple way to do what you want in PHP...
Code:
<div class="MyDiv"><?php
switch ($id){
default:
echo 'blah';
break;
case "more-content":
echo 'This is more content.';
break;
}
?></div>
Basically, what this does is: by default, output "blah", but when the page is accessed like page.php?id=more-content, it outputs "this is more content". You can then create a link like this:
Code:
<?php
switch ($id){
default:
echo '<a href="page.php?id=more-content">View more content</a>';
break;
case "more-content":
echo '<a href="page.php">View original content</a>';
break;
}
?>
eday2010
Is that how the PHP would be set up if the links to change the content in the one div in in another div? Basically, the navigation menu is in a seperate dive from the main content, so when someone clicks a link, i want the stuff in the other div to change basically by loading an separate new page within that div. That ajax thing does exactly what i want, but i was keen on doing it all with PHP.
BruceTheDauber
When I read the question, I thought it sounded like you wanted to do something that could be done with # links, e.g.:

you have a document, thus:

Code:

<html>
<div id=div1><a name='div1'></a>blah
</div>
<div id=div2><a name='div2'></a>more blah
</div>
<div id=div3><a name='div3'></a>even more blah
</div>
</html>


now, when someone goes to http://www.myfabulouswonderfulwebsite.ru/index.html#div3

the browser opens the page and straightaway scrolls down to div3.

But maybe that's not what you meant, since it doesn't involve any PHP or, indeed, any other scripting.
eday2010
Not what I meant at all Smile

http://www.frihost.com/forums/vt-45081.html is a posting I did on how to do my layout before I figured it out. From the image on there, each div is colour coded. the Content I want to change is the white div, and the links that i want to cause the content to change are in the purple div. Everything else is to stay the same, because it is just graphics and doesn't need to change.

So I want the functionality of that Ajax thing, but in PHP if it isn't overly complex. Though even if it is, I want to learn it.
Related topics
What's the best way to design a site?
HTML tables with irregular rows and/or columns
CSS Help...
[*OUT OF BUSINESS*] Please close
Aligning coloums with <div> and CSS
Center the site with the <DIV> tag.
Can you target a <div> like a frame?
Validation problems with <form>
Difference between <div> and <p>...?
problem with <ul> navigation
<h1> and <strong> tags
<li> tag document type?
<?php ?> Tags
[HELP QUICKLY!] Images not filling wrapper <div> eleme
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.