gpt4 book ai didi

javascript - 计算订单没有给出结果

转载 作者:行者123 更新时间:2023-11-28 06:48:28 25 4
gpt4 key购买 nike

我正在尝试为客户创建一个订单表单,允许他的一些客户订购零件,该表单会计算总数并将其通过电子邮件发送给他。在彻底查找我想要的内容后,我浏览了另一个网站上的教程。但是,对代码进行一些更改并将其放在我的服务器上进行测试后,它不起作用。我没有对代码做任何特殊的事情,因为我想在添加 66 个项目之前对其进行测试。我真的对 PHP 或 JS 一无所知,而且可能真的超出了我的理解范围。这里的任何帮助都会非常有帮助。

这是 html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Order Form</title>
<script language="javascript">
<!--//
function dm(amount)
{
string = "" + amount;
dec = string.length - string.indexOf('.');
if (string.indexOf('.') == -1)
return string + '.00';
if (dec == 1)
return string + '00';
if (dec == 2)
return string + '0';
if (dec > 3)
return string.substring(0,string.length-dec+3);
return string;
}

function calculate()
{
QtyA = 0; QtyB = 0; QtyC = 0;
TotA = 0; TotB = 0; TotC = 0;
PrcA = 1.25; PrcB = 2.35; PrcC = 3.45;

if (document.ofrm.qtyA.value > "")
{ QtyA = document.ofrm.qtyA.value };
document.ofrm.qtyA.value = eval(QtyA);

if (document.ofrm.qtyB.value > "")
{ QtyB = document.ofrm.qtyB.value };
document.ofrm.qtyB.value = eval(QtyB);

if (document.ofrm.qtyC.value > "")
{ QtyC = document.ofrm.qtyC.value };
document.ofrm.qtyC.value = eval(QtyC);

TotA = QtyA * PrcA;
document.ofrm.totalA.value = dm(eval(TotA));

TotB = QtyB * PrcB;
document.ofrm.totalB.value = dm(eval(TotB));

TotC = QtyC * PrcC;
document.ofrm.totalC.value = dm(eval(TotC));


Totamt =
eval(TotA) +
eval(TotB) +
eval(TotC) ;

document.ofrm.GrandTotal.value = dm(eval(Totamt));

}


function validNum(theForm)
{
var checkOK = "0123456789.,";
var checkStr = theForm.qtyA.value;
var allValid = true;
var validGroups = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch == "," && decPoints != 0)
{
validGroups = false;
break;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit characters in the \"Class A quantity\" field.");
theForm.qtyA.focus();
return (false);
}

if (decPoints > 1 || !validGroups)
{
alert("Please enter a valid number in the \"Class A quantity\" field.");
theForm.qtyA.focus();
return (false);
}

var checkOK = "0123456789.,";
var checkStr = theForm.qtyB.value;
var allValid = true;
var validGroups = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch == "," && decPoints != 0)
{
validGroups = false;
break;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit characters in the \"Class B quantity\" field.");
theForm.qtyB.focus();
return (false);
}

if (decPoints > 1 || !validGroups)
{
alert("Please enter a valid number in the \"Class B quantity\" field.");
theForm.qtyA.focus();
return (false);
}

var checkOK = "0123456789.,";
var checkStr = theForm.qtyC.value;
var allValid = true;
var validGroups = true;
var decPoints = 0;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch == ".")
{
allNum += ".";
decPoints++;
}
else if (ch == "," && decPoints != 0)
{
validGroups = false;
break;
}
else if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only digit characters in the \"Class C quantity\" field.");
theForm.qtyC.focus();
return (false);
}

if (decPoints > 1 || !validGroups)
{
alert("Please enter a valid number in the \"Class C quantity\" field.");
theForm.qtyC.focus();
return (false);
}


calculate();
return (true);
}


//-->
</script>





</head>

<body>

