计算订单不会给出结果

计算订单不会给出结果

问题描述:

我正在尝试为客户创建一个订单,允许他的几个客户订购部件,表格计算总数并将其发送给他。在查看所有我想要的内容后,在另一个网站上浏览了一个教程。但是,在对代码进行一些更改并将其放置在我的服务器上进行测试之后,它无法正常工作。我没有对代码做任何特殊的处理,因为我想在添加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 = ".,"; 
    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 = ".,"; 
    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 = ".,"; 
    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 = "[email protected]"; 
$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"; 

} 

?> 

你已经说过,你可能会在你的头上,所以我会尽力和给你一些方向在这里...

不要误会我的意思,我们都在这里帮助,但是有一些问题与你提供和询问:

您还没有确定一个问题。它不起作用的原因是因为有很多错误。任何人都可以给你的唯一答案是基本上重新写所有。

*的想法是提出问题的具体问题,像'所有这些都不起作用'的问题是不是真的可以回答。

我可以给你一些指向一些错误的指针,如果你可以相应地改变代码,我肯定有人(包括我自己)能够提供帮助。但如果你不明白需要什么,那么其他一些帮助可能会更好。

所以,在这里去了几点意见:

重复相同的代码来照此计算为每一个需要它的项目。

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

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

错字? document.ofrm。你的意思是document.form?

无论如何,这不是你应该如何访问DOM。更好的是:函数中的document.getElementById(elementId)。

你正在重新声明你的变量在相同的功能;声明一次后,您只需设置其当前值。

您正在向HTML中的函数传递错误,无效或不存在的项目。

将'document.ofrm'从输入传递给validNum函数;只需发送'this'(validNum(this))。然后,您可以获取触发onchange事件的单个输入的值。

当我意识到这是一个相当重新设计和重新编码的时候,我得到了这里。希望这可以帮助...

+0

我很欣赏你的坦率。我正在尝试在线学习教程,以帮助教我如何创建一个表单,以创建可以通过电子邮件发送给所有者的订单。我会重新开始。谢谢。 –

+0

没问题,我们都是每天都在学习。当你需要更具体的帮助时,我们仍然在这里。 –