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
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question javascript var value outside javascript

    This is my javascript:

    <script type=text/javascript>
    function alert(){var value=1; alert("value");}
    </script>

    <?php
    Some Code....
    ?>

    Now i want to convert "var value" in to php variable in my php script on the same page. How can i do it?

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    It's not quite clear what you are trying to achieve. If you need a php variable, just declare it.
    PHP Code:
    <?php $php_var 1?>

    <script type=text/javascript>
      function alert(){var value=<?php echo $php_var?>; alert("value");}
    </script>

    <?php
    // Some Code...
    ?>
    I am still learning English

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Quote Originally Posted by sunnynosid View Post
    This is my javascript:

    <script type=text/javascript>
    function alert(){var value=1; alert("value");}
    </script>

    <?php
    Some Code....
    ?>

    Now i want to convert "var value" in to php variable in my php script on the same page. How can i do it?
    You can't. Period.

    The ONLY way to send the value of a JavaScript variable to PHP code is to make an HTTP Request of the PHP server and get back an HTTP Response.

    You can do that by
    (a) submitting a <form>
    (b) using location.href="somephppage.php?value=" + value
    (c) using AJAX

    Read post #2 in this thread:
    http://www.codingforums.com/showthread.php?t=247585
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Sep 2011
    Posts
    50
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question

    I think this would make clear to you.
    This is my script:

    <html>
    <head>
    <script type=text/javascript>
    value=1
    value=<?php $num?>;
    </script>
    <?php echo"$num";?>
    </head>
    </html>

    I am trying to convert the javascript variable "value" in to php variable "$num".
    Is this script correct for that? If not correct then please correct it.

  • #5
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    If the JavaScript variable value may dynamically change, then consider Old Pedant's suggestions.
    I am still learning English

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    I can't make any sense of that script, anyway.

    First you set the JavaScript variable value to 1.

    Then you change it to equal the value of the PHP variable $num.

    No place do you even attempt to change the PHP variable to be equal to the JavaScript variable. WHich is a good thing, because you can't do it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    I may add that JS and PHP have different execution times. namely PHP first and way after that JS. hence to pass a value from JS to PHP use the suggestions from Old_Pedant.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    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
    •