I understand how to use the basic include function in a .php page so that repetitive content (like a menu) can be coded in a separate file and then dynamically included in multiple pages. But I don't get how to use it so that a URL in a page can be clicked to load content into the original page with the hyperlink.

For example a page with a special offer that requires a disclaimer would use the following to link to a separate page:

<a href="disclaimer.html">click here to read the disclaimer</a>

I'm hoping to load that disclaimer right into the original page if someone wants to read it when they click on the link.

How can that be done with the PHP include function?

And can PHP be used in a .shtml page?

Thanks.

Actually, kkeith29 example doesn't load new content in the original page; it just reloads the page with a parameter passed through the $_GET variable, and shows some text as a response of that parameter.
Since php is a server side script, which means it is processed on the server and only the result of the process is sent to the client (browser), in order to process any piece of php code (like an 'include' request), that piece of code must be passed through the server; you cannot execute php code inside the user browser; for that, you need to use a client side script (like javascript).
A simple approach to what you want is to insert in your page, from the beginning, in a hiddend block (div, p etc.), the piece of text that you want to show on click, and make a javascript that simply shows that block of text when you click the link.

If the block of text (or whatever it is) must be the result of a php script, than the only way you can insert that on the page on client side (without reloading the page) is through AJAX, which is, simply put, javascript communicating with the server (the php page in your case) without the page reload.

commented: excelent +1

sorry, i'm not stupid. I know how php works, and yes it does load new content into the original page. the text wasn't there before was it. By saying he wanted it in the original page, i think he means that he wanted the text to show up without have to go to a different page to view the content. If he means that it should show up without the page reloading then he should be more specific. Anyways, think if javascript is disabled. Neither javascript or AJAX would work, then your screwed. if you use my way, it always works.

Hey, kkeith29, don't take it so personally; I didn't meant you were stupid; I wasn't even replying to you, but to Syakoban; your solution is very good if you want to reload the page; I just understood something else from Syakoban post (to be more clear, I understood he wanted to load some content in the page without reloading the page) and I pointed out to him the differences so he can learn an choose what best suits him (or her :) ).
I am sorry if my post offended you. I asure you that wasn't my intention. Ok?

he wanted the text to show up without have to go to a different page to view the content.

That's right! I've used another script that works this way w/cookies that based on number of visits to the home page, pulls in different content while still staying at the "home page". I am looking for that effect - reloading is not an issue.

I just started to try your sample code and am not getting it to work on the server yet. I took both pages you created as is (show.php and stuff.html) and simply wanted to get it working before making any changes. I uploaded in ASCII. I gave the original page a .php extension, but does the script need to be enclosed in <?PHP ?> tags? Can it be anywhere in the page? When I click the link I get no change except to the URL.

Mine is at http://ehydrant.com/show.php

Thanks.

OK I tried the <?PHP ?> tags around the code and it works on my server now. I'm guessing some servers are stricter than others???

Also how do I get the

echo '< ' . 'a href="show.php?show=disclaimer">click here to view disclaimer<' . '/a>';

to appear as just: click here to view disclaimer

Thanks.

sorry about that, i had to leave out the <?php and ?> tags so my server wouldn't execute the code. sorry if i confused you.

use:

<?php
if (isset($_GET['show'])) {
$show = $_GET['show'];
if ($show == "disclaimer") {
include 'stuff.html';
}
}
else {
echo '<a href="show.php?show=disclaimer">click here to view disclaimer</a>';
}
?>

and to johny_d, sorry about that (long day,tired). i took your post personally and wasn't thinking at the time. my bad

Working at it I commented out the echo lines and got rid of the href tag stuff. I went a little farther and added another copy of the script to have a second link and it works. But, can the two link scripts be combined somehow to just have one script covering all of it? I tried with an "else" command but couldn't get it to work.

Also, is there an elegant way (PHP?) to have a back link in place of the original link? Here's what I mean: I put an HTML href on the "disclaimer" content to go back to the show.php page, disguised as "close disclaimer". Instead, it would be real cool to have the original click here to view disclaimer change to click here to close disclaimer instead of the separate link. Am I getting too crazy or is it realistic?

Thanks again!

Almost...

Instead of the links moving around I was hoping that the original link would change to close disclaimer in the same location. That way the disclaimer can load below the link and not affect the layout of it like:

BEFORE

