我刚刚发现isset函数不再对我的登录和注册表单起作用。我觉得很奇怪,所以我撤消了我最近做的所有事情,看看它是否引起了它,但没有运气。如果我删除isset并替换为这个;
if($_SERVER['REQUEST_METHOD'] == "POST"){
它工作!但是我在一个页面上有两个表单,所以我需要检查提交的是哪一个。
下面是isset函数:if (isset($_POST['submit_login'])) {
提交按钮这样你就知道它有正确的名字;
<input type="submit" name="submit_login" value="Login" class="buttonClassic"/>
注册表的一个完全相同,但名称为submit_reg
。
形式:
<form action="<?php echo htmlentities('Login'); ?>" method="post" id="login">
<p class="p1">Already signed up? Log in</p><hr/>
<label for="email">Your email address </label><input type="email" required name="email" placeholder="Email" class="text" id="email">
<label for="password">Your password </label><input type="password" name="pass" required placeholder="Password" class="text" id="password">
<center><input type="submit" name="submit_login" value="Login" class="buttonClassic"/></center>
<div class="center-align-text">
<p class="p3"><a href="passreset.html">Forgotten your password?</a></p>
</div>
</form>
注册表单:
<form action="<?php echo htmlentities('Login'); ?>" method="post" id="register" >
<p class="p1">New to NBS? Sign up, it's free!</p><hr/>
<label for="reg_email">What's your email address? </label><input type="email" name="email" required placeholder="Email" class="text" id="reg_email">
<label for="reg_password">Choose a password </label><input type="password" required name="pass" placeholder="Password" class="text" id="reg_password">
<label for="reg_password2">Re-type password </label><input type="password" required name="pass2" placeholder="Re-type password" class="text" id="reg_password2">
<input type="checkbox" name="subscribed" value="subscribed" id="subscribed"><label for="subscribed">Yes, send me email updates from NewBorn Sounds. </label>
<br/>
<input type="checkbox" required="flag" name="terms" value="ticked" id="terms"><label for="terms">I agree to the <a href="Terms">terms & conditions</a>.</label>
<center><input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic"></center>
</form>
如果你还需要什么就喊!
哦,我知道我可以只是提交表单到外部PHP脚本,但我不是特别想这样做,因为我希望用户输入错误输出到同一页面。我知道我可以使用ajax,我这样做,但我试图保持javascript作为一个附加组件,而不是减少用户体验,没有js。
完整HTML:
<div id="login_form_wrapper">
<form action="Login" method="post" id="login" novalidate="novalidate">
<p class="p1">Already signed up? Log in</p><hr>
<label for="email">Your email address </label><input type="email" required="" name="email" placeholder="Email" class="text" id="email">
<label for="password">Your password </label><input type="password" name="pass" required="" placeholder="Password" class="text" id="password">
<center><input type="submit" name="submit_login" value="Login" class="buttonClassic"></center>
<div class="center-align-text">
<p class="p3"><a href="passreset.html">Forgotten your password?</a></p>
</div>
</form>
<form action="Login" method="post" id="register" novalidate="novalidate">
<p class="p1">New to NBS? Sign up, it's free!</p><hr>
<label for="reg_email">What's your email address? </label><input type="email" name="email" required="" placeholder="Email" class="text" id="reg_email">
<label for="reg_password">Choose a password </label><input type="password" required="" name="pass" placeholder="Password" class="text" id="reg_password">
<label for="reg_password2">Re-type password </label><input type="password" required="" name="pass2" placeholder="Re-type password" class="text" id="reg_password2">
<input type="checkbox" name="subscribed" value="subscribed" id="subscribed"><label for="subscribed">Yes, send me email updates from NewBorn Sounds. </label>
<br>
<input type="checkbox" required="flag" name="terms" value="ticked" id="terms"><label for="terms">I agree to the <a href="Terms">terms & conditions</a>.</label>
<center><input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic"></center>
</form>
</div>
也许你可以这样做:
if($_SERVER['REQUEST_METHOD'] == "POST"){
if (isset($_POST['submit_login'])) {
//do something
} else {
//do something else
}
}
表单操作需要是一个有效的url,例如:"/login"。
复选框要么有给定的值(选中后),要么根本不出现。isset($_POST['mycheckbox']) &&"价值"= = $ _POST [' mycheckbox ']"。
向我们展示用于计算表单的php。
这是怎么回事?
<input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic">
<?php
if($_POST['submit'] == 'Sign Up'){
//do something
}
?>