0

好的,到目前为止,我已经从你们那里得到了一些帮助,这是我花太多时间做的最后一件事。

我正在将 nopCart 用于 uni 的在线商店。我们不需要做后端工作,但我一直将客户详细信息保存到文本文件中。最初,我能够通过电子邮件发送所有内容,但由于通过 PHP,我不知道如何获取订单详细信息。客户信息被输入到文本框中,然后通过 PHP 保存到文本文件中。

我的结帐代码

        <form action="process.php" method ="post">
            <script type="text/javascript">
                CheckoutCart();
            </script>
            <br /><br />
            First Name:     <input type="text" name="first" /><br />
            Last Name:      <input type="text" name="last" /><br />
            Street Address: <input type="text" name="address" /><br />
            Suburb:         <input type="text" name="suburb" /><br />
            State:          <input type="text" name="state" /><br />
            Postcode:       <input type="text" name="postcode" /><br />
            Country:        <input type="text" name="country" /><br />
            Phone Number:   <input type="text" name="phone" /><br />
            Email:          <input type="text" name="email" /><br />

            shop:           <input type="text" name="shop" /><br />
            <br /><br />
            Other form data can go here when you create your prototype cart ...
            <br /><br />

            <input type="submit" name="submitButton" value=" Submit Order " />
        </form>

当前未使用输入商店,我试图将订单信息输入其中

我的 process.php 代码

<?php
// We will put the data into a file in the current directory called "data.txt"
// But first of all, we need to check if the user actually pushed submit
if (isset($_POST['submitButton'])) {
// The user clicked submit
// Put the contents of the text into the file
file_put_contents('./data.txt', $_POST['shop'] . " " .$_POST['first'] . " " . $_POST['last'] . "\n" . $_POST['address'] . "\n" . $_POST['suburb'] . " " . $_POST['state'] . " " . $_POST['postcode'] . "\n" . $_POST['country'] . "\n" . "\n", FILE_APPEND);
// ./data.txt: the text file in which the data will be stored
// $_POST['myInputName']: What the user put in the form field named "myInputName"
// FILE_APPEND: This tells the function to append to the file and not to overwrite it.
}
?>

在 checkout.html 中使用此代码的早期版本中。这将通过电子邮件向我发送我需要的一切

<!-- Checkout Begin-->
            The items listed below are currently in your shopping cart:

            <form action="mailto:xxxxxxx@example.com" method ="post">
                <script type="text/javascript">
                    CheckoutCart();
                </script>
                <br /><br />
                Name:   <input type="text" name="b_first" />
                        <input type="text" name="b_last" /><br />
                Email:  <input type="text" name="b_email" /><br />
                <br /><br />
                Other form data can go here when you create your prototype cart ...
                <br /><br />

                <input type="submit" value=" Submit Order " />
            </form>
            <!-- Checkout End -->

单击发送按钮后,我会在新电子邮件中打开此文本

ID_1=ID+000&QUANTITY_1=1&PRICE_1=28.99&NAME_1=Cat+Scratcher&SHIPPING_1=4.99&ADDTLINFO_1=&SUBTOTAL=%2428.99&SHIPPING=%244.99&TAX=%240.00&TOTAL=%2433.98&b_first=Jonny&blast_a%40=Smith&b_email=Smith&b_email

我希望能够在此处将此字符串与客户详细信息一起放入 .txt 文件中。有没有一种简单的方法可以从 CheckoutCart() 获取此信息并将其保存在文本框中,以便我可以将其添加到我的 process.php 并将其放入文本文件中?

到目前为止,我一直在反复试验,我真的不知道下一步该怎么做

这是 nopcart.js 中的 checkoutCart 函数

