Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:17:47+00:00 2026-06-14T16:17:47+00:00

Possible Duplicate: how to access PHP variables from within JavaScript? I am trying to

  • 0

Possible Duplicate:
how to access PHP variables from within JavaScript?

I am trying to pass a variable from mySQL to PHP then to JavaScript to perform some math on it then have the result displayed on page in a specific area (in a <p> element with a specific ID assigned to it). Is this possible or is there an easier solution?

Thanks!

I already know how to get the variable from MySQL to PHP just having trouble on what to do after that.

Here is my updated code after reading responses. Still not working but I am sure I’m doing something wrong!

<p id="p_id"><?php echo $price; ?></p>
<input type="radio" group="radio_group" value="Reduce 10%" onclick="radio_click();" />

<script type="text/javascript">
var price = <?php echo $price; ?>;

function radio_click() {
var target = document.GetElementById('p_id');
var final_number; // this will be variable your store the final number in

final_number = price * .9;
target.innerHTML = final_number;

}
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:17:48+00:00Added an answer on June 14, 2026 at 4:17 pm

    In short, you have to pass the variable to the <script> tag as a string in a JavaScript friendly format (quotes and stuff).

    <?php
    
    $variable = 5;
    $javaScriptAccessible = '
        <script type="text/javascript">
            var javaScriptAccessible = "'. $variable . '";
        </script>
    ';
    
    echo $javaScriptAccesible;
    

    Also, you could JSON it:

    <?php
    
    $variable = 5;
    $javaScriptAccessible = '
        <script type="text/javascript">
            var javaScriptAccessible = '. json_encode($variable) .';
        </script>
    ';
    
    echo $javaScriptAccessible;
    

    With JSON, the quotes would be appended automatically.

    Here you can see both in action: http://codepad.viper-7.com/Jyfw31

    Update:

    Here are more refined and, I think, better to understand examples: http://codepad.viper-7.com/1cloyB
    Only thing you have to do, is use it on your radio buttons / elements.

    I’d strongly suggest going with JSON, because it actually stands for JavaScript Object Notation, it has libraries in, probably, every single programming language on planet, and is specifically designed to pass JS data from one environment to other.

    http://php.net/manual/en/function.json-encode.php here you can see multiple options you’re able to pass in order to render it differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Access a JavaScript variable from PHP Let's say I have the following
Possible Duplicate: javascript object, access variable property name? I'm trying to call a method
Possible Duplicate: Pass a PHP string to a Javascript variable (including escaping newlines) So
Possible Duplicate: How to access PHP variables in JavaScript or jQuery rather than <?php
Possible Duplicate: Access GET variables with PHP + .htaccess I am trying write a
Possible Duplicate: Objective C Equivalent of PHP’s Variable Variables Today in one of my
Possible Duplicate: PHP - MySQL access denied error - Works in other programs i'm
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: How to access a variable in a PHP class that is set
Possible Duplicate: Is it possible to access outer local variable in PHP? PHP closure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.