#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    2

    How to store javascript value into php variable


    Hi,

    How do i store javascript value of q1 into php variable $ff? Its not working.
    PHP Code:

    <html>

    <SCRIPT language="JavaScript">

    var q1=Math.floor(Math.random()*11)
    <?php
    $ff 
    =  q1;
    ?>

     }

    </SCRIPT>

    </HEAD>

    <BODY>
    <?php
    echo $ff ;
    ?>
    </body>
    </html>
  2. #2
  3. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    This doesn't work as i see you expect it to. Javascript runs on the client side php on the serverside.

    In this case you need to output the variable withjavascript to the browser. For instance in the form of a link with a GET variable. Ones that link is clicked the request is made to the server and php can get involved. Have a read here and here

    So in a nutshell. you create alink with javascript that includes a GET variable. ones clicked php gets involved.
    Code:
    http://example.org/index.php?q1=blablabla
    PHP Code:
    if(isset($_GET['q1']{
    $ff $_GET['q1']

    Comments on this post

    • molossus agrees : simple and elegant solution
  4. #3
  5. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Any JavaScript will not run until the page is rendered in the browser, by which time PHP is no longer involved. You need to understand the distinction between client and server-side code: they are quite distinct, and run at differerent times.

    language = "JavaScript" is obsolete; it should be:

    Code:
    <script type="text/javascript">
    </script>
    (although type is no longer essential). It is also generally accepted now that HTML tags should be in lowercase - it looks out-dated otherwise.
    "The mysql extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used." the docs
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0
    Originally Posted by phpstudent
    Hi,

    How do i store javascript value of q1 into php variable $ff? Its not working.
    PHP Code:

    <html>

    <SCRIPT language="JavaScript">

    var q1=Math.floor(Math.random()*11)
    <?php
    $ff 
    =  q1;
    ?>

     }

    </SCRIPT>

    </HEAD>

    <BODY>
    <?php
    echo $ff ;
    ?>
    </body>
    </html>


    U can use this ... If you wish to do like that:

    <html>
    <head>
    <title>Untitled</title>
    <script>
    var p1 = "success";
    </script>
    </head>

    <body>

    <?php
    echo "<script>document.writeln(p1);</script>";
    ?>

    </body>
    </html>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    64
    Rep Power
    9
    Perhaps if you give us a better idea of what you are trying to accomplish we can give you a better suggestion.

    Php is executed before the page is pushed to the client and javascript is excuted as the page is loading in the browser (although it is often benificial to tell the browser to not execute it until the entire page is loaded) and after event on the loaded page.

    The only way to get the variable back to the php interpreter is to call the page again passing variables, or use an ajax call to the server.

    As your example is written it is easiest to simply do everything in javascript.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,871
    Rep Power
    6351
    The new user guide actually covers this exact scenario explicitly.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    You can try by using hidden variable. or else generally use some input type.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    San Jose
    Posts
    9
    Rep Power
    0
    You cannot use java-script inside PHP like that, because one runs on the client, the other on the server.

IMN logo majestic logo threadwatch logo seochat tools logo