www.webdeveloper.com
Results 1 to 8 of 8

Thread: Hold a variable from PHP in Javascript

  1. #1
    Join Date
    Jan 2007
    Location
    Columbia, MD
    Posts
    45

    Hold a variable from PHP in Javascript

    I found an answer to this and tried it but it did not work so I am posting this for more help. I posted a reply to the thread I found earlier but no one replied.
    I am trying to write a game where you roll the dice and get half a quote and you have to guess if it matches the half that you were originally given. I am using AJAX and everything works fine right up until the end when I send a variable from php to javascript using echo '<script>currentQuote = $currentQuote;</script>'; It shows up on the page but when I check the value of currentQuote in JS it is undefined. Here is a link to a simplified version of the game just showing my problem.http://workwithu.com/Games/Game15snip.php The PHP code which you cannot see is:
    <?php
    $response = mt_rand(1,6);
    echo $response;
    $currentQuote=$response;
    echo '<script>currentQuote = $currentQuote;</script>';//this not working
    echo "<br>";
    echo "currentQuote is:";
    echo $currentQuote; //this shows on the screen fine
    ?>
    Any help would be GREATLY appreciated. I have run out of ideas to try.
    Thanks

  2. #2
    Join Date
    May 2007
    Posts
    184
    echo '<script>currentQuote = '.$currentQuote.';</script>';
    "Profanity is the one language all programmers know best."

  3. #3
    Join Date
    Jan 2007
    Location
    Columbia, MD
    Posts
    45

    Nope, I still get undefined

    Quote Originally Posted by ss1289 View Post
    echo '<script>currentQuote = '.$currentQuote.';</script>';
    Thanks for replying. I tried the code above and still get undefined in the javascript variable. If you click on the link I have an alert to show what values are recorded in javascript. I like your quote about the language we all know though

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,138
    I have a script that sets a server time in a Javascript JSON field, the method I use is this.

    PHP Code:
        $theValue "This is a string";
        
    $numberVar 12345
    then...

    Code:
    <script>
    json = {
    
        theVar:"<?php echo $theValue; ?>",
        numberVar:<?php echo $numberVar; ?>,
        anotherVar:false
    
    }
    </script>
    You have to wrap string output if your value is a string, so your JSON object should have...

    json.theVar = an output that is a string
    json.numberVar = a number

    assuming that the respective elements are string and then number.

    Should render as...


    Code:
    <script>
    json = {
    
        theVar:"This is a string",
        numberVar:12345,
        anotherVar:false
    
    }
    </script>
    Last edited by \\.\; 10-24-2013 at 06:59 PM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Jan 2007
    Location
    Columbia, MD
    Posts
    45

    trying json

    Quote Originally Posted by \\.\ View Post
    I have a script that sets a server time in a Javascript JSON field, the method I use is this.

    PHP Code:
        $theValue "This is a string";
        
    $numberVar 12345
    then...

    Code:
    <script>
    json = {
    
        theVar:"<?php echo $theValue; ?>",
        numberVar:<?php echo $numberVar; ?>,
        anotherVar:false
    
    }
    </script>
    You have to wrap string output if your value is a string, so your JSON object should have...

    json.theVar = an output that is a string
    json.numberVar = a number

    assuming that the respective elements are string and then number.

    Should render as...


    Code:
    <script>
    json = {
    
        theVar:"This is a string",
        numberVar:12345,
        anotherVar:false
    
    }
    </script>
    Using your example I put
    $numberVar = 12345; in the php file that I am calling with AJAX
    then the script on my main page<script>
    json = {
    numberVar:<?php echo $numberVar; ?>
    }
    </script>
    Then once I got the AJAX response I tried
    onclick="tryAnswer(json.numberVar)"
    and
    function tryAnswer(turn)
    {
    alert("playerNumber is: "+playerNumber+" currentQuote is: "+turn);function tryAnswer(turn)
    }
    which my debugger responded:ReferenceError: Can't find variable:json
    I have not used json before so I may have this screwed up.
    Please advise.

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,138
    If your using AJAX then you can't use the method your trying to employ, your PHP script ouput would have ot be echoed out so the returning query is either a straight number or string.

    So ...
    Code:
    AjaxObj = new XMLHttpRequest();
    AjaxObj.open("GET","yourphpscript.php",true);
    AjaxObj.onreadystatchange = function(){
        if(AjaxObj.readyState == 4 && AjaxObj.status == 200){
            theResponse = AjaxObj.responseText;
        }
    }
    AjaxObj.send();
    as a rough example, then your script that is "yourphpscript.php" would be like
    PHP Code:
     <?php
       
    echo "your string"
    ?>
    so that all that is returned to AJAX is your string to theResponse variable

    What you do after that is up to you.

    If you want to write javascript variables from PHP when the page is being rendered then the previous example is what you need.

    What you seem to be doing is mixing Javascript and PHP up and you can't render PHP clientside as its a serverside scripting language.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,138
    To clarify...

    PHP Code:
    <?php
        $theValue 
    "This is a string";
        
    $numberVar 12345

    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script>
    json = {

        theVar:"<?php echo $theValue?>",
        numberVar:<?php echo $numberVar?>,
        anotherVar:false

    }
    alert( json.theVar ); // outputs This is a String
    </script>
    </head>

    <body>
    </body>
    </html>
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,138
    Then if you look at the page source code, you will find it renders as...
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script>
    json = {
    
        theVar:"This is a string",
        numberVar:12345,
        anotherVar:false
    
    }
    alert( json.theVar ); // outputs This is a String
    </script>
    </head>
    
    <body>
    </body>
    </html>
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles