0

私は、PHP とエラー検出を使用した Authorize.net のクレジット カード入力フォームに関する John Conde のチュートリアルに従っていました。

うまくいきましたが、支払い金額を入力するための入力ボックスを追加し、不要な配送先住所の要件を削除することにしました。

送信されたフォーム入力が正しくないか空の場合、赤くならず、「金額」ボックスが実際に空か入力済みかを認識しなくなりました。不正なクレジット カードの送信に対しては、エラー ボックスが引き続き表示されます。

これがページです(トラブルシューティングを簡単にするためのデザインを除いたもの);

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

EDIT:クレジットカード以外のコードと、州や年の有効期限などを削除して、サイズを小さくしました。一番下の PHP コードは、ユーザーがフォームに間違って入力したときに表示される赤いエラー ポップアップ ボックス用です。

誰かが比較したい場合、私はチュートリアルのこの部分にいました 。代金引換/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>

最後に、チェックアウト ボタンを div フォームの外に移動したかったので、このようなボタンを作成しました (上記の例ではなく、設計されたページ内)。

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

ボタンは機能しますが、(WIP) 設計ページのラベルとして値が表示されません。

4

2 に答える 2

2

これは実際にはいくつかの質問です。いくつかあるので、混同しているかもしれませんが、明らかに間違っていると誰かが指摘してくれます。

RE: 「「金額」ボックスは実際に空か満杯かを認識します。」--

金額を独自のフォームに分割して、他のフォーム要素の残りの要素と一緒に移動させることはできません。投稿したいものはすべて同じフォーム要素にある必要があります。(html5 フォーム属性を使用しない限り、IE はまだこれをサポートしていないと思います。間違っていたら誰か訂正してください。それでも、私の記憶が正しければ、フォーム要素を追加することはないでしょう。) を参照してください。HTMLフォーム要素を複数のフォームタグでラップすることは可能ですか? 詳細については、受け入れられた回答のコメントを参照してください。

エラーで箱が変わらない件について。--

<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 属性をサポートするブラウザーをターゲットにしていると仮定すると、フォーム要素でラップする必要はありません。

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

インサイドフォーム。

于 2013-04-07T21:53:18.257 に答える
2

これ:

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

<button>要素の構築方法ではありません。を変更しようとしたようです<input />。これはおそらくあなたが求めているものです:

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

また、2 つの異なるフォームで を複製しているように見えますがid、これは無効です。id送信ボタンをラップするフォームの を削除するか、別のものに変更します。

于 2013-04-07T21:21:44.787 に答える