Forum Moderators: coopster

Message Too Old, No Replies

How can php pass variable to jave script function

         

hughc

8:30 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



I want to pass the php variable to a windows open function to display.
How can I do that?

Please reply me at your eariest convenience.Thanks first~

httpwebwitch

8:48 pm on Mar 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can write a PHP variable into a javascript, it's quite simple.


<?php
$foo="bar";
?>
<script>
window.open("myfile.htm?var=<?php print($foo)?>")
</script>

PHP runs on the server, javascript runs at the client. Thus the output from above will look like this:


<script>
window.open("myfile.htm?var=bar")
</script>

So... You can use PHP to output custom javascripts - it's a very useful method. Try this as an example:


<script>
<?php
for ($i=0;$i<5;$i++){
print("alert('hello world!');");
}
?>
</script>

The result is:


<script>
alert('hello world');
alert('hello world');
alert('hello world');
alert('hello world');
alert('hello world');
</script>

... and you'll see 5 alerts when the script is run.

Good luck!

daisho

8:49 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



You can't directly since PHP is serverside and Javascript is client side.

But you can probably get what you want by using PHP to generate your JavaScript.

ie:

<?
$msg='This is a PHP variable being sorta passed to JavaScript';

print "<script>alert('$msg');</script>";
?>

Daisho.

hughc

9:43 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Thank you very much for you 2~~
Really Thanks~~

hughc

10:05 pm on Mar 24, 2004 (gmt 0)

10+ Year Member



Sorry.....

Actually, I want to display nine items in a page. And when visitors click on the 'More>>', a new windows will be opened, so, can I use one function for all items?
That mean the function will be used several times and display different information.
If so, what can I do in passing variable to it?

httpwebwitch

8:26 pm on Mar 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since functions can be user-defined (a nice feature of just about every programming language), you can write a function to do pretty much anything you please. This isn't a PHP problem, it's a problem-solving problem. You need to wear your "problem solving hat" and think about what you want to do.

How you approach this problem depends where you're getting these "items" from. If you're querying a database, you can use SQL to grab as many or as few items as you want. If the items are in a text file, an array, or generated by some kind of algorithm, then you will have other challenges.

A good strategy to start you off is to make a function that displays $x items, stating at $offset.

function showstuff($myarray,$x=10,$offset=0){
for ($i=$offset;$i<$offset+$x;$i++){
echo($myarray[$i]);
}
}

then you can call the function a few ways:

showstuff($inarray);
// shows the first 10 items in $inarray
// note: these are items 0 through 9, you might
// like to display them as 1 - 10, just by adding 1
// to the value of $i as it loops

showstuff($inarray,count($inarray));
// shows all the contents of $inarray

showstuff($inarray,10,30);
// shows items 30-39 of $inarray

Good luck to you!