计算订单表格不会给出结果

时间:2015-10-18 05:40:26

标签: javascript php html forms

我正在尝试为客户创建一个订单表单,允许他的一些客户订购部件,表单会计算总数并通过电子邮件发送给他。在仔细查看我想要的内容之后,在另一个网站上浏览了一个教程。但是,在对代码进行一些更改并将其放在我的服务器上进行测试后,它无法正常工作。我没有对代码做任何特别的事情,因为我想在添加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";

}

?>

1 个答案:

答案 0 :(得分:0)

你已经说过你可能会在你的头上,所以我会试着给你一些方向......

不要误解我的意思,我们都在这里提供帮助,但是您提供的内容存在一些问题并被问到:

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

StackOverflow的想法是向特定问题提问,而“所有这些都不起作用”之类的问题并不是真正的问题。

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

所以,这里有一些观察:

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

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

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

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

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

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

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

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

当我意识到这是一个非常重新设计和重新编码时,我已经到了这里。希望这会有所帮助...