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 11 of 11
  1. #1
    New Coder
    Join Date
    May 2003
    Location
    England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript to get a php variable and write it ?

    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 )

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <?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.

  • #3
    New Coder
    Join Date
    May 2003
    Location
    England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Brisbane, Australia
    Posts
    181
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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);
    -mR_eGo
    _______________________
    Programming since
    3 years old.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Doh! I really stink at PHP...

    Thank you for that information.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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?>;

  • #7
    New Coder
    Join Date
    May 2003
    Location
    England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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>

  • #8
    New Coder
    Join Date
    May 2003
    Location
    England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry Glenn, I dunno why but I didn't see your post before( must be these shades ) 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)

  • #9
    New Coder
    Join Date
    May 2003
    Location
    England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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.

  • #10
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

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

  • #11
    New Coder
    Join Date
    May 2003
    Location
    England
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well that doesn't surprise me that I've spent all this time trying to do something impossible
    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.
    Last edited by Steve_Moore; 10-22-2003 at 05:31 PM.


  •  

    Posting Permissions

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