3

basically what I've been trying to do is with PHP get a integer from MySQL after that is done, using JavaScript get the integer that PHP has and display it on HTML

<?php include('ConnectionCode.php');

$conn = mysqli_connect($svr, $usr, $pwd, $db) or die("Could not connect " . mysql_error());

$sql = "SELECT RetailPrice FROM WebHosting_PricingCOP WHERE id='32402' LIMIT 1";
$result = mysqli_query($conn, $sql);
$price = mysqli_fetch_array($result);

echo json_encode(number_format($price['RetailPrice'],0,".",","));

mysqli_close($conn)
?>

the code above will connect to the Database and get the value 59,347 now I'm trying to move this single value from PHP to Javascript in order to display it on HTML

<script type="text/javascript">
var PriceValue = "<?php echo json_encode($price) ?>";
document.write('<h3>'+PriceValue+'</h3>');
</script>

I have gone through many discussions and options here and there and still cant figure out to make it work, when i try to run the html it doesn't display anything

I would greatly appreciate your input

6
  • PHP runs on the server, Javascript runs on the browser, PHP does nto run on the browser. You may need to look into AJAX as the mechanism of getting data from PHP to Javascript Commented Mar 11, 2017 at 23:39
  • 1
    console.log(PriceValue); before your document.write. What appears in the console? Commented Mar 11, 2017 at 23:41
  • Are your HTML and PHP in the same file? What exactly is the role of the Javascript? If you just want to display the value, it's enough to just echo it with PHP. Commented Mar 11, 2017 at 23:45
  • everything is on the same file, the reason i going to such extends there are some prices within the website I'm building i will be changing the retail prices, and i figure that it would be easier to change them on MySQL and have a code to dynamically change it elsewhere, instead of manually changing it in over 20-30 places. i'm fairly new to web development Commented Mar 11, 2017 at 23:50
  • The issue appears to be that you're not giving an index for $price. It should be $price['RetailPrice'] should it not? Commented Mar 11, 2017 at 23:55

1 Answer 1

1

Update:

You may also have a problem with using json_encode inside of "" quotes.

var PriceValue = "<?php echo json_encode($price) ?>";

Instead, use:

var PriceValue = <?php echo json_encode($price) ?>;

or

var PriceValue = <?php echo $price ?>; // if $price is not an object

Note:

To debug this, check the generated HTML source to see what JavaScript you are actually generating.


$price needs to be in the same scope when you try to generate JS.

http://phpfiddle.org/main/code/aeav-gb1w

<?php
$price = 2523525;
?>

<script type="text/javascript">
var PriceValue = "<?php echo $price; ?>";
document.write('<h3>'+PriceValue+'</h3>');
</script>

However, it is going to be preferable for you to use your PHP file as an API endpoint instead of mixing your PHP and JavaScript together.

Sign up to request clarification or add additional context in comments.

1 Comment

your are indeed correct, rocky mistake, just learning as i go thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.