2015-10-18 76 views
1

我正在嘗試爲客戶創建一個訂單,允許他的幾個客戶訂購部件,表格計算總數並將其發送給他。在查看所有我想要的內容後,在另一個網站上瀏覽了一個教程。但是,在對代碼進行一些更改並將其放置在我的服務器上進行測試之後,它無法正常工作。我沒有對代碼做任何特殊的處理,因爲我想在添加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"; 

} 

?> 

回答

0

你已經說過,你可能會在你的頭上,所以我會盡力和給你一些方向在這裏...

不要誤會我的意思,我們都在這裏幫助,但是有一些問題與你提供和詢問:

您還沒有確定一個問題。它不起作用的原因是因爲有很多錯誤。任何人都可以給你的唯一答案是基本上重新寫所有。

StackOverflow的想法是提出問題的具體問題,像'所有這些都不起作用'的問題是不是真的可以回答。

我可以給你一些指向一些錯誤的指針,如果你可以相應地改變代碼,我肯定有人(包括我自己)能夠提供幫助。但如果你不明白需要什麼,那麼其他一些幫助可能會更好。

所以,在這裏去了幾點意見:

重複相同的代碼來照此計算爲每一個需要它的項目。

代碼重用在這種情況下是巨大的。你已經在同一個函數中重複了同樣的計算3個項目,但是你提到最終有66個項目。你真的想要再次寫這些檢查,驗證等63次嗎?

創建一個計算,格式化或驗證作爲參數傳入的數據的函數。不要重複該代碼。這使得代碼更短,更易於閱讀,更易於調試和更改。

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

無論如何,這不是你應該如何訪問DOM。更好的是:函數中的document.getElementById(elementId)。

你正在重新聲明你的變量在相同的功能;聲明一次後,您只需設置其當前值。

您正在向HTML中的函數傳遞錯誤,無效或不存在的項目。

將'document.ofrm'從輸入傳遞給validNum函數;只需發送'this'(validNum(this))。然後,您可以獲取觸發onchange事件的單個輸入的值。

當我意識到這是一個相當重新設計和重新編碼的時候,我得到了這裏。希望這可以幫助...

+0

我很欣賞你的坦率。我正在嘗試在線學習教程,以幫助教我如何創建一個表單,以創建可以通過電子郵件發送給所有者的訂單。我會重新開始。謝謝。 –

+0

沒問題,我們都是每天都在學習。當你需要更具體的幫助時,我們仍然在這裏。 –