//---------------------------------------------------------------------||
// FUNCTION:    CheckoutCart                                           ||
// PARAMETERS:  Null                                                   ||
// RETURNS:     Product Table Written to Document                      ||
// PURPOSE:     Draws current cart product table on HTML page for      ||
//              checkout.                                              ||
//---------------------------------------------------------------------||
function CheckoutCart( ) {
   var iNumberOrdered = 0;    //Number of products ordered
   var fTotal         = 0;    //Total cost of order
   var fTax           = 0;    //Tax amount
   var fShipping      = 0;    //Shipping amount
   var strTotal       = "";   //Total cost formatted as money
   var strTax         = "";   //Total tax formatted as money
   var strShipping    = "";   //Total shipping formatted as money
   var strOutput      = "";   //String to be written to page
   var bDisplay       = true; //Whether to write string to the page (here for programmers)
   var strPP          = "";   //Payment Processor Description Field

   iNumberOrdered = GetCookie("NumberOrdered");
   if ( iNumberOrdered == null )
      iNumberOrdered = 0;

   if ( TaxByRegion ) {
      QueryString_Parse();
      fTax = parseFloat( QueryString( OutputOrderTax ) );
      strTax = moneyFormat(fTax);
   }

   if ( bDisplay )
      strOutput = "<TABLE CLASS=\"nopcart\"><TR>" +
                  "<TD CLASS=\"nopheader\"><B>"+strILabel+"</B></TD>" +
                  "<TD CLASS=\"nopheader\"><B>"+strDLabel+"</B></TD>" +
                  "<TD CLASS=\"nopheader\"><B>"+strQLabel+"</B></TD>" +
                  "<TD CLASS=\"nopheader\"><B>"+strPLabel+"</B></TD>" +
                  (DisplayShippingColumn?"<TD CLASS=\"nopheader\"><B>"+strSLabel+"</B></TD>":"") +
                  "</TR>";

   for ( i = 1; i <= iNumberOrdered; i++ ) {
      NewOrder = "Order." + i;
      database = "";
      database = GetCookie(NewOrder);

      Token0 = database.indexOf("|", 0);
      Token1 = database.indexOf("|", Token0+1);
      Token2 = database.indexOf("|", Token1+1);
      Token3 = database.indexOf("|", Token2+1);
      Token4 = database.indexOf("|", Token3+1);

      fields = new Array;
      fields[0] = database.substring( 0, Token0 );                 // Product ID
      fields[1] = database.substring( Token0+1, Token1 );          // Quantity
      fields[2] = database.substring( Token1+1, Token2 );          // Price
      fields[3] = database.substring( Token2+1, Token3 );          // Product Name/Description
      fields[4] = database.substring( Token3+1, Token4 );          // Shipping Cost
      fields[5] = database.substring( Token4+1, database.length ); //Additional Information

      fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
      fShipping  += (parseInt(fields[1]) * parseFloat(fields[4]) );
      if ( !TaxByRegion ) fTax = (fTotal * TaxRate);
      strTotal    = moneyFormat(fTotal);
      if ( !TaxByRegion ) strTax = moneyFormat(fTax);
      strShipping = moneyFormat(fShipping);

      if ( bDisplay ) {
         strOutput += "<TR><TD CLASS=\"nopentry\">"  + fields[0] + "</TD>";

         if ( fields[5] == "" )
            strOutput += "<TD CLASS=\"nopentry\">"  + fields[3] + "</TD>";
         else
            strOutput += "<TD CLASS=\"nopentry\">"  + fields[3] + " - <I>"+ fields[5] + "</I></TD>";

         strOutput += "<TD CLASS=\"nopentry\">" + fields[1] + "</TD>";
         strOutput += "<TD CLASS=\"nopentry\">"+ MonetarySymbol + moneyFormat(fields[2]) + "/ea</TD>";

         if ( DisplayShippingColumn ) {
            if ( parseFloat(fields[4]) > 0 )
               strOutput += "<TD CLASS=\"nopentry\">"+ MonetarySymbol + moneyFormat(fields[4]) + "/ea</TD>";
            else
               strOutput += "<TD CLASS=\"nopentry\">N/A</TD>";
         }

         strOutput += "</TR>";
      }

      if ( AppendItemNumToOutput ) {
         strFooter = i;
      } else {
         strFooter = "";
      }
      if ( PaymentProcessor != '' ) {
         //Process description field for payment processors instead of hidden values.
         //Format Description of product as:
         // ID, Name, Qty X
         strPP += fields[0] + ", " + fields[3];
         if ( fields[5] != "" )
            strPP += " - " + fields[5];
         strPP += ", Qty. " + fields[1] + "\n";
      } else {
         strOutput += "<input type=hidden name=\"" + OutputItemId        + strFooter + "\" value=\"" + fields[0] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemQuantity  + strFooter + "\" value=\"" + fields[1] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemPrice     + strFooter + "\" value=\"" + fields[2] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemName      + strFooter + "\" value=\"" + fields[3] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemShipping  + strFooter + "\" value=\"" + fields[4] + "\">";
         strOutput += "<input type=hidden name=\"" + OutputItemAddtlInfo + strFooter + "\" value=\"" + fields[5] + "\">";
      } 

   }

   if ( bDisplay ) {
      strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strSUB+"</B></TD>";
      strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strTotal + "</B></TD>";
      strOutput += "</TR>";

      if ( DisplayShippingRow ) {
         strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strSHIP+"</B></TD>";
         strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strShipping + "</B></TD>";
         strOutput += "</TR>";
      }

      if ( DisplayTaxRow || TaxByRegion ) {
         strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strTAX+"</B></TD>";
         strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + strTax + "</B></TD>";
         strOutput += "</TR>";
      }

      strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>"+strTOT+"</B></TD>";
      strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2 ALIGN=RIGHT><B>" + MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "</B></TD>";
      strOutput += "</TR>";

      strOutput += "</TABLE>";


      if ( PaymentProcessor == 'an') {
         //Process this for Authorize.net WebConnect
         strOutput += "<input type=hidden name=\"x_Version\" value=\"3.0\">";
         strOutput += "<input type=hidden name=\"x_Show_Form\" value=\"PAYMENT_FORM\">";
         strOutput += "<input type=hidden name=\"x_Description\" value=\""+ strPP + "\">";
         strOutput += "<input type=hidden name=\"x_Amount\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\">";
      } else if ( PaymentProcessor == 'wp') {
         //Process this for WorldPay
         strOutput += "<input type=hidden name=\"desc\" value=\""+ strPP + "\">";
         strOutput += "<input type=hidden name=\"amount\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\">";
      } else if ( PaymentProcessor == 'lp') {
         //Process this for LinkPoint         
         strOutput += "<input type=hidden name=\"mode\" value=\"fullpay\">";
         strOutput += "<input type=hidden name=\"chargetotal\" value=\""+ moneyFormat((fTotal + fShipping + fTax)) + "\">";
         strOutput += "<input type=hidden name=\"tax\" value=\""+ MonetarySymbol + strTax + "\">";
         strOutput += "<input type=hidden name=\"subtotal\" value=\""+ MonetarySymbol + strTotal + "\">";
         strOutput += "<input type=hidden name=\"shipping\" value=\""+ MonetarySymbol + strShipping + "\">";
         strOutput += "<input type=hidden name=\"desc\" value=\""+ strPP + "\">";
      } else {
         strOutput += "<input type=hidden name=\""+OutputOrderSubtotal+"\" value=\""+ MonetarySymbol + strTotal + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderShipping+"\" value=\""+ MonetarySymbol + strShipping + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderTax+"\"      value=\""+ MonetarySymbol + strTax + "\">";
         strOutput += "<input type=hidden name=\""+OutputOrderTotal+"\"    value=\""+ MonetarySymbol + moneyFormat((fTotal + fShipping + fTax)) + "\">";
      }
   }

   document.write(strOutput);
   document.close();
}

