...

View Full Version : Javascript to get a php variable and write it ?



Steve_Moore
10-21-2003, 11:35 PM
javascript is fairly new to me and php is even newer.

I can write html in my message board as I allow it and it will also accept javascript.

I have written a php file which when executed independantly returns exactly what I want.

I have figured that you can write html in a .php file but not php inside a .html file. (I naively tried <?php include(".... .php") ?>:) )

So can I use javascript to get the value (string) of the variable by calling/getting/whatever the php file ?? Then write it using JS ??

I suppose I'm trying to convert server side to client side in some respects, but having tried to define what it is by name that I'm trying to do, a search of this site yielded nothing nor 2 days of going through all the threads. (picked up some good tips though;) )

nolachrymose
10-22-2003, 12:09 AM
Do you mean have JavaScript read a variable from another PHP page? This is possible in a sense, if you include the PHP page. Try this:


<?php include("variables.php"); ?>
<script type="text/javascript">
var blah = <?php echo $myvariable ?>;
</script>
I'm not too familiar with PHP, so I'm not sure if the "echo" command is needed or not. Try it both ways, and see which one does the trick.

Steve_Moore
10-22-2003, 12:25 AM
Thanks, but it won't work.

I cannot put php code inside an html document and execute it successfully.

I need to point js towards the php file without using php code inside the html.

mr_ego
10-22-2003, 12:36 AM
Your data has to be in quotes..
also:


var somevariable = "<?php echo $variable?>";

wont work if $variable contains a " or a \

add this above that line:

$variable = str_replace("\\", "\\\\", $variable);
$variable = str_replace("\"", "\\\"", $variable);

nolachrymose
10-22-2003, 12:47 AM
Doh! I really stink at PHP... :o

Thank you for that information.

glenngv
10-22-2003, 10:03 AM
It's not a PHP quirk. All server-side languages need that. Server-side variable embedded in javascript need to follow javascript's syntax.
mr_ego is right, you should insert your server-side variable inside the quotes in a javascript code, that is if the js variable is a string. If it's an integer or float, you don't need the quotes.

var myNumber = <?php echo $number_variable?>;

Steve_Moore
10-22-2003, 10:14 AM
That's okay nolachrymose, but php is worth getting to grips with. Requires a little bit of patience. Two days I've spent trying to do this and now my patience is wearing thin I have to admit.:)

mr_ego, you have cleared up a small detail which I would have left toward the end.
When I run the php file on its own I get the slash everytime theres an ' in the text. So I can use that code above to clear that up, thanks.

Here's the php file I currently have:


<?php
$mystring = 'http://www.elvisnews.com/output/dbd.asp';
$get = implode('', file($mystring)) or die('Cannot read file!');
$length = strlen ($get);
$newlength = $length - 265;
$history_output = substr($get, 16, $newlength);
$history = "<font size=2>$history_output";
echo "$history";
?>


I'm really pleased with myself on this one (not yet even at php novice stage).
I've managed to get a .asp from another server, managed to get rid of the standard html code it finishes with (last 265 characters
and also the first 16) and output the balance with corrected font size.
This asp file generates random quotes so everytime the php file is refreshed in the browser it changes the quote.

Great!, but how do I place that output inside an existing html document without using the horrible<iframe> tags ? Preferably somehow with javascript.

Here's what I tried within the html originally:
<table>
<tr>
<td>
<?php
include ("http://www.myserver.com/thephpfile.php");
?>
</td>
</tr>
</table>

Steve_Moore
10-22-2003, 04:12 PM
Sorry Glenn, I dunno why but I didn't see your post before( must be these shades :cool: ) but thanks for contributing.

I'm gonna try this within the js within the html.

My fall back is to recreate the entire php file but using js to do it. (I think it can be done)

Steve_Moore
10-22-2003, 04:34 PM
Can't get this yet.

It is a string, not a number.

Here's what I've now tried based upon your input above:

<table>
<tr>
<td>

<script language="javascript">

var somevariable = "
<?php include("http://www.domainname.com/phpfile.php"); ?>";

document.write (somevariable);

</script>

<!--This code looks like it's gonna suck something awful but I'll give anything a try once -->

</td>
<td width="146" background="image.jpg">
</td>
</tr>
</table>

I'm not sure how it would get to write what I want the output from the php file to write but then again when I tried it, it doesn't do it.

You have the code from the php file in the post above.
That outputs the string $history.

From within an html document what exactly do I need to write in js to get that output ?

Apologies if I haven't been clear before, I just don't know how to explain the result I want in the right words.

Choopernickel
10-22-2003, 05:14 PM
I'm not sure why you're going to all this trouble to try and do something that's completely impossible: HTML can't perform php include()s, and it never will be able to.

My suggestion: Change your php to consist only of javascript, and serve itself with the mime-type "text/javascript" - then, in the head of your html document, use this line:


<script type="text/javascript" src="path/to/phpFile.php"></script>

This will load your PHP variables into your javascript scope. I do this with coldfusion all the time.

In another set of script tags (preferably with an onload function), grab your PHP-created variables and write them to the document where they belong.

HTH

Steve_Moore
10-22-2003, 05:26 PM
Well that doesn't surprise me that I've spent all this time trying to do something impossible :D
Though I did say I was aware that php files can include html but not the other way around. (I learnt that the hard way as well!!)

What you suggest sounds great.

I haven't gotta clue what you meant, but you sounded very confident and I'm sure it will work.

What's a mime thingy ?

If I change my php file to entirely js how will it "load your PHP variables into your javascript scope"

scope, mime, other jargonny stuff, I'm not clearly out of my depth here, I've completely drowned.

I need to find the newbie room quickly so I can chat with loads of others who also don't have a clue what we're all talking about.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum