如何检查在 PHP 中提交了哪个表单
How to check which form is submited in PHP
我的 Registration/LogIn 表单提交有问题。
我的主要 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>
你只需要添加双重测试:
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;
}
?>
当您提交第二个表单时,$_POST 将不包含 "submit" 按钮值 "bt1",反之亦然。
最佳做法是使用隐藏字段来确定您所在的表单。例如,在第一种表单中使用:<input type="hidden" name="which_form" value="form1"/>
然后 <input type="hidden" name="which_form" value="form2"/>
在第二种形式中。
然后您可以检查 $_POST['which_form'] 的值以确定张贴的表单。
我的 Registration/LogIn 表单提交有问题。 我的主要 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>
你只需要添加双重测试:
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;
}
?>
$_POST 将不包含 "submit" 按钮值 "bt1",反之亦然。
最佳做法是使用隐藏字段来确定您所在的表单。例如,在第一种表单中使用:<input type="hidden" name="which_form" value="form1"/>
然后 <input type="hidden" name="which_form" value="form2"/>
在第二种形式中。
然后您可以检查 $_POST['which_form'] 的值以确定张贴的表单。