PDA

View Full Version : Passing Variables (php to js to php)



aazad
02 Oct 2008, 07:56 PM
Hi Guys,

I'm creating an online qouting system where clients put together a list of items they want to order and get an instant quote. They have the option to include labour by a checkbox. (If checked add labour to total if unchecked don't).

I'm trying to pass my labour and total values from estimate.php to summary.php but am hitting some problems.

When a client doesn't want labour (box unchecked) total value passes fine. When a client wants labour included (box checked) labour value passes fine but total is an empty space.

My labour checkbox has an input id, total has a div id. These are passed into my js function and a sum is calculated. Due to my sum returning r this is the total and I think I need to convert this back into php in order to pass it to summary.php. I'm a bit confussed and lack knowledge with php and js inpurticular so any help or advice is greatly appreciated.

Below is my code to help you understand my problem a bit better.

estimate.php

var oldTotal=0;
function showlabour(total,labour) {
//alert(total);
if(document.getElementById('labour').checked == true) {
total = parseFloat(total) + labour;
oldTotal = total;
total = formatNumber(total,'2',',','.','','','','');
document.getElementById('total').innerHTML = '<b>&pound;'+total+'</b>';
}else {
total = parseFloat(oldTotal) - labour;
oldTotal = total;
total = formatNumber(total,'2',',','.','','','','');
document.getElementById('total').innerHTML = '<b>&pound;'+total+'</b>';
}
}

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
var x = Math.round(num * Math.pow(10,dec));
if (x >= 0)
n1=n2='';
var y = (''+Math.abs(x)).split('');
var z = y.length - dec;
if (z<0)
z--;
for(var i = z; i < 0; i++)
y.unshift('0');
y.splice(z, 0, pnt);
if(y[0] == pnt)
y.unshift('0');
while (z > 3)
{
z-=3;
y.splice(z,0,thou);
}
var r = curr1+n1+y.join('')+n2+curr2;
return r;
}

function OnButton2() {
document.form1.action = "summary.php"
document.form1.submit();
return true;
}


$jsTotal = $total;

echo "<input id='labour' type='checkbox' name='checkbox1' value='checkbox1' onclick='showlabour($jsTotal , $totallabourcost );' />";
echo 'Labour';
echo '&pound;', $totallabour, '<input name="totallabour" type="hidden" value="', $totallabour, '" />';

echo 'Total';
echo '<div id="total">&pound;', $total, '<input name="total" type="hidden" value="', $total, '" /></div>';

summary.php

if ( $_POST['checkbox1'] ) {
//add labour to the total
echo "labour ", ($_POST["totallabour"]), "<br/>";
echo "total ", ($_POST ["total"]), "<br/>";
} else {
//dont
echo "total ", ($_POST ["total"]), "<br/>";
}