<form method="POST" action="ordertest.php" name="Order_Form">
<p>Please tell us who you are (<font color="#FF0000">red</font> denotes
required information):</p>
<table border="0" cellpadding="0" width="550" id="table1">
<tr>
<td width="340" align="right"><font color="#FF0000">Name</font></td>
<td width="10">&nbsp;</td>
<td width="200"><input type="text" name="Name" size="30" tabindex="1"></td>
</tr>
<tr>
<td width="340" align="right"><font color="#FF0000">Email</font>
(Your confirmation will be sent here): </td>
<td width="10">&nbsp;</td>
<td width="200"><input type="text" name="Email" size="30" tabindex="1"></td>
</tr>
<tr>
<td width="340" align="right">Other Contact Info:</td>
<td width="10">&nbsp;</td>
<td width="200"><input type="text" name="OtherInfo" size="30" tabindex="1"></td>
</tr>
<tr>
<td width="340" align="right">&nbsp;</td>
<td width="10">&nbsp;</td>
<td width="200">&nbsp;</td>
</tr>
</table>
<p>And tell us what you would like:</p>
<table border="0" cellpadding="0" width="550" id="table2">
<tr>
<td width="250" height="31"><b>Item Description</b></td>
<td align="center" width="100" height="31"><b>Quantity</b></td>
<td align="right" height="31" width="60"><b>Price </b></td>
<td align="right" height="31" width="140"><b>Total</b></td>
</tr>
<tr>
<td width="250">Class &quot;A&quot; Widgets</td>
<td align="center" width="100">
<input type="text" name="qtyA" size="5" tabindex="5" onchange="return validNum(document.ofrm)"></td>
<td align="right" width="60">1.25</td>
<td align="right" width="140">
<input type="text" name="totalA" size="12" tabindex="99" onchange="calculate()"></td>
</tr>
<tr>
<td width="250">Class &quot;B&quot; Widgets</td>
<td align="center" width="100">
<input type="text" name="qtyB" size="5" tabindex="5" onchange="return validNum(document.ofrm)"></td>
<td align="right" width="60">2.35</td>
<td align="right" width="140">
<input type="text" name="totalB" size="12" tabindex="99" onchange="calculate()"></td>
</tr>
<tr>
<td width="250">Class &quot;C&quot; Widgets</td>
<td align="center" width="100">
<input type="text" name="qtyC" size="5" tabindex="5" onchange="return validNum(document.ofrm)"></td>
<td align="right" width="60">3.45</td>
<td align="right" width="140">
<input type="text" name="totalC" size="12" tabindex="99" onchange="calculate()"></td>
</tr>
<tr>
<td width="250">&nbsp;</td>
<td align="center" width="100">&nbsp;</td>
<td align="right" width="60">&nbsp;</td>
<td align="right" width="140">&nbsp;</td>
</tr>
<tr>
<td width="250">
<p align="right"><b>TOTALS:</b></p></td>
<td align="center" width="100">&nbsp;</td>
<td align="right" width="60">&nbsp;</td>
<td align="right" width="140">
<input type="text" name="GrandTotal" size="15" tabindex="99" onchange="calculate()"></td>
</tr>
<tr>
<td width="250">&nbsp;</td>
<td align="center" width="100">&nbsp;</td>
<td align="right" width="60">&nbsp;</td>
<td align="right" width="140">&nbsp;</td>
</tr>
</table>
<p>&nbsp;</p>
<table border="0" cellpadding="0" width="550" id="table3">
<tr>
<td width="563">
<p align="center">
<input type="submit" value="Submit" name="subButton" tabindex="50">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Reset" name="resetButton" tabindex="50"></p></td>
</tr>
</table>
</form>
</body>
</html>

这是 PHP 文件:

<? PHP
//uncomment for debugging
//print_r($_POST);

//make sure there is data in the name and email fields
if( empty($_POST["Name"]) )
{
$error["name"] = "Name is required.";
$Name = "";
}
else
$Name = $_POST["Name"];

if( empty($_POST["Email"]) )
{
$error["email"] = "Email is required.";
$Email = "";
}
else
$Email = $_POST["Email"];

if( empty($_POST["OtherInfo"]) )
{
$OtherInfo = "";
}
else
$OtherInfo = $_POST["OtherInfo"];



//check to make sure the qty fields are whole numbers
//but only check if there was data entered
if( !empty($_POST["qtyA"]) )
{
if( is_numeric($_POST["qtyA"]) && ( intval($_POST["qtyA"]) == floatval($_POST["qtyA"]) ) )
{
//we have a whole number
}
else
$error["qtyA"] = "Please enter a whole number for Class A Widgets.";
}

if( !empty($_POST["qtyB"]) )
{
if( is_numeric($_POST["qtyB"]) && ( intval($_POST["qtyB"]) == floatval($_POST["qtyB"]) ) )
{
//we have a whole number
}
else
$error["qtyB"] = "Please enter a whole number for Class B Widgets.";
}

