首页 > 解决方案 > 如何检查在 PHP 中提交的表单

问题描述

我在提交注册/登录表单时遇到问题。我的主要 php 中有两种形式,如下所示:

<form role="form" action="profile.php" onsubmit="return validateForm1()" method="post" class="login-form" name="form1" id="form1">
    <div class="form-group">
        <label class="sr-only" for="form-username">Username</label>
        <input type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
    </div>
    <div class="form-group">
        <label class="sr-only" for="form-password">Password</label>
        <input type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
    </div>

    <button type="submit" class="btn" id="btn1" name="btn1">Sign in!</button>
</form>

<form role="form" action="profile.php" onsubmit="return validateForm2()" method="post" class="registration-form" name="form2" id="form2">
    <div class="form-group">
        <label class="sr-only" for="form-first-name">Username</label>
        <input type="text" name="form-first-name" placeholder="Username..." class="form-first-name form-control" id="form-username-2">
    </div>
    <div class="form-group">
        <label class="sr-only" for="form-last-name">Email</label>
        <input type="text" name="form-last-name" placeholder="Email..." class="form-last-name form-control" id="form-email">
    </div>
    <div class="form-group">
        <label class="sr-only" for="form-email">Password</label>
        <input type="password" name="form-email" placeholder="Password..." class="form-email form-control" id="form-password-2">
    </div>
    <div class="form-group">
        <label class="sr-only" for="form-about-yourself">Confirm Password</label>
        <input type="password" name="form-confirm-pass" placeholder="Confirm Password..." class="form-email form-control" id="form-password-3">
    </div>
    <button type="submit" class="btn" name="btn2" id="btn2">Sign me up!</button>
</form>

然后我有 profile.php 如下:

<?php

if (isset($_POST['btn2'])) {

$usr = $_POST['form-username-2'];
$pass = $_POST['form-password-2'];
$email = $_POST['form-email'];

echo $usr;
echo $pass;
echo $email;

}

?>

据我尝试,我无法在另一侧得到正确回显的值,没有打印任何内容,只有当我按下注册按钮时,我才试图获取这些值。我尝试了 SERVER 选项,但它适用于两个按钮,但我希​​望它适用于第二个按钮。你能帮我解决这个问题吗?非常感谢(对不起,如果我的英语不好提前......)

编辑:我为您提供了 Javascript 代码,因为我发现没有它可以工作...请告诉我 javascript 验证有什么问题...

<script>
    function validateForm1()
    {
        var name = document.forms["form1"]["form-username"].value;
        var pass = document.forms["form1"]["form-password"].value;
        var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

        if (pass.length < 7){
            alert("Please enter at least 7 character password");
            return false;
        }
        if (!format.test(pass)){
            alert("Please enter at a symbol in password");
            return false;
        }
        return true;
    }
</script>

<script>
    function validateForm2()
    {
        var name = document.forms["form2"]["form-username-2"].value;
        var mail = document.forms["form2"]["form-last-name"].value;
        var pass1 = document.forms["form2"]["form-password-2"].value;
        var pass2 = document.forms["form2"]["form-password-3"].value;

        var passformat = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
        var mailformat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if (pass1 != pass2){
            alert("Confirmation password doesn't match");
            return false;
            }
        if (pass1.length < 7){
            alert("Please enter at least 7 character password");
            return false;
        }
        if (!passformat.test(pass1)){
            alert("Please enter at a symbol in password");
            return false;
        }
        if (!mailformat.test(mail)){
            alert("Please enter a valid email");
            return false;
        }

        return true;
    }
</script>

标签: phphtmlpostform-submitregistration

解决方案


你只需要添加双重测试:

if (isset($_POST['btn1'])) {

...

} elseif(isset($_POST['btn2'])) {

...

}

尝试这个 :

$.validate({
    lang: 'en',
    modules : 'security'
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>

<form role="form" action="profile.php"  method="post" class="login-form" name="form1" id="form1">
        <div class="form-group">
            <label class="sr-only" for="form-username">Username</label>
            <input data-validation="length" data-validation-length="min4" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username">
        </div>
        <div class="form-group">
            <label class="sr-only" for="form-password">Password</label>
            <input data-validation="length" data-validation-length="min8" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password">
        </div>
    
        <button type="submit" class="btn" id="btn1" name="btn1">Sign in!</button>
    </form>
    
    <form role="form" action="profile.php"  method="post" class="registration-form" name="form2" id="form2">
        <div class="form-group">
            <label class="sr-only" for="form-username-2">Username</label>
            <input data-validation="length" data-validation-length="min4" type="text" name="form-username" placeholder="Username..." class="form-username form-control" id="form-username-2">
        </div>
        <div class="form-group">
            <label class="sr-only" for="form-email">Email</label>
            <input data-validation="email"  type="text" name="form-email" placeholder="Email..." class="form-email form-control" id="form-email">
        </div>
        <div class="form-group">
            <label class="sr-only" for="form-password-2">Password</label>
            <input data-validation="confirmation length" data-validation-length="min8" type="password" name="form-password" placeholder="Password..." class="form-password form-control" id="form-password-2">
        </div>
        <div class="form-group">
            <label class="sr-only" for="form-confirm-pass">Confirm Password</label>
            <input  type="password" name="form-password_confirmation" placeholder="Confirm Password..." class="form-password form-control" id="form-confirm-pass">
        </div>
        <button type="submit" class="btn" name="btn2" id="btn2">Sign me up!</button>
    </form>

配置文件.php

<?php

if (isset($_POST['btn1'])) {

    $username = $_POST['form-username'];
    $password = $_POST['form-password'];

    echo $username;
    echo $password;

} elseif(isset($_POST['btn2'])) {

    $username = $_POST['form-username'];
    $email = $_POST['form-email'];
    $password = $_POST['form-password'];

    echo $username;
    echo $email;
    echo $password;

}

?>

推荐阅读