-
Thanks for the script, but does it go on the main page, with all of the item drop down boxes, or on the mail.php page? Because I cannot get it to work.:confused: Sorry, im really new to all of this. I wanted the calculator to work though still, because I want my clients to be able to see their running total WHILE they are choosing there items. I thought java script was the best way to do this. Im sure this is possible to do, but I have no clue how to do it. Thanks djr33 for the tip, if nothing else works, I guess i might have to do that, im just trying to save as much time possible.
Thanks twey, but could you help me a bit more on this script please?:)
-
Here's what I threw together. Uses Mike's slightly-famed Number.toCurrency() function.
Code:
<?php
// test.php
function toCurrency($amount) {
$amount *= 100;
$amount = round($amount);
$amount /= 100;
if(strpos($amount, ".") === false)
return $amount . ".00";
while(strlen(substr($amount, ".")) < 2)
$amount .= "0";
return $amount;
}
function getSelectedItems($items) {
$si = array();
foreach($_POST as $k => $v)
if(isset($items[$v])) array_push($si, $items[$v]);
return $si;
}
function formatItemList($items) {
$si = getSelectedItems($items);
$rt = 0;
$html = '<table style="text-align:center;"><tr><th>Item</th><th>Price</th><th>Running Total</th></tr>';
for($i = 0; $i < count($si); ++$i)
$html .= "<tr><td>" . $si[$i]['name'] . "</td><td>\$" . toCurrency($si[$i]['price']) . "</td><td>\$" . toCurrency($rt += $si[$i]['price']) . "</tr>";
$html .= "<tr style=\"font-weight:bold;\"><td>Total</td><td>\$" . toCurrency($rt) . "</td><td>\$" . toCurrency($rt) . "</td></tr></table>";
return $html;
}
$items = array(
"os0" => array(
"name" => "Microsoft Windows XP Professional Edition SP2",
"price" => 135.0
),
"os1" => array(
"name" => "Microsoft Windows XP Home Edition SP2",
"price" => 90.0
),
"os2" => array(
"name" => "Gentoo Linux",
"price" => 10.0
),
"os3" => array(
"name" => "Fedora Core 5",
"price" => 5.0
),
"os4" => array(
"name" =>"No OS",
"price" => 0.0
),
"cpu0" => array(
"name" => "AMD Athlon 64 FX55 San Diego 2000MHz HT Socket 939",
"price" => 639.0
),
"cpu1" => array(
"name" => "AMD Athlon 64 FX57 San Diego 2000MHz HT Socket 939",
"price" => 811.0
),
"cpu2" => array(
"name" => "AMD Athlon 64 FX60 Toledo 2000MHz HT Socket 939 Dual Core",
"price" => 853.0
)
// Add more. Doesn't matter what the names are, so long as they match the values on the form.
);
if(!isset($_POST['calculate'])) { ?>
<html>
<head>
<title>F</title>
<script type="text/javascript">
Number.prototype.toCurrency = function(c, t, d) {
// Mike Winter (mwinter)'s Number.toCurrency() function.
var n = +this,
s = (0 > n) ? '-' : '',
m = String(Math.round(Math.abs(n))),
i = '',
j, f;
c = c || '';
t = t || '';
d = d || '.';
while (m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while (j > 3) {i = t + m.substring(j - 3, j) + i; j -= 3;}
i = m.substring(0, j) + i;
return s + c + i + d + f;
};
var items = new Object();
<?php foreach($items as $k => $v) { ?>
items['<?php echo($k); ?>'] = <?php echo(toCurrency($v['price']) * 100); ?>;
<?php } ?>
function calcTotal(frm) {
var total = 0;
for(var i=0; i<frm.elements.length; i++) {
if(typeof items[frm.elements[i].value] != "undefined")
total += items[frm.elements[i].value];
}
return total;
}
window.onload = function() {
for(var i=0;i<document.forms[0].elements.length;i++)
if(document.forms[0].elements[i].tagName.toLowerCase() == "select")
document.forms[0].elements[i].onchange = function() {
document.getElementById("ttl").innerHTML = calcTotal(this.form).toCurrency('$');
};
document.getElementById("ttl").innerHTML = calcTotal(document.forms[0]).toCurrency('$');
};
</script>
</head>
<body>
<form action="<?php echo($PHP_SELF); ?>" method="post">
<input type="hidden" name="calculate" value="true">
<select name="os">
<?php for($i = 0; isset($items["os$i"]); $i++) { ?>
<option value="os<?php echo($i); ?>"><?php echo($items["os$i"]['name']); ?> ($<?php echo(toCurrency($items["os$i"]['price'])); ?>)</option>
<?php } ?>
</select>
<br>
<select name="cpu">
<?php for($i = 0; isset($items["cpu$i"]); $i++) { ?>
<option value="cpu<?php echo($i); ?>"><?php echo($items["cpu$i"]['name']); ?> ($<?php echo(toCurrency($items["cpu$i"]['price'])); ?>)</option>
<?php } ?>
</select>
<br>
<p style="font-weight:bold;">
Total: <span id="ttl"></span>
<input type="submit">
</p>
</form>
</body>
</html>
<?php } else { ?>
<html>
<head>
<title>Confirmation Page</title>
</head>
<body>
<?php echo(formatItemList($items)); ?>
</body>
</html>
<?php } ?>
It lacks your formatting and data, since I was merely experimenting. I'm sure you should have no trouble integrating it.
Marginally tested.
-
SWEET!!! Thats just what i needed. THANK YOU VERY MUCH. I appreciate it.:) :)
But, one last thing, I promise! How do I change the font of the listed items and prices on the confirmation page? Whenever I mess with the script, it no longer works, but that's probably because I dont know what the heck im doing.
If you could let me know how to change the font, that would be great. Thanks.
-
You can wrap the PHP blocks in a styled element just as you would with any other table.
Also, any of the HTML in the formatItemList() function can be edited easily enough, with the stipulation that you escape special characters such as double quotes and dollar signs.
-
Thanks again Twey. I have everything how I want it now, but now I need to email that confirmation page with the buyers name, credit card info and other information. How would I do that, using what you have already made?
I tried doing something like this, just to email myself the setup of the system, but I know it's completely wrong, here is a piece of it:
Code:
function confirm {
$to = "josh@oharanetworks.com";
$subject = "X-7000 Order";
$message = "<?php echo (formatItemList($items)); ?>";
$from = "someonelse@anotherplace.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
}
function getSelectedItems($items) {
$si = array();
foreach($_POST as $k => $v)
if(isset($items[$v])) array_push($si, $items[$v]);
return $si;
}
function formatItemList($items) {
$si = getSelectedItems($items);
$rt = 0;
$html = '<img src="../../images/PNG/logo_build_7000_confirmation.png"><br><table style="text-align:right; border-left:1px solid; border-right:1px solid; border-top:1px solid; border-bottom:1px solid;"><tr><th width="737">Item</th><th width="79">Price</th><th width="84">Running Total</th></tr>';
for($i = 0; $i < count($si); ++$i)
$html .= "<tr><td>" . $si[$i]['name'] . "</td><td>\$" . toCurrency($si[$i]['price']) . "</td><td>\$" . toCurrency($rt += $si[$i]['price']) . "</tr>";
$html .= "<tr><td></td></tr><tr><td></td></tr><tr><td><b>Total:</b></td><td>\$" . toCurrency($rt) . "</td></tr></table>";
return $html;
$html = '<form action="<?php function confirm?>" method="post"><input type="submit" name="submit" value="Confirm Order">';
}
What I was trying to do is link the "Confirm Order" button to the confirm function, but, as you can tell, I did it wrong, because it doesn't work.
Thanks again for your help, it is greatly appreciated!
Again, the working url is here: http://www.oharanetworks.com/jcr/build/7000/
-
Blank TDs won't display in some browsers. You should put " " inside them at least.
HTML outside the table (with particular attention to that image) would be better put here:
Code:
<?php } else { ?>
<html>
<head>
<title>Confirmation Page</title>
</head>
<body>
<?php echo(formatItemList($items)); ?>
</body>
</html>
<?php } ?>
... where you can enter it in plain, nicely-formatted HTML.
The confirm function should look something like this:
Code:
function confirm() {
global $items;
$to = "josh@oharanetworks.com";
$subject = "X-7000 Order";
$message = formatItemList($items));
$from = "someonelse@anotherplace.com";
$headers = "From: $from";
mail($to, $subject, $message, $headers);
}
-
Thanks Twey for all of your help, that will work just fine.:) :)
-
Minus the extra bracket on the $message line, of course.
-
Hi I know this is an old discussion but can I please have some help with modifying this to suit my needs?
I like the running total and I would like to make quote/order form that will then email both of us the order total but then using a session go to the next form to get them to pay a deposit for the work.
Does that make sense?
Thanks...I am confusing myself at the moment.