Forum Moderators: coopster

Message Too Old, No Replies

url in an external php file

how to displya link with document.write in php

         

selomelo

5:23 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



Hello,

I am not a php programmer, and in fact not a programmer at all in any script language. Therefore, please excuse me for asking such a simple question.

Situation: I have an external php file with a function in it that returns a value with document.write like this:
<?php
Header("content-type: application/x-javascript");

function myfunction()
{
return $something;
}

$some_variable=$something;

echo "document.write(\"Some_Text: " . $some_variable . "\")";
?>

When called remotely by javaScript, the php function behaves perfectly returning the desired variable:

<script> type="text/javascript" src="http://www.mydomain.com/remote.php"></script>

The Problem:

I would like to display the "Some_text:" part with an active link as in
<a href="http://www.yourdomain.com/">Some_text</a>

So that when executed, the JavaScript displays the link with an anchor text.

How can I write this code into the echo document.write function?
Thank you for your valuable insights.

StupidScript

5:31 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can either assign the whole value to a variable or build your echo to include the pieces.

$some_variable="<a href=\"http://www.yourdomain.com/\">Some_text</a>";

echo "document.write(\"Some_Text: " . $some_variable . "\")";

echo "document.write(\"Some_Text: <a href=\"" .$url_variable . "\">" . $text_variable . "</a>\")";

selomelo

2:11 am on Feb 11, 2006 (gmt 0)

10+ Year Member



Thank you StupidScript,

Finally, I managed to write the code with correct syntax.

I am including it here for those who might need to call an external php file through javascript, and display a variable, and a link with anchor text:

Method 1:
$url ="myurl/";
$variable=some_function();

echo "document.write(\"<a href=http://" . $url . ">anchor_text</a>: " . $variable . "\")";

Method 2: If the link to be displayed is pointing to the host of the php file, then it simply becomes:

echo "document.write(\"<a href=http://" . $_SERVER['HTTP_HOST'] . ">anchor_text</a>: " . $variable . "\")";

Both are equally functional when called externally by a Javascript.