#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Fairlawn, Ohio
    Posts
    5
    Rep Power
    0

    Passing string from PHP to Javascript


    Hello all,
    After reading through 95 pages of messages I've found most people are trying to do the exact opposite from what I want to do, pass a variable (string) from PHP to a Javascript. I hope someone might be able to direct me in the proper approach to do this. Here is where I'm at so far: I am using a PHP script to determine the file size of a file on the server and, depending on the size, return a different java formatted string to use in a javascript running on my webpage to display to users. The problem is I can't seem to understand the best way, or any way for that matter, to do this.

    My PHP code ("alert.php" on the server):

    PHP Code:
    <?php
        $my_file 
    "/myserver/info.txt";
        
    $file_size filesize($my_file);
        if (
    $file_size >= 300)
        {
            
    $string="There is new information";
        }else{
              
    $string="<BR>"
        }
        print 
    $string;
        echo 
    "<script language=\"javascript\">\n"
        echo 
    "text=\"".$string."\";\n"
        echo 
    "alert(text);\n";
        echo 
    "</script>\n"
    ?>
    and the Java (embedded in my HTML):

    <SCRIPT LANGUAGE="JavaScript" type="text/js" src="http://myserver/alert.php">
    <!-- Begin
    document.write("<MARQUEE DIRECTION=LEFT>" + text + "</MARQUEE>" + "<br>" + "<br>")
    // End -->
    </SCRIPT>

    I added the "alert" so I could verify that the php code was actually being executed and have tried adding several different ways to call the script into my html file but have only had success using:

    <object data="http://myserver/alert.php"></object>
    <iframe src="http://myserver/alert.php"></iframe>

    I also read that this:

    <img src="http://myserver/alert.php" height=1 width=1>

    should execute the PHP file also but will not return anything so I don't know for sure if it also works but I have tried it.

    The two things I tried that didn't work were:

    <script type="text/js" src="http://myserver/alert.php"></script>
    <script language="javascript" src=\"http://myserver/alert.php\"> </script>

    I realize that PHP is a server side app and Javascript is client-side but I've read that there might be several options to use to pass the string to the client window for javascript. Cookies (yuck), forms, attaching the string to the url??? and hidden windows but I'm not sure which would be best (read: easiest) for a beginner.

    One other thing to note: the ISP that hosts my site does not have embedded support for php in html turned on so I cannot use embedded PHP, that is why I created a seperate PHP file on the server and yes I can call the file directly from a browser and it works perfectly.

    Any suggestions? And please, I'm a newbie to PHP and Java so lets not get too "deep".

    Thanks,
    Ken
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,815
    Rep Power
    4554
    <SCRIPT LANGUAGE="JavaScript" type="text/js" src="http://myserver/alert.php">

    will work, but you have to remember that "alert.php" must return valid JavaScript code, just like including a regular .js file. That means that the result doesn't need the <script> tags in it. If the "alert.php" file just assigned the string to text and called the alert(), it should work.

    How about if the file is less than 300 bytes then just return an empty javascript file so nothing happens instead of returning a "<br>" (?) string.

    ---John Holmes...
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Fairlawn, Ohio
    Posts
    5
    Rep Power
    0
    Thanks John, but I'm still not able to show the string using js in a html page. Here is the code I'm using now, simplified as much as possible.

    My PHP code (alert.php)
    PHP Code:
    <?php
        $my_file 
    "/myserver/summary.txt";
        
    $file_size filesize($my_file);
        if (
    $file_size >= 300)
        {
        
    $string="There are new alerts";
        }
        else
        {
        
    $string=""
        }
        echo 
    "text=\"".$string."\";\n"
    ?>
    My HTML code (new.htm)
    <html>
    <head>
    </head>
    <body bgcolor="#C0C0C0">
    <h1>Alert Page</h1>
    <!-- BEGIN - New information banner code -->
    <SCRIPT LANGUAGE="JavaScript" type="text/js" src="http://myserver/alert.php">
    alert(text)
    document.write("<MARQUEE DIRECTION=LEFT>" + text + "</MARQUEE>" + "<br>" + "<br>")
    </SCRIPT>
    <!-- END - New information banner code -->
    </body>
    </html>

    Everything looks right to me and when I open the "alert.php" file with IE I receive the response I expect: text="There are new alerts"; but when I open the "new.htm" file I only get the "Alert Page" header and nothing else? I don't understand what might be wrong, any ideas? (thinking outloud) could it be that my web host has something configured (or not) that could stop this from working?

    Ken
  6. #4
  7. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,815
    Rep Power
    4554
    What if you make an "alert.js" file and simply put in it

    text="new alerts";

    and load that into your HTML file. Do you get the expected text?

    I'm kind of grasping at straws here, too.

    Maybe you should just use an iframe or hidden frame pass the value from one frame to the other?

    ---John Holmes...
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Fairlawn, Ohio
    Posts
    5
    Rep Power
    0
    No luck John, that doesn't work either.

    I guess I'll need to look into hidden frames and pass the data that way. Guess I have more research to do, back to reading posts again!

    Thanks again for the suggestions and help!!

    Ken

IMN logo majestic logo threadwatch logo seochat tools logo