Is it possible to Echo a Web Page link in PHP?

If it is can anyone supply the code please.

Matthew

Recommended Answers

All 6 Replies

Indeed it is:

<?
echo "<a href=\"YOUR_LINK_HERE\" >LINK_TITLE_HERE</a>";
?>

P.s: Don't forget the backslashes for the 'href' attribute, if you leave them out the php won't work.

Thankyou for that but i still can't get it working now it doesn't even change when i click the link.

I have a image map link that i click which i want to change what is in a div further down the page but what i want in the div is a list of files with links to them i can get plain text just not links

Matthew

Member Avatar for diafol

If I'm getting you, you want to change the content of a part of the page when you click a link. If so you'll probably want Ajax, although you could use an iframe, or even a page reload with url parameters.

An image map is just a glorified bunch of image links, so it/they should work as stated by Menster. If an imagemap link doesn't work, be sure to check the html syntax - all the t's crossed and all the i's dotted!

<?php
echo '<a href="YOUR_LINK_HERE" >LINK_TITLE_HERE</a>';
?>

You could try the above (Menster's e.g. but without escaped double quotes).

I get my links to work by simply inputting:

<?php
 echo '<a href="http://mydomain.com">My domain</a>';
?>

That hasnt worked either, if you hadnt guessed as well i am new to this. I have attached the code for the whole page. could it be something wrong with the script for the div

otherwise think i might look into this iframe and ajax

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<!-- php code start -->
<? 
 
$table_env01='help';

?>
<!-- php code end -->

<html>

<head>
	<script type="text/javascript" src="swfobject.js"></script>
</head>

<style>
	div, a { 
		padding:3px;
		margin:3px 3px 3px 3px;}
	#scriptVars1 {
		text-align:Left;
		font:Arial;
		width:635px; 
		height:240px;}
</style>
 
<script>
	function changeDivContent( nameOfDiv, newContent )
	{
		var div = document.getElementById( nameOfDiv );
		if( div )
		{
			div.innerText = newContent;
		}
	}
</script>

<body>
<table width="528" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="384" align="center"><img src="Environmental Pie Chart.jpg" width="344" height="343" border="0" usemap="#Map" /></td>
    <td width="20">&nbsp;</td>
    <td width="124"><img src="Environmental Green Boxes.jpg" width="124" height="255" border="0" usemap="#Map3" /></td>
  </tr>
  <tr>
    <td width="384" height="20">&nbsp;</td>
    <td width="20">&nbsp;</td>
    <td width="124" height="20">&nbsp;</td>
  </tr>
  <tr>
    <td width="384" align="center"><img src="Environmental Grey Boxes.jpg" width="384" height="125" border="0" usemap="#Map2" /></td>
    <td width="20">&nbsp;</td>
    <td width="124">&nbsp;</td>
  </tr>
</table>

<br />

<table width="650" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><div id="scriptVars1">This content should be replaced...</div></td>
  </tr>
</table>

<p>
  <map name="Map" id="Map">
    <area shape="poly" coords="170,9,170,95,141,101,113,122,45,70,60,54,80,38,99,26,122,17,145,11" href="#" onclick=
    "changeDivContent( 'scriptVars1', '<?php
		echo '<a href="http://mydomain.com">My domain</a>';
      ?>' )">
  </map>
  
  <map name="Map2" id="Map2">
  </map>
  
  <map name="Map3" id="Map3">
    <area shape="rect" coords="0,0,124,60" href="#" />
    <area shape="rect" coords="0,65,124,125" href="#" />
    <area shape="rect" coords="0,130,124,190" href="#" />
    <area shape="rect" coords="0,195,124,255" href="#" />
  </map>
</p>
</body>
</html>
Member Avatar for diafol

echo '<a href="http://mydomain.com">My domain</a>' This bit may cause problems - I didn't realise that you were passing this as a parameter in a js call. You need to encode the tag, or better still, just pass the url string (mydomain.com):

Like this: ... onclick="changeDivContent('scriptVars1','<?php echo $url;?>','<?php echo $label;?>');" ... and change this to:

<script>
	function changeDivContent( nameOfDiv, newContent, newLabel )
	{
		var div = document.getElementById( nameOfDiv );
		if( div )
		{
			div.innerText = '<a href="http://' + newContent + '">' + newLabel + '</a>' ;
		}
	}
</script>
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.