if( !empty($_POST["qtyC"]) )
{
if( is_numeric($_POST["qtyC"]) && ( intval($_POST["qtyC"]) == floatval($_POST["qtyC"]) ) )
{
//we have a whole number
}
else
$error["qtyC"] = "Please enter a whole number for Class C Widgets.";
}


//we should have at least 1 item ordered in the form
if( empty($_POST["qtyA"]) && empty($_POST["qtyB"]) && empty($_POST["qtyC"]) )
$error["no_qty"] = "Please enter at least 1 item to order.";


if( is_array($error) )
{

echo "An error occurred while processing your order.";
echo "<br>\n";
echo "Please check the following error messages carefully, then click back in your browser.";
echo "<br>\n";

while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}

//stop everything as we have errors and should not continue
exit();

}
//we do not need the rest of the form fields as we can just calculate them from the whole numbers
if( !empty($_POST["qtyA"]) )
{
$qtyA = $_POST["qtyA"];
$totalA = $qtyA * 1.25;
}
else
{
$qtyA = 0;
$totalA = 0;
}

if( !empty($_POST["qtyB"]) )
{
$qtyB = $_POST["qtyB"];
$totalB = $qtyB * 2.35;
}
else
{
$qtyB = 0;
$totalB = 0;
}

if( !empty($_POST["qtyC"]) )
{
$qtyC = $_POST["qtyC"];
$totalC = $qtyC * 3.45;
}
else
{
$qtyC = 0;
$totalC = 0;
}

$GrandTotal = $totalA + $totalB + $totalC;

//we have our data, and now build up an email message to send
$mailto = "mymail@atmygmail.com";
$subject = " Online Order";

$body = "The following confirms the details of your order:\n";
$body .= "\n\n";
$body .= "Name: " . $Name . "\n";
$body .= "Email: " . $Email . "\n";
$body .= "Other Contact Info: " . $OtherInfo . "\n";
$body .= "\n\n";
$body .= "Class A Widgets: (" . $qtyA . " * 1.25) = " . $totalA . "\n";
$body .= "Class B Widgets: (" . $qtyB . " * 2.35) = " . $totalB . "\n";
$body .= "Class C Widgets: (" . $qtyC . " * 3.45) = " . $totalC . "\n";
$body .= "\n";
$body .= "TOTALS: " . $GrandTotal . "\n";

mail($mailto, $subject, $body);
mail($Email, $subject, $body);

//we should state the order was sent
echo "The following information was sent.";
echo "<br>\n";
echo "<pre>\n";
echo $body;
echo "</pre>\n";

}

?>

最佳答案

你已经说过你可能无法理解,所以我会尝试在这里给你一些指导......

不要误会我的意思,我们都在这里提供帮助,但您提供和询问的内容存在一些问题:

你还没有真正的问题。它不起作用的原因是因为有很多错误。任何人能给你的唯一答案就是基本上全部重写。

StackOverflow 的想法是针对具体问题提出问题,而诸如“这一切都不起作用”之类的问题实际上是无法回答的。

我可以为您提供一些错误提示,如果您可以相应地更改代码,我相信有人(包括我自己)能够提供帮助。但如果您无法理解需要什么,那么其他一些帮助可能会更好。

因此,这里有一些观察结果:

重复相同的代码,对可能需要的每个项目进行相同的计算。

在这种情况下,代码重用是巨大的。您在同一个函数中重复了相同的计算 3 项,但您提到最终有 66 项。您真的想再重复编写这些检查、验证等 63 次吗?

创建一个函数来计算、格式化或验证作为参数传入的数据。不要重复该代码。这使得代码更短、更容易阅读、更容易调试和更改。

打字错误?文件.ofrm。您指的是 document.form 吗?

无论如何,这不是访问 DOM 的方式。更好的是:来自函数的 document.getElementById(elementId) 。

您正在同一函数中重新声明变量;声明一次后,您只需设置它们的当前值。

您从 HTML 向函数传递了错误、无效或不存在的项。

将“document.ofrm”从输入传递到 validNum 函数;只需发送“this”(validNum(this))。然后您可以获得触发 onchange 事件的各个输入的值。

当我意识到这是一次重新设计和重新编码时,我就走到了这里。希望这有帮助...

关于javascript - 计算订单没有给出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com