我的PHP信用卡表单没有识别某些输入和错误

时间:2013-04-07 21:12:45

标签: php forms credit-card

我正在使用PHP和错误检测,在Authorize.net的信用卡输入表格上关注John Conde的教程。

它完美但我决定添加输入框以输入付款金额并删除不需要的送货地址要求;

现在,当提交的表单输入不正确或为空时,它们不再变为红色,“金额”框实际上也不会识别它是空的还是已填充。错误框仍会弹出不良信用卡提交。

这是页面(减去故障排除的设计);

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>

最后,我想在div格式之外移动结帐按钮,所以我按下这样的按钮(在设计页面中,而不是上面的例子)

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

按钮有效,但它没有将值显示为我(WIP)设计页面上的标签。

2 个答案:

答案 0 :(得分:2)

此:

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

不是<button>元素的构造方式。您似乎试图更改<input />。这可能是你想要的:

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

看起来您在两个不同的表单上复制id,这是无效的。删除包装提交按钮的表单上的id,或将其更改为其他内容。

答案 1 :(得分: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应搜索errors数组的键。 (请注意,这将更改标签颜色,而不是输入框本身。如果您希望更改框本身,请将类设置代码放在框中。)

按钮在另一个答案中解决:

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

表单元素之外的HTML5。再次,不确定IE是否支持此功能。假设您的目标是支持表单属性的浏览器,则无需将其包装在表单元素btw中。

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

内部形式。