如何检查PHP提交的表单

时间:2018-10-21 16:37:28

标签: php html post form-submit registration

我在提交注册/登录表单时遇到问题。 我的主要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>

2 个答案:

答案 0 :(得分:3)

您只需要添加双重测试:

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>

profile.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;

}

?>

答案 1 :(得分:1)

在提交第二个表单时,$ _ POST将不包含“提交”按钮值“ bt1”,反之亦然。
最佳做法是改用隐藏字段来确定您所使用的表单。例如,在第一种表单中使用此表单:<input type="hidden" name="which_form" value="form1"/> 然后在第二个表单内<input type="hidden" name="which_form" value="form2"/>。 然后,您可以检查$ _POST ['which_form']的值以确定发布的表单。