Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?
if(isset($_POST['submit'])) {
//correct the following code for your DB server / Db
//and uncomment the connection code block below
/*
$host = "localhost";
$user = "db_user_name";
$pass = "db_user_password";
$dbname = "database_name";
$link = mysql_connect($host, $user, $pass) or die('Could not connect to database server');
mysql_select_db($dbname) or die('Cannot select database');
*/
$oktotal = '^([1-9]{1})([0-9]{0,})\.([0-9]{2})$';
if(!ereg($oktotal, $_POST['mytotal'])) {
$message = "<p><strong>Sorry, your input total is not in correct format.</strong></p>\n";
}
else {
$price = $_POST['myprice'];
$quan = $_POST['myqty'];
$tax = $_POST['mytax'];
$total = $_POST['mytotal'];
//change name of table and fields below
//and uncomment code block to do insert
/*
$sql = "INSERT INTO mytable(mycolumn) VALUES ($total)";
if($rs = mysql_query($sql)) {
*/
$message = "<p>Your order total of $$total has been entered in the database.</p>\n";
/*
}
else {
$message = "<p><strong>There was an error entering your order total.</strong></p>\n";
}
*/
}
}
else {
$price = '0.00';
$quan = '1';
$tax = '1.00';
$total = '0.00';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Using PHP / MySQL To Store A Form Value Calculated Via JavaScript / DOM
Copyright 2006 Doug Vanderweide
http://www.dougv.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
Any distribution or derivative work based on this script must retain this copyright
notice block intact.
-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Using PHP / MySQL To Store A Form Value Calculated Via JavaScript / DOM</title>
<link href="../demo.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function getOrderTotal() {
var icost = document.myform.myprice.value;
var iqty = document.myform.myqty.value;
var itax = getSalesTax();
var recurrency = '^[0-9]{1,5}\.[0-9]{2}$';
var reitems = '^([1-9])([0-9]{0,2})$';
if(!icost.match(recurrency)) {
alert('Please provide an item cost in 0.00 format!');
}
else if(!iqty.match(reitems)) {
alert('Please provide a quantity of 1 to 999!');
}
else {
var itotal = (icost * iqty) * itax;
itotal *= 100;
itotal = Math.ceil(itotal);
itotal /= 100;
if(itotal.toFixed) {
itotal = itotal.toFixed(2);
}
document.getElementById('mytotal').value = itotal;
}
}
function getSalesTax() {
var taxarray = document.myform.mytax;
var retax = '^[1]{1}\.[0-9]{1,4}$';
var i;
for(i=0; i < taxarray.length; i++) {
if(taxarray[i].checked) {
if(!taxarray[i].value.match(retax)) {
alert('Please provide a tax rate from the button list only!');
return 0;
}
else {
return parseFloat(taxarray[i].value);
}
}
}
return 1.0;
}
</script>
</head>
<body>
<h2>Using PHP / MySQL To Store A Form Value <br />
Calculated Via JavaScript / DOM</h2>
<?
echo $message;
?>
<form id="myform" name="myform" method="post">
<table>
<tr>
<td class="label">Enter item cost in dollars (0.00 format): </td>
<td>
$<input name="myprice" type="text" id="myprice" size="10" maxlength="10" onchange="getOrderTotal()" value="<? echo $price; ?>" />
</td>
</tr>
<tr>
<td class="label">How many items do you want? </td>
<td>
<input name="myqty" type="text" id="qty" onchange="getOrderTotal()" value="<? echo $quan; ?>" size="3" maxlength="3" />
</td>
</tr>
<tr>
<td class="label">What state do you live in? </td>
<td>
<input name="mytax" type="radio" value="1.05" onclick="getOrderTotal()" <? if($tax == '1.05') { echo "checked=\"checked\""; } ?> />&nbsp;Maine (5% sales tax)
<br />
<input name="mytax" type="radio" value="1.0725" onclick="getOrderTotal()" <? if($tax == '1.0725') { echo "checked=\"checked\""; } ?> />&nbsp;California (7.25% sales tax)
<br />
<input name="mytax" type="radio" value="1.00" onclick="getOrderTotal()" <? if($tax == '1.00') { echo "checked=\"checked\""; } ?> />&nbsp;Other (no sales tax)
</td>
</tr>
<tr>
<td class="label">Your order total: </td>
<td><input name="mytotal" type="text" id="mytotal" readonly="true" value="<? echo $total; ?>" /></td>
</tr>
</table>
<br />
<input name="submit" type="submit" id="submit" value="Place Order" />
</form>
<p><a href="http://www.dougv.com/blog/2007/01/01/using-php-mysql-to-store-a-form-value-calculated-via-javascript-dom/" rel="bookmark" title="Permanent Link: Using PHP / MySQL To Store A Form Value Calculated Via JavaScript / DOM">Using PHP / MySQL To Store A Form Value Calculated Via JavaScript / DOM</a></p>
<p>&nbsp;</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment