PHP的isset函数用于提交检查不起作用

4

我刚刚发现我的登录和注册表单中,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 &amp; conditions</a>.</label>

   <center><input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic"></center>
</form> 

</div>

2
似乎你的按钮不在表单内。 - Fabio
使用任何虚拟服务器,比如WAMP或XAMP? - Vivek Sadh
@Vivek 我正在使用 Wamp。 - Jacob Windsor
@Vivek 我已经发布了form_wrapper div中所有内容的完整HTML。我真的不认为还需要其他的了。没有其他表单或任何东西。 - Jacob Windsor
@Fabio 不是这样的。我已经使用了HTML实体。我只是从浏览器中的源代码中复制了它。 - Jacob Windsor
显示剩余17条评论
3个回答

1
也许你可以像这样做:

maybe you could do something like this :

if($_SERVER['REQUEST_METHOD'] == "POST"){ 
 if (isset($_POST['submit_login'])) { 
    //do something
 } else {
   //do something else
 }

}

0

这个怎么样?

<input type="submit" name="submit_reg" value="Sign Up" class="buttonClassic">
<?php
if($_POST['submit'] == 'Sign Up'){
  //do something
}
?>

0

表单操作需要是一个有效的URL,例如“/login.php”。

复选框要么具有给定的值(一旦选中),要么根本不显示。最好再次检查它们:“isset($ _POST ['mycheckbox'])&&'value' == $ _POST ['mycheckbox']”。

展示你用于评估表单的PHP代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接