无法使用Ajax和PHP发送联系表单

时间:2017-09-05 06:50:47

标签: javascript php jquery html ajax

我的页面中有2个表单,一个用于联系页面,另一个用于注册目的。

我正在使用 - 几乎 - 两种形式都完全相同。 我正在改变的是我需要发送的字段。

我创建了我的JSBin,以防您想查看正常工作的联系表单:https://jsbin.com/fetexumune/1/edit?html,js

现在,我在这里找到了无效的寄存器表单代码:

HTML:

<form id="register-submit" method="post" action="register.php" class="register-form">
    <div>
        <input type="text" name="fname" id="fname" placeholder="First Name *" required="required"/>
    </div>
    <div>
        <input type="text" name="lname" id="lName" placeholder="Last Name *" required="required"/>
    </div>
    <div>
        <input type="email" name="email" id="email" placeholder="Email *" required="required"/>
    </div>
    <div>
        <input type="email" name="cemail" id="cemail" placeholder="Confirm Email *" required="required"/>
    </div>
    <div>
        <input type="password" name="password" id="password" placeholder="Password *" required="required"/>
    </div>

    <div>
        <input type="password" name="cpassword" id="cpassword" placeholder="Confirm Password *" required="required" />
    </div>
    <div>
        <input type="text" name="country" id="country" placeholder="Country *" required="required"/>
    </div>
    <div>
        <input type="text" name="phone" id="phone" placeholder="Phone # *" required="required"/>
    </div>
    <div>
        <div>
            <select class="selectpicker " required="required" id="area" name="area" placeholder="Select Field *">
                <option value="">Select Your Field *</option>
                <option value="IT / Software">IT / Software</option>
                <option value="Engineering">Engineering</option>
                <option value="Economics">Economics</option>
                <option value="Politics">Politics</option>
                <option value="Sports">Sports</option>
                <option value="Music">Music</option>
                <option value="Sales">Sales</option>
                <option value="Other">Other</option>
            </select>
        </div>
    </div>
    <div>
        <textarea id="details" name="message" rows="5" placeholder="Send any other detail (optional)"></textarea>
    </div>
    <hr>
    <h4>Credit Card Information:</h4>
    <hr>
    <div>
        <input type="text" name="nameoncard" id="nameoncard" placeholder="Name on Card *"  required="required"/>
    </div>
    <div>
        <input type="number" name="ccnumber" id="ccnumber" placeholder="Card Number *" required="required"/>
    </div>
    <div>
        <div>
            <label for="expiry-month">Expiration Date *</label>
            <div>
                <div>
                    <div>
                        <select name="expiry-month" id="expiry-month" required="required">
                            <option value="01">Jan (01)</option>
                            <option value="02">Feb (02)</option>
                            <option value="03">Mar (03)</option>
                            <option value="04">Apr (04)</option>
                            <option value="05">May (05)</option>
                            <option value="06">June (06)</option>
                            <option value="07">July (07)</option>
                            <option value="08">Aug (08)</option>
                            <option value="09">Sep (09)</option>
                            <option value="10">Oct (10)</option>
                            <option value="11">Nov (11)</option>
                            <option value="12">Dec (12)</option>
                        </select>
                    </div>
                    <div>
                        <select name="expiry-year" id="expiry-year" required="required">
                            <option value="17">2017</option>
                            <option value="18">2018</option>
                            <option value="19">2019</option>
                            <option value="20">2020</option>
                            <option value="21">2021</option>
                            <option value="22">2022</option>
                            <option value="23">2023</option>
                            <option value="24">2024</option>
                            <option value="25">2025</option>
                            <option value="26">2026</option>
                            <option value="27">2027</option>
                        </select>
                    </div>

                    <div>
                        <input type="number" name="ccv" id="ccv" placeholder="Card CVV: Security Code *" required="required"/>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="register">
        <hr>
        <h4>Choose The Proper Services:</h4>
        <div class="checkbox">
            <label>
                <input type="checkbox" id="checkAll" value="All Services"> All Services
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="Content Strategy" name="services" id="a1"> Content Strategy
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="ppc" name="services" id="a2"> Social Media Marketing
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="social" name="services" id="a3"> Search Engine Optimization

            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="ads" name="services" id="a4"> Advanced Web Analytics
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" value="e-marketing" name="services" id="a5"> Email Marketing
            </label>
        </div>
        <hr>
    </div>

    <div>
        <section id="pricelist">
            <div>
                <div class="row pricelist-title">
                    <div class=" text-center">
                        <h2 class="title">Web Ads Promo Plan Options</h2>
                        <p class="sub-title">We provide the best packages for all your needs.</p>
                    </div>
                </div>
                <div>
                    <div>
                        <div>
                            <ul class="price">
                                <li class="header">START UP</li>
                                <li class="grey"><label for="plan1">Choose <input type="checkbox" value="plan1" id="plan1" name="plan"></label></li>
                            </ul>
                        </div>
                    </div>
                    <div>
                        <div class="table-columns">
                            <ul class="price">
                                <li class="header bg-gold">SME</li>
                                <li class="grey"><label for="plan2">Choose <input type="checkbox" value="plan2" id="plan2" name="plan"></label></li>
                            </ul>
                        </div>
                    </div>
                    <div>
                        <div class="table-columns">
                            <ul class="price">
                                <li class="header bg-black">ENTERPRISE</li>
                                <li class="grey"><label for="plan3">Choose <input type="checkbox" value="plan3" id="plan3" name="plan"></label></li>
                            </ul>
                        </div>
                    </div>
                    <div>
                        <div class="table-columns">
                            <ul class="price">
                                <li class="header bg-green">All Ads Package</li>
                                <li class="grey"><label for="plan4">Choose <input type="checkbox" value="plan4" id="plan4" name="plan"></label></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

        </section>
    </div>
    <hr>
    <div>
        <button type="submit" name="submit" required="required">Submit</button>
    </div>
