• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

PHP Variables in Javascript

Canadian
Loyal Member
Joined
Dec 4, 2007
Messages
1,936
Reaction score
96
Code:
<?php 
    $pricequery = mysql_query("SELECT * FROM `prices`");
    $row = mysql_fetch_row($pricequery);
    $one = $row['0'];
    $five = $row['5'];
    $twenty = $row['20'];
    $onehundred = $row['100'];
?>
<script type="text/javascript">
    $('input#GoldQuantity').keyup(function() {
        var goldQuantity = $('input#GoldQuantity').val();
        if (parseInt(goldQuantity) != goldQuantity) {
            $('#Update').html('Input Error');
        } else {
            var multiplier = -1; // If something goes wrong, then the user will notice that their total is oddly negative.
            if (goldQuantity >= 251) {
                multiplier = 99999; //Orders over 250M
            } else if (goldQuantity >= 100) {
                multiplier = <?php $onehundred ?>; //Orders over 100M
           } else if (goldQuantity >= 20) {
                multiplier = <?php $twenty ?>; //Orders over 20M
            } else if (goldQuantity >= 5) {
                multiplier = <?php $five ?>; //Orders over 5M
            } else if (goldQuantity >= 0) {
                multiplier = <?php $one ?>; //Orders over 1M
            }
            var algorithm = goldQuantity * multiplier;
            $('#Update').html('$' + algorithm);
        }
    });
</script>

I've been trying to get these PHP variables to work in Javascript, but they won't. Are you unable to do this for some reason?

It worked fine before, but now with the variables there is just doesn't update (when the value in one box is edited by the user, this one is supposed to change based on that - live in the browser).
 
Joined
Nov 17, 2008
Messages
800
Reaction score
1,392
for starters, in the php that's inside the javascript, you need to echo it, and end with a semicolon. I'm still not sure if that will work though, I've have problems with php inside javascript before.
 
Canadian
Loyal Member
Joined
Dec 4, 2007
Messages
1,936
Reaction score
96
Well don't I feel dumb haha.

Thanks Chris!
 
Joined
May 23, 2008
Messages
1,071
Reaction score
574
Mike had a couple issues here. He wasn't echoing the variables correctly and he was attempting to access nonexistant array keys of the $row variable. The actual keys were 0, 1, 2, and 3. I helped him get this all fixed up and it works fine now.

And I'll agree with jMerliN's - his suggestion is, IMO, the best practice in this situation.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
It's hard to mix server-side logic with client-side logic when they are in the same file. A solution to the hardness is to use a service to transfer data from one to the other. JSON or XML are languages JavaScript and PHP both work well with, so you can use one of those languages as a middle-man to work around this problem. Mixing server-side and client-side logic gets very complicated very quickly, so a separation is surely needed. PHP and JavaScript work so much differently that it's impractical to put PHP tags inside of a JavaScript file, and just as impractical to put HTML or JavaScript inside of a PHP file. I've seen plenty of young coders learn this lesson the hard way.
 
Back
Top