slaterino Posted January 4, 2010 Share Posted January 4, 2010 Hey, I have created a small Javascript function which automatically calculates the postage of an order using a small javascript function. I am now trying to include it in my shopping cart and having some problems. The Javascript works by replacing whatever is in the contents of a <div id="regiondiv"> with the necessary value from my database. At the moment, the last lines of my shopping cart look as such: $output[] = '<h3 align="right">Postage: £<div id="regiondiv">0.00</div></h3>'; $output[] = '<h2 align="right">Total: £'.$total.'</h2>'; When I choose a value in a drop-down box the value in the 'regiondiv' div changes to the correct postage. I can't seem to work out how to have this also affect the $total as well though. I've tried some basic things like: $postage = '<div id="regiondiv">0.00</div>'; $total += ($Price + $postage); But had no success with this. Does anyone know how I could add the value from this <div> tag to my final total??? Quote Link to comment https://forums.phpfreaks.com/topic/187123-incorporating-small-javascript-function-into-my-php-shopping-cart/ Share on other sites More sharing options...
KevinM1 Posted January 4, 2010 Share Posted January 4, 2010 Short answer: Use AJAX. Long answer: Use AJAX, and here's why: JavaScript executes after PHP has run on the server. It also executes within the browser itself. Both of these attributes mean that JavaScript cannot interact with your server side script (including whatever objects you're using) unless you use AJAX techniques. Quote Link to comment https://forums.phpfreaks.com/topic/187123-incorporating-small-javascript-function-into-my-php-shopping-cart/#findComment-988320 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 This code does not work and you should know if you knew how PHP operated $postage = '<div id="regiondiv">0.00</div>'; $total += ($Price + $postage); Is the same as: $total += ($Price + 0); http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion More specifically If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Quote Link to comment https://forums.phpfreaks.com/topic/187123-incorporating-small-javascript-function-into-my-php-shopping-cart/#findComment-988325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.