</form>

PHP:

<?php
    header('Content-type: application/json');
    $status = array(
        'status'=>'success',
        'message'=>'Thank you! One of our agents will contact you soon.'
    );

    $debug_log = "./debugginglog.txt";
    $currContent = file_get_contents($debug_log);
    file_put_contents($debug_log, "Trying to send email ... \n");

    $subject = 'Registration Form';
    $firstName = @trim(stripslashes($_POST['firstName']));
    $lastName = @trim(stripslashes($_POST['lastName']));
    $email = @trim(stripslashes($_POST['email']));
    $confirmEmail = @trim(stripslashes($_POST['confirmEmail']));
    $password = @trim(stripslashes($_POST['password']));
    $confirmPass = @trim(stripslashes($_POST['confirmPass']));
    $country = @trim(stripslashes($_POST['country']));
    $number = @trim(stripslashes($_POST['number']));
    $area = @trim(stripslashes($_POST['area']));
    $details = @trim(stripslashes($_POST['details']));
    $nameoncard = @trim(stripslashes($_POST['nameoncard']));
    $ccnumber = @trim(stripslashes($_POST['ccnumber']));
    $expirymonth = @trim(stripslashes($_POST['expirymonth']));
    $expiryyear = @trim(stripslashes($_POST['expiryyear']));
    $ccv = @trim(stripslashes($_POST['ccv']));
    $services = @trim(stripslashes(implode(', ', $_POST['services'])));
    $plan = @trim(stripslashes(implode(', ', $_POST['plan'])));

    $email_from = $email;
    $email_to = 'name@mail.domain'; //replace with your email

    $body =
        'Name: ' . $firstName . "\n\n" .
        'Last Name: ' . $lastName . "\n\n" .
        'Email: ' . $email . "\n\n" .
        'Confirm Email: ' . $confirmEmail . "\n\n" .
        'Password: ' . $password . "\n\n" .
        'Confirm Pass: ' . $confirmPass . "\n\n" .
        'Country: ' . $country . "\n\n" .
        'Number: ' . $number . "\n\n" .
        'Field: ' . $area . "\n\n" .
        'Details: ' . $details . "\n\n" .
        'Name on card: ' . $nameoncard . "\n\n" .
        'Card number: ' . $ccnumber . "\n\n" .
        'Expiration Date: ' . $expirymonth . "/" . $expiryyear . "\n\n" .
        'Security code: ' . $ccv . "\n\n" .
        'Services: ' . $services . "\n\n" .
        'Plan: ' . $plan . "\n\n";

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    $currContent = file_get_contents($debug_log);
    file_put_contents($debug_log, "Response ..." . $success . "\n");

    echo json_encode($status);

die;
?>

的JavaScript / jQuery的:

$('#register-submit').submit(function (e) {
        e.preventDefault();
        var firstName = $('#fname').val();
        var lastName = $('#lName').val();
        var email = $('#email').val();
        var confirmEmail = $('#cemail').val();
        var password = $('#password').val();
        var confirmPass = $('#cpassword').val();
        var country = $('#country').val();
        var number = $('#phone').val();
        var area = $('#area').val();
        var details = $('#details').val();
        var nameoncard = $('#nameoncard').val();
        var ccnumber = $('#ccnumber').val();
        var expirymonth = $('#expiry-month').val();
        var expiryyear = $('#expiry-year').val();
        var ccv = $('#ccv').val();
        var services = [];
        $('[name="services"]:checked').each(function (i, e) {
            services.push(e.value);
        });
        var plan = [];
        $('[name="plan"]:checked').each(function (i, e) {
            plan.push(e.value);
        });

        var data =  {
            'firstName': firstName,
            'lastName': lastName,
            'email': email,
            'confirmEmail': confirmEmail,
            'password': password,
            'confirmPass': confirmPass,
            'country': country,
            'number': number,
            'area': area,
            'details': details,
            'nameoncard': nameoncard,
            'ccnumber': ccnumber,
            'expirymonth': expirymonth,
            'expiryyear': expiryyear,
            'ccv': ccv,
            'services': services,
            'plan': plan,
        };

        $.ajax({
            type: "POST",
            url: "register.php",
            data: data,
            success: function (message) {
                var response = message;
                console.log('response top:', response);
                if (response.status === 'success') {
                    $('.register-form')[0].reset();
                    console.log('response:', response);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log('response:', textStatus);
                console.log('XMLHttpRequest:', XMLHttpRequest);
                console.log('errorThrown:', errorThrown);
            }
        });
    });

对我来说最奇怪的事情是联系表格工作正常而登记表格没有。

在ajax调用的success部分,我有一个console.log('response:', response);,它在控制台中打印出来:

  

回复:{状态:“成功”,消息:“谢谢!我们的代理商会尽快与您联系。”}

这意味着它会在response.status === 'success'时向我提供来自成功部分的回复消息。

那么,我做错了什么?

修改

我知道一个正在工作而另一个没有,是因为我正在使用我的个人电子邮件帐户进行测试。 所以我收到了联系表格的电子邮件,但我没有从注册表中获得任何内容。

1 个答案:

答案 0 :(得分:0)

添加到$ .ajax param dataType: 'json'并从表单中获取数据,您可以使用$('#register-submit').serializeArray();