//=====================================================================||
//               END NOP Design SmartPost Shopping Cart                ||
//=====================================================================||

我知道它有很多文本和代码,但我认为找到解决方案不会太难,它只是我课程中第一个深入的 HTML 主题,我希望能够让它发挥作用。

4

1 回答 1

0

你可以做类似的事情

file_put_contents('data.txt', var_export($_POST, true), FILE_APPEND);

这会将 $_POST 参数中的所有字段保存到文件中。

var_export($_POST, true) 的替代方法是 json_encode($_POST) 可以稍后由 Javascript 读取,或者您可以格式化一些人类可读的文本。

例如把它放在你的process.php.

<?php
// submitButton was sent.
if(isset($_POST['submitButton'])) {
    // Build a string with all fields that was submitted.

    $str = "--- BEGIN ORDER ---\n"
    // For each field in the $_POST variable, iterate and add them to the string.
    foreach($_POST as $k => $v) {
        $str .= "$k: $v\n";
    }
    $str .= "--- END ORDER ---\n";

    // Show the user the result
    echo $str;

    // Also save it to data.txt
    file_put_contents('data.txt', $str, FILE_APPEND);
} else {
    // submitButton was not sent, show some error.
    echo "Please submit something";
}
于 2013-05-30T14:04:31.957 回答