header
view warning
view disclaimer
footer

AFTER

header
view warning
close disclaimer
blah blah blah blah blah blah blah blah blah blah blah blah
footer

OR

header
close warning
blah blah blah blah blah blah blah blah blah blah blah blah
view disclaimer
footer

It kinda evolved after seeing your example in action... I tried to play with your code but I don't have the skills.:$

Thanks again, I appreciate the help!

Here's your code:

This is 'show.php'

<p>header</p>
<hr />
<?php
if (isset($_GET['show']) && $_GET['show'] == 'warning') {
 echo '<p><a href="show.php">Close warning</a></p>';
 include ('warning.html');
 echo '<p><a href="show.php?show=disclaimer">view disclaimer</a></p>';
 }
elseif (isset($_GET['show']) && $_GET['show'] == 'disclaimer') {
 echo '<p><a href="show.php?show=warning">view warning</a></p>';
 echo '<p><a href="show.php">Close disclaimer</a></p>';
 include ('disclaimer.html');
 }
else {
 echo '<p><a href="show.php?show=warning">view warning</a></p>';
 echo '<p><a href="show.php?show=disclaimer">view disclaimer</a></p>';
 }
?>
<hr />
<p>footer</p>

This is 'warning.html'

<h1>WARNING TEXT</h1>
<p>askdh fasf asdkfhas djfhakdhf ashfkjash fdhf kajsdhfkashf asjdfhds</p>

and this is 'disclaimer.html'

<h1>DISCLAIMER TEXT</h1>
<p>jdshf gdhsfg dsfhg dsfgasdowe[ wfnsdfg lsdjfgjdslfjgdsjf gljdslfjgldskfjg sjfklsjdfklgj skldjsdjfg dsjfklgjdsklfgj dskljfgklsdgfkl dsjlfgkjdsf</p>

And to kkeith29, thanks for the reply; we all have our bad days; we're cool now ;)

BEAUTIFUL Guys!!! I appreciate your help very much! :)

I know every language has its pros and cons but I really want to learn PHP. To me it seems like it has the best of server and inline coding rolled into one. This one little script turns my project into something really cool...

I do have one area that uses SSI to get server time and affect an image. I know it's old fashioned but it works really well. I tried using this script with a .shtml extension so that the SSI can work too, but the script bombs out. Is there a way to have the two coexist on the same page in this section?

Thanks.

you can do this, but you have to specially configure the server and i doubt that your hosting company will let you do this.

Thanks again guys. The custom extensions are really cool - I had no idea that could be done, I thought the server software has its defaults and that's it. I'm on a shared server so I don't know what we are or are not allowed to do in this area. Is it a .htaccess file that gets configured or something else?

The SSI I would like to use is a call to a perl CGI that uses server time and outputs back to the page. It uses the statement

<!--#include virtual="cgi-bin/open_new.pl" -->

I tried changing that to

<? include("cgi-bin/open_new.pl"); ?>

but all I got was the content of the script instead of the processed result.

you have to edit the server config file (httpd.conf). and for the second question, why do you have to use perl, couldn't you just use php to get server time.

You're right, I don't have permission to modify the httpd.conf file but can request mods through the host. I think that's not a good idea because they can change their policy in the future or I may need to change hosts.

I don't have to use Perl - I'm sure PHP (my new miracle language...) can do it well too. It's just something someone wrote for me a while ago that uses a Perl CGI to check server time and load a graphic based on a schedule that changes each day of the week. It's running at http://ehydrant.com/open_new.shtml. For an hour a day the graphic changes.

Member Avatar for fatihpiristine

it is better if you try with div and java.

I have a similar problem...

I'm a newbie and am currently teaching myself php and html...currently using dreamweaver and have been able to be pretty lazy with regards to learing code but now am using php...and got to type it in myslef....its good for me.

But i have a menu system with rollovers and want to assign a onclick variable to them, then have the main content change and navigate to a php page.....

Have tried href=<?PHP $page= "home.php";?> and this doesn't work should i be using onclick? How? Do i need to reset $pager variable each time....how?

And in my main content i have written <?PHP include("$page");?> and when it does it puts a lot of space in the content page.

This is a basic page i'm writing for this page you can view the source there isn't any php in it yet.

www.thesilvergem.vndv.com

Thanks everyone.

aformoftruth

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.