dongyao8698
2017-09-03 01:18
浏览 19
已采纳

坚持PHP If语句

Struggling to code what should be a fairly simple if/else-type statement in PHP. We have a lengthy web form that collects a sport's teams roster (with name, grade and date of birth for each youth player) and then sends that data back to the league's commissioner to be archived for future reference.

I set this form up more than a year ago and it's been working just fine. However, I'd like to tweak it a bit to verify that all of the fields have been completed before executing the script (in case of user error). Here is the code I'm trying to make work:

//Validate player 1:
if (!empty($_POST['player1'])) {
    $player1 = $_POST['player1'];
    $player1_dob = $_POST['player1_dob'];
    $player1_grade = $_POST['player1_grade'];
} elseif (!empty($_POST['player1_dob'])) {
    echo "You forgot to enter Player 1's date of birth.<br>";
    $validate = 'false';
} elseif (!empty($_POST['player1_grade'])) {
    echo "You forgot to enter Player 1's grade.<br>";
    $validate = 'false';
} else {
    $validate = 'true';
}

This seems like the most logical way (to me) to code the statement. However, the script is not verifying the date of birth and grade as intended. Clearly, I am missing an important concept and not using the if/elseif correctly. Perhaps a kind user can help point me in the right direction?

图片转代码服务由CSDN问答提供 功能建议

在PHP中编写一个相当简单的if / else-type语句的代码。 我们有一个冗长的网络表格,收集一个运动队的名单(每个青年球员的姓名,成绩和出生日期),然后将这些数据发送给联盟的专员进行存档以供将来参考。 \ n

我在一年多前设置了这个表格并且工作得很好。 但是,我想稍微调整它以验证在执行脚本之前所有字段都已完成(如果出现用户错误)。 这是我正在尝试的代码:

  //验证播放器1:
if(!empty($ _ POST ['player1'])){
  $ player1 = $ _POST ['player1']; 
 $ player1_dob = $ _POST ['player1_dob']; 
 $ player1_grade = $ _POST ['player1_grade']; 
} elseif(!empty($ _ POST ['player1_dob  '))){
 echo“你忘了输入玩家1的出生日期。&lt; br&gt;”; 
 $ validate ='false'; 
} elseif(!empty($ _ POST ['player1_grade'])  ){
 echo“你忘了输入玩家1的等级。&lt; br&gt;”; 
 $ validate ='false'; 
} else {
 $ validate ='true'; 
} 
   
 
 

这似乎是对我来说最合乎逻辑的方式(对我来说)。 但是,该脚本未按预期验证出生日期和等级。 很明显,我错过了一个重要的概念而没有正确使用if / elseif。 也许一位善良的用户可以帮助我指出正确的方向?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drs3925 2017-09-03 01:27
    已采纳

    The empty vs. !empty is part of it. But also you need to check the pieces BEFORE you say "everything OK". Essentially flip it:

    //Validate player 1:
    if (!empty($_POST['player1'])) {
        if (empty($_POST['player1_dob'])) {
            echo "You forgot to enter Player 1's date of birth.<br>";
            $validate = 'false';
        } elseif (empty($_POST['player1_grade'])) {
            echo "You forgot to enter Player 1's grade.<br>";
            $validate = 'false';
        } else {
            $player1 = $_POST['player1'];
            $player1_dob = $_POST['player1_dob'];
            $player1_grade = $_POST['player1_grade'];
            $validate = 'true';
        }
    }
    

    It isn't clear from your code what you want to do if empty(player1) - i.e., fail validation or just skip the entire section, but either one can be handled easily.

    打赏 评论
  • douleng0794 2017-09-03 01:21

    I would totally refactor what you are doing, but if you want to make it work, you have to remove ! from the elseif statments. You are saying if is not empty player 1 date of birth, error, so here I think you wanted to say if IS empty.

    打赏 评论
  • doqdcks3948 2017-09-03 01:42

    You can use also isset function

    $validate=true;
    if (!empty($_POST['player1'])) {
        $player1 = $_POST['player1'];
        $player1_dob = $_POST['player1_dob'];
        $player1_grade = $_POST['player1_grade'];
     } 
    if(!isset($player1_dob)) {
        echo "You forgot to enter Player 1's date of birth.<br>";
        $validate = 'false';
     } 
     if (!isset($player1_grade))  {
        echo "You forgot to enter Player 1's grade.<br>";
        $validate = 'false';
      }
      if($validation) {.... } 
    
    打赏 评论

相关推荐 更多相似问题