Hi, I am a PHP developer I never learned much JS but there is no way to accomplish this function using only PHP without a refresh. I am trying to adapt a script that did the basics.
<script language="JavaScript">
function som()
{
var First, Last, Full;
First = document.Credit.First.value;
Last = document.Credit.Last.value;
Full = document.Credit.Full.value;
document.Credit.Full.value = (First / Last);
}
</script>
My page is a dynamic php page using MySQL and I have created the JS using concatenation of strings in a loop for those items pulled from the DB table where sts==1 - this is really complicated to explain and this is my 2nd query on any forum and I only use daniweb. OK
Overall, if using the CMS the site owner selects certain items [sts==1] then I wrote the following to mark-up in PHP
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($row['sts'] ==1){
$jsvar .= " unit_price_". $row['pid'] .", " . "qty_" . $row['pid'] . ",";
$jsunit .= "unit_price_". $row['pid'] ."=document.box.unit_price_". $row['pid'] .".value; ";
$jsqty .= "qty_". $row['pid'] ." = document.box.qty_" . $row['pid'] .".value; ";
$jstot .= "document.box.tot.value;";
$jscalc .= " (unit_price_". $row['pid'] ." * qty_". $row['pid'] .") + ";
}
}
$jsvar .=" tot;";
$jscalc.= " 0;";
$jscomplete = "<script language='JavaScript'>function sumtot() {". $jsvar . $jsunit . $jsqty . $jscalc ."}</script>";
This produces, in one instance the following:-
<script language='JavaScript'>
function sumtot()
{
var unit_price_8, qty_8, unit_price_9, qty_9, unit_price_10, qty_10, unit_price_12, qty_12, tot;
unit_price_8=document.box.unit_price_8.value; unit_price_9=document.box.unit_price_9.value; unit_price_10=document.box.unit_price_10.value; unit_price_12=document.box.unit_price_12.value;
qty_8 = document.box.qty_8.value; qty_9 = document.box.qty_9.value; qty_10 = document.box.qty_10.value; qty_12 = document.box.qty_12.value; document.box.tot.value = (unit_price_8 * qty_8) + (unit_price_9 * qty_9) + (unit_price_10 * qty_10) + (unit_price_12 * qty_12) + 0;}</script>
This is dynamic creation of JS code always ends up with the calclation equalling zero.
IF ANYONE CAN FOLLOW THIS AND ANSWER WITH AN ANSWER THAT WORKS I WILL BE MORE THAN HAPPY TO USE YOU AS A CONTRACTOR...
The whole code for the PHP on this page is -
<?php
session_start();
require_once ('../mysql_connect.php'); // Connect to the db.
$cid = $_POST['cid'];
$surname = $_POST['surname'];
$order = $_POST['order'];
/* Retrieve the cid and surname for that combination. */
$query = "SELECT * FROM customer WHERE cid='$cid' AND surname='$surname'";
$result = @mysql_query ($query); // Run the query.
$row = mysql_fetch_array ($result, MYSQL_NUM); // Return a record, if applicable.
/* ============ ALL OK WITH LOGON ================ */
if ($row) { // A record was pulled from the database.
// Set the session data & redirect.
session_name ('YourVisitID');
session_start();
$_SESSION['cid'] = $row[0];
$_SESSION['surname'] = $row[3];
$_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
/* ============= LOG DETAILS ============= */
$title = $row[2];
$surname = $row[3];
$add1 = $row[4];
$add2 = $row[5];
$add3 = $row[6];
$add4 = $row[7];
$pc = $row[8];
$round = $row[19];
$email = $row[9];
/* =============== CREATE JS =============== */
$query = "SELECT * FROM product";
$result = mysql_query($query);
$jsvar = "var ";
$jsunit = "";
$jsqty = "";
$jstot = "";
$jscalc = "document.box.tot.value = ";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($row['sts'] ==1){
$jsvar .= " unit_price_". $row['pid'] .", " . "qty_" . $row['pid'] . ",";
$jsunit .= "unit_price_". $row['pid'] ."=document.box.unit_price_". $row['pid'] .".value; ";
$jsqty .= "qty_". $row['pid'] ." = document.box.qty_" . $row['pid'] .".value; ";
$jstot .= "document.box.tot.value;";
$jscalc .= " (unit_price_". $row['pid'] ." * qty_". $row['pid'] .") + ";
}
}
$jsvar .=" tot;";
$jscalc.= " 0;";
$jscomplete = "<script language='JavaScript'>function sumtot() {". $jsvar . $jsunit . $jsqty . $jscalc ."}</script>";
/* ============== LIST DETAILS ============ */
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" type="text/css" href="http://www.slipstream-organics.co.uk/css.css" />
<meta name="author" content="Andrew Frazer" />
<meta name="description" content="" />
<meta name="keywords" content="" /> ';
echo $jscomplete;
echo '
</head>
<body>
<img src="../images/logo.gif" width="400" height="47">
';
?>
<script language="JavaScript">
document.write('IS THIS PRINTING');
document.write(unit_price_8);
</script>
<?php
echo"
<table style='width:400px;'>
<tr>
<td colspan='2' style='color:#363;font-weight:bold;text-align:left;line-height:1.5em;font-family:verdana, helvetics, sans-serif;'>Your Details</td>
</tr>
</table>
<table style='width:400px;background:rgb(230,240,230);'>
<tr>
<td><p class='header'>Name: </p></td><td><p class='header'>" . $title . " " . $surname. "</p></td>
</tr>
<td style='vertical-align:top;'><p class='header'><p class='header'>Address: </p></td>
<td><p class='header'>" . $add1 . " " . $add2 . "<br />" . $add3 . "<br />" . $add4 . "<br />" . $pc . "</p></td>
</tr>";
if ($email) {
echo "
<tr><td><p class='header'>Email: </td><td><p class='header'>" . $email . "</p></td></tr>";
}
echo" <tr>
<td><p class='header'>Delivery Round: </p></td><td><p class='header'>" . $round . "</p></td></tr></table>";
echo "<br />";
if ($order == "replace"){
echo "<p class='header' style='color:#800;'>THIS ORDER WILL <b>REPLACE</b> YOUR ORDER <b>THIS</b> WEEK</p>";
} else {
echo "<p class='header' style='color:#800;'>THIS ORDER WILL <b>BE ADDED</b> YOUR ORDER <b>THIS</b> WEEK</p>";
}
echo "<p>Some text here to explain what to do, Nick...</p>";
echo "<form method='POST' name='box' action='order.php'><table style='color:#363;font-size:85%;font-family:verdana, helvetica, sans-serif;'>
<tr>
<td><b>Product</b></td>
<td style='width:50px;text-align:left;'><b>Unit</b></td>
<td style='text-align:right;'><b>Price</b></td>
<td><b>Qty</b></td>
<td><b>Line Total</b></td>
</tr>";
/* Retrieve the data for form. */
$query = "SELECT * FROM product";
$result = mysql_query($query);
$jsvar = "var ";
$jsunit = "";
$jsqty = "";
$jstot = "";
$jscalc = "document.box.tot.value = ";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
if ($row['sts'] ==1){
echo "<tr>
<td>" . $row['product_description'] . "</td>
<td>" . $row['unit'] . "</td>
<td>£" . $row['unit_price'] . "</td><input type='hidden' name='unit_price_". $row['pid'] ."' />
<td><input type='text' size='4' name='qty_" . $row['pid'] . "' /><td>
<td>ln tot</td>
</tr>";
$jsvar .= " unit_price_". $row['pid'] .", " . "qty_" . $row['pid'] . ",";
$jsunit .= "unit_price_". $row['pid'] ."=document.box.unit_price_". $row['pid'] .".value; ";
$jsqty .= "qty_". $row['pid'] ." = document.box.qty_" . $row['pid'] .".value; ";
$jstot .= "document.box.tot.value;";
$jscalc .= " (unit_price_". $row['pid'] ." * qty_". $row['pid'] .") + ";
}
}
$jsvar .=" tot;";
$jscalc.= " 0;";
// echo $jsvar;
// echo $jsunit;
// echo $jsqty;
// echo $jscalc;
echo "</table><input type='submit' value='Send Order' />";
echo "<input name='tot' type='text' /><input type='button' value='Calculate' onClick='sumtot()'></form>";
?>
<script language="JavaScript">
document.write('IS THIS PRINTING');
document.write(unit_price_8);
document.write(qty_8);
</script>
<?php
/* ============== LOGON FAILED ============= */
} else {
echo "Sorry your details do not match those on our records.<br />Click <a href='http://www.slipstream-organics.co.uk/makeyourownbox/index.php'>here</a> to go back.";
}
mysql_close(); // Close the database connection.
?>
</body>
</html>