Jump to content

Incorporating small Javascript function into my PHP shopping cart


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).
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.