【问题标题】:PHP Ajax form submitting without user interaction无需用户交互的 PHP Ajax 表单提交
【发布时间】:2021-09-23 08:50:38
【问题描述】:

不确定是否有人能够回答这个问题。

我刚刚在我的网站上创建了一个使用 jQuery 验证的 PHP Ajax 表单。所有字段都经过验证,因此它们需要内容才能提交表单。

由于某种原因,我不断收到从表单发送的电子邮件,但它们不包含任何表单信息。有谁知道为什么会这样?请参阅下面的代码和屏幕截图(我已经删除了 jQuery 验证代码以节省空间,但已经对其进行了测试并且它正在工作)-

---HTML---

<div class="col-md-6">
                    <div class="thankyou-message">
                        <p class="hero">THANK YOU!!!</p>
                        <p>For submitting your registration.</p>
                        <p>We have received your information and will contact you in due course.</p>
                    </div>
                    <div class="registration-form my-5 my-md-0">
                        <div class="row">
                            <div class="col-sm-4">
                                <label for="ageteam">Age group and team</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="age-team" name="testage" required />
                                <span class="error age-team">Please tell us your age group</span>
                            </div>
                        </div>
                        <div class="row margin-top--20 margin-bottom--16 border-bottom--orange">
                            <div class="col-12">
                                PLAYER
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="name">Name</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" class="w-100" type="text" id="name" name="name" required />
                                <span class="error name">Please tell us your name</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="address">Address</label>
                            </div>
                            <div class="col-sm-8">
                                <textarea class="w-100" id="address" name="address" rows="4" required></textarea>
                                <span class="error address">Please tell us your address</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="postcode">Postcode</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="postcode" name="postcode" required />
                                <span class="error postcode">Please tell us your postcode</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="dob">Date of birth</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="dob" name="dob" required />
                                <span class="error dob">Please tell us your date of birth</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="school">School</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="school" name="school" required />
                                <span class="error school">Please tell us the name of your school</span>
                            </div>
                        </div>
                        <div class="row margin-top--20 margin-bottom--16 border-bottom--orange">
                            <div class="col-12">
                                PARENT OR GUARDIAN
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="parent-name">Name</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="parent-name" name="parent-name" required />
                                <span class="error parent-name">Please tell us the name of your parent or guardian</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="parent-address">Address</label>
                            </div>
                            <div class="col-sm-8">
                                <textarea class="w-100" id="parent-address" name="parent-address" rows="4" required></textarea>
                                <span class="error parent-address">Please tell us your parent or guardians address</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="parent-postcode">Postcode</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="parent-postcode" name="parent-postcode" required />
                                <span class="error parent-postcode">Please tell us the postcode of your parent or guardian</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="phone">Phone</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="phone" name="phone" required />
                                <span class="error number">Please tell us your phone number</span>
                                <span class="error number2">Please enter a valid phone number</span>
                            </div>
                        </div>
                        <div class="row margin-bottom--8">
                            <div class="col-sm-4">
                                <label for="email">Email</label>
                            </div>
                            <div class="col-sm-8">
                                <input class="w-100" type="text" id="email" name="testemail" required />
                                <span class="error email">Please tell us your email address</span>
                                <span class="error email2">Please enter a valid email address</span>
                            </div>
                        </div>
                        <div class="submit-btn">
                            Submit registration
                        </div>
                    </div>
                </div>

---jQuery提交代码---

$('div.submit-btn').click(function() {

        if (ageTeamValidate === true && nameValidate === true && addressValidate === true && postcodeValidate === true && dobValidate === true && schoolValidate === true && parentNameValidate === true && parentAddressValidate === true && parentPostcodeValidate === true && numberValidate === true && emailValidate === true) {
            console.log("form submitted");

            var varData = 'ageTeam= ' + ageTeam + ' & name= ' + name + ' & address= ' + address + ' & postcode= ' + postcode + ' & dob= ' + dob + ' & school= ' + school + ' & parentName= ' + parentName + ' & parentAddress= ' + parentAddress + ' & parentPostcode= ' + parentPostcode + ' & number= ' + number + ' & email= ' + email;
            console.log(varData);

            $.ajax({
                type: "POST",
                url:'form-process.php',
                data: varData,
                cache: false,
                success: function() {
                    //alert("Post successful");
                    $('div.registration-form').css('display', 'none');
                    $('div.thankyou-message').css('display', 'block');
                }
            });
        }

        return false;

    });

--- PHP表单处理代码---

<?php

$ageTeam = $_POST['ageTeam'];
$name = $_POST['name'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$dob = $_POST['dob'];
$school = $_POST['school'];
$parentName = $_POST['parentName'];
$parentAddress = $_POST['parentAddress'];
$parentPostcode = $_POST['parentPostcode'];
$number = $_POST['number'];
$email = $_POST['email'];

$email_thankyou = "
Hi".$name."

Thank you for registering.

We look forward to welcoming you to our team and will contact you in due course.

Thanks again
";

$email_alert = "
".$name."has completed a registration form.

Their details are listed below:

Age and team:".$ageTeam."
Players full name:".$name."
Players address:".$address."
Players postcode:".$postcode."
Players date of birth:".$dob."
Players school:".$school."
Parent/guardians name:".$parentName."
Parent/guardians address:".$parentAddress."
Parent/guardians postcode:".$parentPostcode."
Contact number:".$number."
Email Address:".$email."
";

$headers = "From: Test PHP Ajax Form";

// Notification email to client
mail($email, 'Thank you for registering', $email_thankyou, $headers);

// Notification email to Matthiola
mail('testemail@email.com', 'Player Registration', $email_alert, $headers);

?>

--- 收到电子邮件 ---

收到的电子邮件显示静态内容,但没有用户应提供的信息。

【问题讨论】:

  • 例如,自动化机器人可以轻松地直接向您的 PHP 发送 POST 请求,而无需实际使用表单 - 这当然意味着验证将不适用。或者,如果有人只是在他们的浏览器中禁用 JavaScript,它可能会产生类似的效果。这是您应该始终主要使用服务器端验证的一个很好的理由,如果它是公共表单,还可能使用验证码。客户端验证只是一个不错的选择,它永远不能成为你唯一的防御——它太容易绕过了。你很幸运,你得到的只是空数据,而不是各种“有趣”的内容......

标签: php jquery ajax


【解决方案1】:

这很可能是因为漫游器和网络爬虫正在浏览您的网站。您应该在发送电子邮件之前验证来自服务器端的输入(使用 PHP)。

if ($name != '' && $address != ''){
    // Notification email to client
    mail($email, 'Thank you for registering', $email_thankyou, $headers);

    // Notification email to Matthiola
    mail('testemail@email.com', 'Player Registration', $email_alert, $headers);
}

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多