【问题标题】:PHP isset function for submit check not working用于提交检查的 PHP isset 函数不起作用
【发布时间】:2013-05-28 05:58:11
【问题描述】:

我刚刚发现 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>

【问题讨论】:

  • 只是出于好奇,您有两个单独的非嵌套表单吗?
  • @Fabio 是的,表格是完全独立的
  • 如果您尝试一种形式,var_dump($_POST); 会打印什么?
  • @str 谢谢。它返回一个包含用户名和密码但没有提交按钮的数组,我想这是解决这个问题的关键!
  • 您的按钮似乎不在表单中

标签: php


【解决方案1】:

表单操作必须是有效的 url,例如“/login.php”。

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

向我们展示您用于评估表单的 php。

【讨论】:

    【解决方案2】:

    也许你可以这样做:

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

    【讨论】:

      【解决方案3】:

      这是怎么回事?

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多