...

View Full Version : Calling a JavaScript function via PHP



hkBattousai
08-10-2007, 10:55 PM
I got stuck at a point while writing my own counter for my website.
Here is the code :


<script language="javascript">
function ShowCounter(nCount)
{
alert('This alert() function never runs.');
var NUM_OF_DIGITS = 6;
var szImages[NUM_OF_DIGITS];
var szCount = nCount.toString();
var nLen = szCount.length;
for (var i=0; i<NUM_OF_DIGITS-nLen; i++)
{
szImages[i] = 'Digits/0.jpg';
}
for (var i=NUM_OF_DIGITS-nLen; i<NUM_OF_DIGITS; i++)
{
szImages[i] = 'Digits/' + szCount.charAt(i-(NUM_OF_DIGITS-nLen)) + '.jpg';
}
for (var i=0; i<NUM_OF_DIGITS; i++)
{
document.write('<img src="' + szImages[i] + '">');
}
}
</script>

<?
echo("Counter : " . "<script language=\"javascript\">" . "ShowCounter(" . 123 . ");" . "</script>");
?>


Am I doing something wrong here? Does JavaScript run before PHP generates HTML code? Is that the reason?

Inigoesdr
08-10-2007, 11:19 PM
Your JavaScript syntax is wrong:


<script language="javascript">
function ShowCounter(nCount)
{
//alert('This alert() function never runs.');
var NUM_OF_DIGITS = 6;
var szImages = new Array();
var szCount = nCount.toString();
var nLen = szCount.length;
for (var i=0; i<NUM_OF_DIGITS-nLen; i++)
{
szImages[i] = 'Digits/0.jpg';
}
for (var i=NUM_OF_DIGITS-nLen; i<NUM_OF_DIGITS; i++)
{
szImages[i] = 'Digits/' + szCount.charAt(i-(NUM_OF_DIGITS-nLen)) + '.jpg';
}
for (var i=0; i<NUM_OF_DIGITS; i++)
{
document.write('<img src="' + szImages[i] + '">');
}
}
</script>

<?
echo("Counter : " . "<script language=\"javascript\">" . "ShowCounter(" . 123 . ");" . "</script>");
?>


Does JavaScript run before PHP generates HTML code?
No.

mcjwb
08-10-2007, 11:21 PM
Nope your javascript is rendered along with html, you can have php create javascript!

The problem lies in this line:

var szImages[NUM_OF_DIGITS];
That's not how to define an array in Javascript.
It should be:

var szImages = Array(NUM_OF_DIGITS);

hkBattousai
08-11-2007, 07:27 AM
The problem lies in this line:

var szImages[NUM_OF_DIGITS];That's not how to define an array in Javascript.
It should be:

var szImages = Array(NUM_OF_DIGITS);

That's what happens when a C++ programmer starts learning JavaScript.

Thank you all for your replies.

MortenH
08-21-2007, 09:31 PM
<?
echo("Counter : " . "<script language=\"javascript\">" . "ShowCounter(" . 123 . ");" . "</script>");
?>



How have you made this working??

I tried a lot and canít succeed in calling a JavaScript function with PHP!!

I tried all those but can't get it working!
Please help if you or any one else can help!



<script language="javascript">
function test(){
document.all.textfield.value="active";
}
</script>

<?php
echo '<a href="javascript:test();"></a>'//case. 1

echo ("<script language=\"javascript\">test();</script>")//case. 2

echo "<script language='javascript'>test();</script>"//case. 3

echo "<SCRIPT LANGUAGE="javascript">"
echo "test();"
echo "</SCRIPT>"//case. 4

echo "<script language=javascript>alert(active)
</script>"//case. 5

echo "<scriptlanguage=javascript>alert(active)</script>"//case. 6

?>
<form name="form1" method="post" action="">
<input name="textfield" type="text">
</form>

hkBattousai
08-21-2007, 10:15 PM
I don't know JavaScript very well, but cases 2, 3 and 4 must work.

Run your PHP script, then on your browser, right click somewhere and click "show source code" in the context menu. Have a look at the source code, check for errors in the generated JavaScript code. Even if it looks ok, once try running that generated JavaScript code itself instead of generating it by PHP.

Inigoesdr
08-21-2007, 10:35 PM
You need to escape the quotes in the first line of case 4. Other than that they should all(except 6?) work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum