Jump to content

Incorporating small Javascript function into my PHP shopping cart


slaterino

Recommended Posts

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???

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.

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).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.