我的网站上有以下表格,使用下面的Javascript代码进行实时计算,并通过PHP发送数据。
表单工作正常,并且在大多数情况下成功发送计算出的数字。但有时(可能是5个提交中的1个)缺少值,例如:
FLOWERS:
尺寸:50
价格:MRP 175.00
数量:5
升: 0.250 - 这很好!
通常情况下,上述内容如下:
FLOWERS:
尺寸:50
价格:MRP 175.00
数量:5
升: 0.000 - 这是错误的,缺少!
如果在用户的浏览器中禁用了JavaScript,情况会是这样吗?
HTML CODE: - LINK FOR HTML CODE
JAVASCRIPT代码:
<?php
if(empty($_POST['name']) || empty($_POST['address'])) {
die('Error: Missing variables');
}
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$telephone=$_POST['telephone'];
$email=$_POST['email'];
$address=$_POST['address'];
$state=$_POST['state'];
$city=$_POST['city'];
$pincode=$_POST['pincode'];
$total_amount=$_POST['total_amount'];
$flowers_size=$_POST['flowers_size'];
$flowers_price=$_POST['flowers_price'];
$flowers_quantity=$_POST['flowers_quantity'];
$flowers_litres=$_POST['flowers_litres'];
$cereals_size=$_POST['cereals_size'];
$cereals_price=$_POST['cereals_price'];
$cereals_quantity=$_POST['cereals_quantity'];
$cereals_litres=$_POST['cereals_litres'];
$vegetables_size=$_POST['vegetables_size'];
$vegetables_price=$_POST['vegetables_price'];
$vegetables_quantity=$_POST['vegetables_quantity'];
$vegetables_litres=$_POST['vegetables_litres'];
$fruits_size=$_POST['fruits_size'];
$fruits_price=$_POST['fruits_price'];
$fruits_quantity=$_POST['fruits_quantity'];
$fruits_litres=$_POST['fruits_litres'];
$plantations_size=$_POST['plantations_size'];
$plantations_price=$_POST['plantations_price'];
$plantations_quantity=$_POST['plantations_quantity'];
$plantations_litres=$_POST['plantations_litres'];
$ip=$_SERVER['REMOTE_ADDR'];
$to="customercare@drearth.in";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=utf-8\r\n";
$headers = 'From: '.$_POST['name'].' <'.$_POST['email'].'>';
'X-Mailer: PHP/' . phpversion();
$subject='Order from '.$name."\n\n\n";
$body.="<h2>DETAILS:</h2>"."";
$body.='Customer Name: <b>'.$name."</b><br>\n";
$body.='Mobile No: <b>'.$mobile."</b><br>\n";
$body.='Telephone No: <b>'.$telephone."</b><br>\n";
$body.='Email Address: <b>'.$email."</b><br>\n";
$body.='Address: <b>'.$address."</b><br>\n";
$body.='State: <b>'.$state."</b><br>\n";
$body.='City: <b>'.$city."</b><br>\n";
$body.='Pin code: <b>'.$pincode."</b><br><br>\n\n";
$body.="<h2>VARIANTS:</h2>"."";
$body.="<b>FLOWERS:</b>"."<br>\n";
$body.='Size: '.$flowers_size."<br>\n";
$body.='Price: '.$flowers_price."<br>\n";
$body.='Quantity: '.$flowers_quantity."<br>\n";
$body.='litres: '.$flowers_litres."<br><br>\n\n";
$body.="<b>CEREALS:</b>"."<br>\n";
$body.='Size: '.$cereals_size."<br>\n";
$body.='Price: '.$cereals_price."<br>\n";
$body.='Quantity: '.$cereals_quantity."<br>\n";
$body.='Litres: '.$cereals_litres."<br><br>\n\n";
$body.="<b>VEGETABLES:</b>"."<br>\n";
$body.='Size: '.$vegetables_size."<br>\n";
$body.='Price: '.$vegetables_price."<br>\n";
$body.='Quantity: '.$vegetables_quantity."<br>\n";
$body.='Litres: '.$vegetables_litres."<br><br>\n\n";
$body.="<b>FRUITS:</b>"."<br>\n";
$body.='Size: '.$fruits_size."<br>\n";
$body.='Price: '.$fruits_price."<br>\n";
$body.='Quantity: '.$fruits_quantity."<br>\n";
$body.='Litres: '.$fruits_litres."<br><br>\n\n";
$body.="<b>PLANTATIONS:</b>"."<br>\n";
$body.='Size: '.$plantations_size."<br>\n";
$body.='Price: '.$plantations_price."<br>\n";
$body.='Quantity: '.$plantations_quantity."<br>\n";
$body.='Litres: '.$plantations_litres."<br><br>\n\n";
$body.="<h2>AMOUNT:</h2>"."";
$body.='Total Price: <b>'.$total_amount."</b><br><br><br><br>\n\n\n\n";
$body.='IP address of the submitter: '."\n".$ip."\n";
$headers = "From: $email <$email>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'bcc: xxx@gmail.com' . "\r\n";
if(mail($to, $subject, $body, $headers)) {
header("Location: http://www.drearth.in/confirmation.html");
} else {
echo "Something has gone wrong! Please try again!";
}
?>
PHP代码:
if((sockAddr.sin_addr.s_addr = inet_addr(szServer)) == INADDR_NONE)
{
LPHOSTENT host;
host = gethostbyname(szServer);
if (host)
memcpy(&sockAddr.sin_addr,host->h_addr_list[0],host->h_length);
else
{
#ifdef LINUX
close(hSocket);
#else
closesocket(hSocket);
#endif
throw ECSmtp(ECSmtp::WSA_GETHOSTBY_NAME_ADDR);
}
}
答案 0 :(得分:1)
我对你原来的javascript函数进行了一些重构 - 我认为这是正确的,但你可能(如果你想的话)需要仔细检查才能看到它是正确的。要经常解决空白值的问题....如果没有启用javascript,你不应该允许提交表单 - 实现这个实际上使用javascript来提交表单而不是简单的提交按钮。另外,您可能想重新计算php中的值并检查它们是否匹配?
/* to format a number with correct separators ~ comma */
function format_thousands(n){
var s=(''+n).split('.');
s[0]=s[0].split('').reverse().join('').match(/\d{1,3}/gi).join(',').split('').reverse().join('');
return(s.join('.'));
}
/* utility to get the value from select menu */
function gsv(n){
var n=typeof(n)=='object' && n.nodeType==1 ? n : document.getElementById(n);
return n.options[n.selectedIndex].value;
}
function calculate() {
var matrix={
flowers:{
size:'flsz',
qty:'flqt',
txt:'flpr',
ltr:'fllt',
values:{50:175,200:599,500:1299,1000:2399,5000:10999}
},
cereals:{
size:'cesz',
qty:'ceqt',
txt:'cepr',
ltr:'celt',
values:{50:199,200:699,500:1599,1000:2899,5000:12999},
},
vegetables:{
size:'vesz',
qty:'veqt',
txt:'vepr',
ltr:'velt',
values:{50:225,200:799,500:1799,1000:3399,5000:14999}
},
fruits:{
size:'frsz',
qty:'frqt',
txt:'frpr',
ltr:'frlt',
values:{50:249,200:899,500:1999,1000:3699,5000:16999}
},
plantations:{
size:'plsz',
qty:'plqt',
txt:'plpr',
ltr:'pllt',
values:{50:275,200:999,500:2199,1000:3999,5000:17999}
}
};
var totals={total:0};
for( var glitch in matrix ){
var keys=Object.keys( matrix[ glitch ] );
var size=gsv( matrix[ glitch ].size );
var qty=gsv( matrix[ glitch ].qty );
var value=matrix[ glitch ].values[ size ];
var price = parseFloat( Math.round( value * 100 ) / 100 ).toFixed(2);
var litres = parseFloat( Math.round( ( ( size * qty ) / 1000 ) * 100 ) / 100 ).toFixed(3);
totals[ glitch ]={ litres:litres, price:price, mrp:'MRP '+price.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",") };
totals.total += qty * price;
document.getElementById( matrix[ glitch ].txt ).value=totals[ glitch ].mrp;
document.getElementById( matrix[ glitch ].ltr ).value=litres;
}
totals.total=parseFloat( Math.round( totals.total * 100 ) / 100 ).toFixed(2);
totals.totalmrp = 'MRP ' + totals.total.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
document.getElementById('tamt').value=totals.totalmrp;
/* do something with result ? */
return totals;
}
您可能希望更改表单,以便在实际提交表单时将操作设置为正确的php脚本。在提交之前通过javascript更改操作。
<form autocomplete="off" class="form" name="form" method="post" action="order.html" onsubmit="return submitform(event)">
<!-- lots of other form content -->
<input type='submit' value='SUBMIT' tabindex='29' class='button' id='subform' name='subform'>
</form>
如果函数submitform
返回true
function submitform(event){
/* example only - add own checks */
var checks_ok=false;
var form=typeof(event.target)!='undefined' ? event.target : event.srcElement;
/* Perform various checks on form fields to ensure fields are not empty, or contain invalid values etc */
/* if ok, set checks_ok to true */
checks_ok=true;
if( checks_ok ){
form.setAttribute('action','/php/order-form.php');
return true;
}
return false;
}
我的版本:
function submitform(event){
var checks_ok=false;
var flq=document.getElementById("flqt").value;
var ceq=document.getElementById("ceqt").value;
var veq=document.getElementById("veqt").value;
var frq=document.getElementById("frqt").value;
var plq=document.getElementById("plqt").value;
var quantity=flq+ceq+veq+frq+plq;
if(quantity==0){
alert("Please select quantity");
checks_ok=false;
return false;
}
if(checks_ok=true){
form.setAttribute('action','http://www.drearth.in/php/order-form-test.php');
return true;
}
}
答案 1 :(得分:0)
请使用循环打印项目明智列表,否则它不是一个好方法。 你只需编写一个通用函数来计算你的MRP。
<script>
function calculate(item,itmval,MRPDOMID){
item.value = itmval;
Price = parseFloat(Math.round(899 * 100) / 100).toFixed(2);
MRPDOMID.value = "MRP"+" "+Price;
}
</script>