简体   繁体   English

如果if语句完成,PHP表单将返回

[英]PHP form is returning before if statement completes

I have a bit of code in which I want a form to appear when the New Player button is clicked. 我有一些代码需要单击“新播放器”按钮时显示一个表单。 My problem is when I click on the submit button in the form, the validation code does not run. 我的问题是,当我单击表单中的“提交”按钮时,验证代码无法运行。 I'm not sure how to force the function to retain control. 我不确定如何强制该功能保留控制权。

The original New Player button calls an external PHP script 原始的New Player按钮调用一个外部PHP脚本

echo "<form action = '', method = 'post'>";
echo "<button  name = 'showPF', type = 'submit', value = 'New Player'>New Player</button></br>";

echo "</form>";

if(isset( $_POST['showPF'])) { 


require(__DIR__.'/new_player.php');

}

The form is then shown and in turn calls a function. 然后显示该表格,并依次调用一个函数。 This function is the part that never gets executed. 此功能是永远不会执行的部分。

echo "<form action = '' method='post'>";
    echo "Player Name: <input type='text' name='playerName'></br>";
    echo "Character Name: <input type='text' name ='charName'></br>";
    echo "Class: <input type='text' name = 'class'></br>";
    echo "Race: <input type='text' name = 'race'></br>";
    echo "Level: <input type = 'int' name = 'level'></br>";
    echo "<button name='add', type = 'submit', value = 'Add Player'>Add Player</button></br>";
    echo "</form>"; 

    if(isset($_POST['add'])){
        addPlayer();
    }

The addPlayer() function looks like addPlayer()函数看起来像

function addPlayer(){
    //PLAYER NAME
    if(empty($_POST['playerName'])){
    //Player Name is required
        echo "Player Name is required";
    }
    elseif(ctype_alpha(str_replace(' ','', $_POST['playerName'])) == FALSE){
        //Player Name can only be letters and spaces
        echo "Player Name can only contain letters and spaces";
    }
    else{
        $playerName = $_POST['playerName'];
    }


    //CHARACTER NAME
    if(empty($_POST['charName'])){
        $charName = "NULL";
    }
    elseif(ctype_alpha(str_replace(' ','', $_POST['charName'])) == FALSE){
            //Character Name can only be letters and spaces
        echo "Character Name can only contain letters and spaces";
    }
    else{
        $charName = $_POST['charName'];
    }

    //CLASS

    if(empty($_POST['class'])){
        $charClass = "NULL";
    }
    elseif(ctype_alpha(str_replace(' ','', $_POST['class'])) == FALSE){
            //Class can only be letters and spaces
        echo "Class can only contain letters and spaces";
    }
    else{
        $charClass = $_POST['class'];
    }

    //RACE

    if(empty($_POST['race'])){
        $charRace = "NULL";
    }
    if(ctype_alpha(str_replace(' ','', $_POST['race'])) == FALSE){
            //Race can only be letters and spaces
        echo "Race can only contain letters and spaces";
    }
    else{
        $charRace= $_POST['race'];
    }

    //LEVEL

    if(empty($_POST['level'])){
        $charLvl = "NULL";
    }
    if(ctype_digit($_POST['level']) == FALSE){
            //Level must be a number
        echo "Level must be a number";
    }
    else{
        $charLvl= (int)$_POST['lvl'];
    }
}

Since I don't know why it's not completing the second if statement I'm not sure how to google my answer. 因为我不知道为什么它不能完成第二个if语句,所以我不确定如何用谷歌搜索我的答案。 And, I know my code isn't pretty. 而且,我知道我的代码并不漂亮。 I'm relearning after 10+ years out of school. 放学十多年后,我正在重新学习。 Unfortunately, I don't know where else to ask such a novice question. 不幸的是,我不知道还能在哪里问这样的新手问题。

Thank you for your time. 感谢您的时间。

The problem is that new_player.php is included only when you submit the showPF value, but the second form doesn't have such field. 问题是, new_player.php只有当你提交包含showPF值,但第二种形式没有这样的字段。 As a solution, just add a hidden field for your second form: 作为解决方案,只需为第二个表单添加一个隐藏字段:

<input type='hidden' name='showPF' value='New Player'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM