2013-04-07 24 views
0

我正在使用PHP和錯誤檢測功能,在John的Conde上使用Authorize.net的信用卡輸入表單。我的PHP信用卡表格不能識別某些輸入和錯誤

它很完美,但我決定添加輸入框以輸入付款金額並刪除不需要的送貨地址要求;

現在,當提交的表單輸入不正確或爲空時,它們不再變成紅色,「金額」框實際上也不會識別它是空的還是空的。錯誤框仍會彈出提示信用卡不良信息。

這裏是頁面(減去簡化故障排除的設計);

http://teetimelawncare.com/payment-form.php

編輯:除去非信用卡相關的代碼之類的東西的狀態,一年期滿日期,以使其更小。最底層的PHP代碼是用於當用戶錯誤地填寫表單時向用戶顯示的紅色錯誤彈出框。

我在教程的這一部分,如果有人想比較: http://community.developer.authorize.net/t5/The-Authorize-Net-Developer-Blog/Handling-Online-Payments-Part-5-Processing-Payment-and-Handling/ba-p/10768

代碼:

<?php 
    $errors = array(); 
    if ('POST' === $_SERVER['REQUEST_METHOD']) 
    { 
     $credit_card   = sanitize($_POST['credit_card']); 
     $expiration_month  = (int) sanitize($_POST['expiration_month']); 
     $expiration_year  = (int) sanitize($_POST['expiration_year']); 
     $cvv     = sanitize($_POST['cvv']); 
     $cardholder_first_name = sanitize($_POST['cardholder_first_name']); 
     $cardholder_last_name = sanitize($_POST['cardholder_last_name']); 
     $billing_address  = sanitize($_POST['billing_address']); 
     $billing_address2  = sanitize($_POST['billing_address2']); 
     $billing_city   = sanitize($_POST['billing_city']); 
     $billing_state   = sanitize($_POST['billing_state']); 
     $billing_zip   = sanitize($_POST['billing_zip']); 
     $telephone    = sanitize($_POST['telephone']); 
     $email     = sanitize($_POST['email']); 
     $account = sanitize($_POST['account']); 
     $amount = sanitize($_POST['amount']); 


     if (!validateCreditcard_number($credit_card)) 
     { 
      $errors['credit_card'] = "Please enter a valid credit card number"; 
     } 
     if (!validateCreditCardExpirationDate($expiration_month, $expiration_year)) 
     { 
      $errors['expiration_month'] = "Please enter a valid exopiration date for your credit card"; 
     } 
     if (!validateCVV($credit_card, $cvv)) 
     { 
      $errors['cvv'] = "Please enter the security code (CVV number) for your credit card"; 
     } 
     if (empty($cardholder_first_name)) 
     { 
      $errors['cardholder_first_name'] = "Please provide the card holder's first name"; 
     } 
     if (empty($cardholder_last_name)) 
     { 
      $errors['cardholder_last_name'] = "Please provide the card holder's last name"; 
     } 
     if (empty($billing_address)) 
     { 
      $errors['billing_address'] = 'Please provide your billing address.'; 
     } 
     if (empty($billing_city)) 
     { 
      $errors['billing_city'] = 'Please provide the city of your billing address.'; 
     } 
     if (empty($billing_state)) 
     { 
      $errors['billing_state'] = 'Please provide the state for your billing address.'; 
     } 
     if (!preg_match("/^\d{5}$/", $billing_zip)) 
     { 
      $errors['billing_zip'] = 'Make sure your billing zip code is 5 digits.'; 
     } 
     if (empty($telephone)) 
     { 
      $errors['telephone'] = 'Please provide a telephone number where we can reach you if necessary.'; 
     } 
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
     { 
      $errors['email'] = 'Please provide a valid email address'; 
     } 
     if (empty($account)) 
     { 
      $errors['account'] = 'Please provide the Your Customer ID Number from your billing statement.'; 
     } 
     if (empty($amount)) 
     { 
      $errors['amount'] = 'Please enter a payment amount.'; 
     } 
     // If there are no errors let's process the payment 
     if (count($errors) === 0) 
     { 
      // Format the expiration date 
      $expiration_date = sprintf("%04d-%02d", $expiration_year, $expiration_month); 

      // Include the SDK 
      require_once('./config.php'); 

      // Process the transaction using the AIM API 
      $transaction = new AuthorizeNetAIM; 
      $transaction->setSandbox(AUTHORIZENET_SANDBOX); 
      $transaction->setFields(
       array(
       'amount' => $amount, 
       'card_num' => $credit_card, 
       'exp_date' => $expiration_date, 
       'first_name' => $cardholder_first_name, 
       'last_name' => $cardholder_last_name, 
       'address' => $billing_address, 
       'city' => $billing_city, 
       'state' => $billing_state, 
       'zip' => $billing_zip, 
       'email' => $email, 
       'card_code' => $cvv, 
       'Customer ID Number' => $account, 

       ) 
      ); 
      $response = $transaction->authorizeAndCapture(); 
      if ($response->approved) 
      { 
       // Transaction approved. Collect pertinent transaction information for saving in the database. 
       $transaction_id  = $response->transaction_id; 
       $authorization_code = $response->authorization_code; 
       $avs_response  = $response->avs_response; 
       $cavv_response  = $response->cavv_response; 

       // Put everything in a database for later review and order processing 
       // How you do this depends on how your application is designed 
       // and your business needs. 

       // Once we're finished let's redirect the user to a receipt page 
       header('Location: thank-you-page.php'); 
       exit; 
      } 
      else if ($response->declined) 
      { 
       // Transaction declined. Set our error message. 
       $errors['declined'] = 'Your credit card was declined by your bank. Please try another form of payment.'; 
      } 
      else 
      { 
       // And error has occurred. Set our error message. 
       $errors['error'] = 'We encountered an error while processing your payment. Your credit card was not charged. Please try again or contact customer service to place your order.'; 

    } 
?> 
<!DOCTYPE HTML> 
<html> 
    <head> 
     <title>Payment Form</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
     <meta http-equiv="Content-Language" content="en-us"> 
     <style type="text/css"> 
      #errormessage 
      { 
       background-color: #FFE7E7; 
       border: 3px solid #CC0033; 
       color: #000000; 
       margin: 20px ; 
       padding: 10px; 
       width: 420px; 
       -moz-border-radius: 6px; 
       -webkit-border-radius: 6px; 
       border-radius: 6px; 
       -moz-box-shadow: 5px 5px 5px #ccc; 
       -webkit-box-shadow: 5px 5px 5px #ccc; 
       box-shadow: 5px 5px 5px #ccc; 
       background: -webkit-gradient(linear, 0 0, 0 bottom, from(#FFEAEA), to(#FFB3B3)); 
       background: -moz-linear-gradient(#FFEAEA, #FFB3B3); 
       background: linear-gradient(#FFEAEA, #FFB3B3); 
       float: left; 
      } 
      .labelerror 
      { 
       color: #ff0000; 
       font-weight: bold; 
      } 
      h3 { 
    font-size: 1.6em; 
    line-height: 10px; 
    padding-left: 17px; 
    padding-top: 8px; 
    -webkit-font-smoothing: antialiased;; 

} 
      #credit 
      { 
      Position: relative; 
      margin-left: 14px; 
      height:620px; 
      width:400px; 
      -webkit-border-radius: 6px; 
       border-radius: 6px; 
       -moz-box-shadow: 5px 5px 5px #ccc; 
       -webkit-box-shadow: 5px 5px 5px #ccc; 
       box-shadow: 5px 5px 5px #ccc; 
       float: left; 
      } 
      #amount1 
      { 
      margin: 5px; 
      height:620px; 
      position: relative; 
      width:400px; 
      -webkit-border-radius: 6px; 
       border-radius: 6px; 
       -moz-box-shadow: 5px 5px 5px #ccc; 
       -webkit-box-shadow: 5px 5px 5px #ccc; 
       box-shadow: 5px 5px 5px #ccc; 
       float: left; 
       } 
     </style> 
    </head> 
    <body> 

<div id="amount1"> <h3> Payment Amount</h3><p> 
       <form id="myform"> <label for="amount"<?php if (in_array('amount', $errors)) echo ' class="labelerror"'; ?>> $</label> 
       <input type="text" name="amount" id="amount" maxlength="5" value=""></form> 
      </p> <br><div id="phpdisplay"> <form action="payment-form.php" method="get" enctype="application/x-www-form-urlencoded" target="_self" id="search"> 
    <strong>Get your current balance by searching<br> your Customer ID number</strong><br>(Don't Know? Ask us on live chat or check your billing invoice):<br> <input type="text" name="term" /><br /> 
    <input type="submit" name="btn" value="Search" /> 
    </form> 


</form></div> 
<div id="credit"> 
<h3> Credit Card Information</h3> 
     <form id="myform" action="/payment-form.php" method="post"> 


<p> 
       <label for="credit_card"<?php if (in_array('credit_card', $errors)) echo ' class="labelerror"'; ?>>Credit Card Number</label> 
       <input type="text" name="credit_card" id="credit_card" autocomplete="off" maxlength="19" value=""> 
      </p> 
      <p> 
       <label for="expiration_month"<?php if (in_array('expiration_month', $errors)) echo ' class="labelerror"'; ?>>Expiration Date</label> 
       <select name="expiration_month" id="expiration_month"> 


        <option value="12">12</option> 
       </select> 
       <select name="expiration_year" id="expiration_year"> 
        <option value="0"> </option> 

        <option value="2019">2019</option> 
        <option value="2020">2020</option> 
        <option value="2021">2021</option> 
       </select> 
      </p> 
      <p> 
       <label for="cvv"<?php if (in_array('cvv', $errors)) echo ' class="labelerror"'; ?>>Security Code</label> 
       <input type="text" name="cvv" id="cvv" autocomplete="off" value="" maxlength="4"> 
      </p> 
      <p> 
       <label for="cardholder_first_name"<?php if (in_array('cardholder_first_name', $errors)) echo ' class="labelerror"'; ?>>Cardholder's First Name</label> 
       <input type="text" name="cardholder_first_name" id="cardholder_first_name" maxlength="30" value=""> 
      </p> 
      <p> 
       <label for="cardholder_last_name"<?php if (in_array('cardholder_last_name', $errors)) echo ' class="labelerror"'; ?>>Cardholder's Last Name</label> 
       <input type="text" name="cardholder_last_name" id="cardholder_last_name" maxlength="30" value=""> 
      </p> 
      <p> 
       <label for="billing_address"<?php if (in_array('billing_address', $errors)) echo ' class="labelerror"'; ?>>Billing Address</label> 
       <input type="text" name="billing_address" id="billing_address" maxlength="45" value=""> 
      </p> 
      <p> 
       <label for="billing_address2"<?php if (in_array('billing_address2', $errors)) echo ' class="labelerror"'; ?>>Suite/Apt #</label> 
       <input type="text" name="billing_address2" id="billing_address2" maxlength="45" value=""> 
      </p> 
      <p> 
       <label for="billing_city"<?php if (in_array('billing_city', $errors)) echo ' class="labelerror"'; ?>>City</label> 
       <input type="text" name="billing_city" id="billing_city" maxlength="25" value=""> 
      </p> 
      <p> 
       <label for="billing_state"<?php if (in_array('billing_state', $errors)) echo ' class="labelerror"'; ?>>State</label> 
       <select id="billing_state" name="billing_state"> 
        <option value="0"> </option> 
        <option value="AL">Alabama</option> 
        <option value="AK">Alaska</option> 
        <option value="AZ">Arizona</option> 
        <option value="AR">Arkansas</option> 


       </select> 
      </p> 
      <p> 
       <label for="billing_zip"<?php if (in_array('billing_zip', $errors)) echo ' class="labelerror"'; ?>>Zip Code</label> 
       <input type="text" name="billing_zip" id="billing_zip" maxlength="5" value=""> 
      </p> 
      <p> 
       <label for="telephone"<?php if (in_array('telephone', $errors)) echo ' class="labelerror"'; ?>>Telephone Number</label> 
       <input type="text" name="telephone" id="telephone" maxlength="20" value=""> 
      </p> 
      <p> 
       <label for="email"<?php if (in_array('email', $errors)) echo ' class="labelerror"'; ?>>Email Address</label> 
       <input type="text" name="email" id="email" maxlength="20" value=""> 
      </p> 
      <p> 
       <label for="account"<?php if (in_array('account', $errors)) echo ' class="labelerror"'; ?>>Customer ID number</label> 
       <input type="text" name="account" id="account" maxlength="6" value=""> 
      </p> 

      <p> 
       <input type="submit" value="Checkout"> 
      </p> 
     </form></div><?php 
    if (count($errors)) 
    { 
?> 
     <div id="errormessage"> 
      <h2> 
       There was an error with your submission. Please make the necessary corrections and try again. 
      </h2> 
      <ul> 
<?php 
      foreach ($errors as $error) 
      { 
?> 
       <li><?php echo $error; ?></li> 
<?php 
      } 
?> 
      </ul> 
     </div> 
<?php 
    } 
?> 
    </body> 
</html> 

最後,我想提出結帳按鈕的股利形式之外,所以我所做的像這樣的按鈕(在設計頁面中,不是上面的例子)

</form> <br> 
    <form id="myform"><p class="center"> 
       <button form="myform" input type="submit" value="Checkout"> 
      </p></form> 

該按鈕的工作原理,但它不顯示值作爲我的(WIP)設計頁面上的標籤。

+2

請僅包含必要的最小代碼..通過您提供的內容進行排序很困難。 – Daedalus 2013-04-07 21:15:05

+1

這是很多您希望我們爲您調試的代碼。 – 2013-04-07 21:15:20

+0

編輯不必要的。 – deek 2013-04-07 21:31:10

回答

2

此:

<button form="myform" input type="submit" value="Checkout"> 

不是<button>元件是如何構造的。它看起來像你試圖改變<input />。這可能是你追求的:

<button form="myform" type="submit">Checkout</button> 

它也像你在兩種不同的形式,這是無效的複製id。在包裝提交按鈕的表單上刪除id,或將其更改爲其他內容。

2

這實際上是我看來的幾個問題。由於有幾個,我可能會混淆一些東西,有人指出我是否犯了一些錯誤。

RE:「」金額「框實際上可以識別它是空的還是空的。」 -

您不能將金額分成自己的形式,並將其與其他表單元素中的其餘元素一起使用。您要發佈的所有內容必須位於相同的表單元素中。 (除非你使用html5表單屬性,但是我認爲IE還不支持這個屬性,如果我錯了,請有人糾正我的錯誤,即使這樣,如果我沒有記錯的話,你也不會添加更多的表單元素。)請參閱: Is it possible to wrap html form elements in multiple form tags?有關更多詳細信息,請參閱接受答案中的註釋。

關於不會因錯誤而更改的框。 -

<label for="billing_address2"<?php if (in_array('billing_address2', $errors)) echo ' class="labelerror"'; ?>>Suite/Apt #</label> 

也許應該是:

<label for="billing_address2"<?php if (in_array('billing_address2', array_keys($errors))) echo ' class="labelerror"'; ?>>Suite/Apt #</label> 

你的陣列上的凸起元素名稱,所以你應該in_array搜索錯誤陣的鑰匙。 (請注意,這將改變標籤的顏色,而不是輸入框本身把類設置代碼箱子上,如果你想的箱子本身發生變化。)

按鈕在另一個答案解決:

<button form="myform" type="submit">Checkout</button> 

表單元素之外的HTML5。再次,不知道IE是否支持這個。如果你的目標瀏覽器支持form屬性,那麼無需將它包裝在一個表單元素btw中。

<button type="submit">Checkout</button> 

內部形式。

+0

謝謝,今晚晚些時候我會試試....我使用按鈕成功了,但是我還沒有在IE中試過,謝謝你的支持! – deek 2013-04-07 23:14:25

+0

工作完美!我使它像我想要的功能! – deek 2013-04-08 17:11:50

相關問題