Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Calling a JavaScript function via PHP

    I got stuck at a point while writing my own counter for my website.
    Here is the code :
    PHP 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?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Your JavaScript syntax is wrong:
    PHP Code:
    <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>");
    ?>
    Quote Originally Posted by hkBattousai View Post
    Does JavaScript run before PHP generates HTML code?
    No.

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Nope your javascript is rendered along with html, you can have php create javascript!

    The problem lies in this line:
    Code:
    var szImages[NUM_OF_DIGITS];
    That's not how to define an array in Javascript.
    It should be:
    Code:
    var szImages = Array(NUM_OF_DIGITS);

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Lightbulb

    Quote Originally Posted by mcjwb View Post
    The problem lies in this line:
    Code:
    var szImages[NUM_OF_DIGITS];
    That's not how to define an array in Javascript.
    It should be:
    Code:
    var szImages = Array(NUM_OF_DIGITS);
    That's what happens when a C++ programmer starts learning JavaScript.

    Thank you all for your replies.

  • #5
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Calling a JavaScript function via PHP

    Quote Originally Posted by hkBattousai View Post
    PHP Code:
    <?
        
    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!

    PHP Code:
    <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>

  • #6
    New Coder
    Join Date
    Jun 2007
    Posts
    13
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You need to escape the quotes in the first line of case 4. Other than that they should all(except 6?) work.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •