Forum Moderators: open

Message Too Old, No Replies

Javascript show output of a php file in specific placeholder

         

babil

4:09 pm on Aug 10, 2006 (gmt 0)

10+ Year Member



Hallo all,
how can I show the output of a php script inside a specific <td> when clicked on the link?
example:


<html>
<head><title></title>
</head>
<body>
<a onClick="myscript()" href="test.php?a=1&b=345">click here</a>
<table>
<tr>
<td>&nbsp;</td>
<td id="test"></td>
</tr>
</table>
</body>
</html>

When the link is clicked, the output of test.php should be displayed
inside the <td> with the id="test".

Is it possible?
Thank in advance
babil

babil

6:03 pm on Aug 10, 2006 (gmt 0)

10+ Year Member



ok, here is the script code and it works..partialy


function writit(text) {
if (document.getElementById) {
x = document.getElementById('test');
x.innerHTML = '';
x.innerHTML = '<?php include ("' + text + '");?>';
}
else if (document.all) {
x = document.all['text'];
x.innerHTML = text;
} else if (document.layers) {
x = document.layers['test'];
var text2 = "<?php include ('tst.php');?>";
x.document.open();
x.document.write(text2);
x.document.close();
}
}

okay the prob... if I use this string '<?php include (" ' + text + ' ");?>'; I get an error
Error: unterminated string literal
Source File: [localhost...]
Line: 11, Column: 14
Source Code:
x.innerHTML = '<br />

if I use this string '<?php include ("tst.php");?>'; everything works fine.
It seems like there is something wrong with the way the string is sewed together.
Anyone an idea? this thing drives me crazy.

thank you
cheers

httpwebwitch

2:51 am on Aug 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



2 ways to do it.

1) Write your PHP output into a string variable in advance, and populate your innerHTML with an onclick event. A good method, provided your PHP output isn't huge, and doesn't change based on user interaction (an expanding/collapsing menu is a good example)

2) have your onclick trigger an HttpXmlRequest routine, which executes the PHP and inserts the result into the DOM. This method is commonly known as AJAX. There are now so many tutorials online for this it would be silly to repeat it here...

MisYu

8:40 am on Aug 11, 2006 (gmt 0)

10+ Year Member



Another possibility is RPC: dynamically create a new <script>, whose source is the php file that outputs something, which is then retrieved by JavaScript. It's possible without using XMLHttp.

httpwebwitch

9:05 pm on Aug 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ya.
So, essentially two ways. PHP output generated synchronously with pageload, or asynchronously triggered by user interaction.

Of the former type there are a few ways to get PHP output into a javascript. MisYu's idea is elegant since it will load separately from the page referencing it.


<script language="javascript" src="mydynamicallygeneratedscript.php" />