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.