doufen1890 2018-12-04 18:23
浏览 46
已采纳

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

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

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

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. 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.

  • 写回答

1条回答 默认 最新

  • doujianglin6704 2018-12-04 18:38
    关注

    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. As a solution, just add a hidden field for your second form:

    <input type='hidden' name='showPF' value='New Player'>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示