Hi all,
I'm having a few issues integrating some code into an existing SMARTY/PHP installation, hopefully someone will be able to point us in the right direction...
Here goes, firstly I have the following code which allows a user to select an item from a dropdown list and update some totals based on the selection made, when I run this in isolation, everything is fine and calculates the totals as expected. The option list is generated from database entries.
[FONT="Courier New"]<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'password';
$totalcost = 0;
$ordercost = 10;
$e_delivery = 0;
$shipping_cost = 0;
$e_totalcost = $ordercost +$e_delivery;
$database = 'mydb';
$table = 'shipping';
if (!mysql_connect($db_host, $db_user, $db_pwd))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
<html>
<head>
<script>
<!--
var shipping = Array ();
<?
$sql = "SELECT shipping_id, shipping_cost FROM shipping";
$r = mysql_query($sql);
while($line = mysql_fetch_assoc($r)) {
?>
shipping[<?=$line['shipping_id']?>] = <?=$line['shipping_cost']?>;
<?
}
?>
function updatetotal() {
var ordercost = parseFloat(document.getElementById('ordercost').value);
var e_totalcost = document.getElementById('total');
var e_delivery = document.getElementById('delivery');
var shipping_cost = shipping[document.getElementById('delivery').value];
e_totalcost = shipping_cost + ordercost;
e_delivery = shipping_cost;
document.getElementById('shipping_cost').value = e_delivery;
document.getElementById('e_totalcost').value = e_totalcost;
}
-->
</script>
<title>Populate Select List from db table</title>
</head>
<body>
<form name='shippingdetails' id='shippingdetails' action="shipping.php">
<p>
<br> Your order total is: <input type='text' name='ordercost' id='ordercost' value=<?=$ordercost?>>
<br>
Delivery Costs: <input type='text' name='shipping_cost' id='shipping_cost' value=<?=$shipping_cost?>>
<br>
Total Due: <input type='text' name='e_totalcost' id='e_totalcost' value=<?=$e_totalcost?> >
</p>
<select name="delivery" id="delivery" onChange="updatetotal();">
<?php
$sql = "SELECT shipping_id, shipping_type FROM shipping";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['shipping_id']."\">".$row['shipping_type']."\n ";
}
?>
</select>
</form>
</body>
</html>[/FONT]
So far so good, however now stuck trying to integrate that into a SMARTY / PHP setup. In the tpl, the drop down list is generated using the following code:
[FONT="Courier New"]Delivery options:
<select name="shipping" id="shipping" onchange="updatetotal();">
{section name=i loop=$obj->mShippingInfo}
<option value="{$obj->mShippingInfo.shipping_id}">
{$obj->mShippingInfo.shipping_type}
</option>
{/section}[/FONT]
but not sure where to go from there, cannot get the variables to add, keeps generating an error. Also tried parsing into an if statement but still no luck, can anyone help?
thanks in advance.