max_power Posted August 4, 2009 Share Posted August 4, 2009 Hi, I am wondering how would I go about checking if a name was already entered in a html form and has been submitted? No mysql query or anything. here is the form: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>PHP footy form</title> </head> <body> <h1>Footy Result Analyser</h1> <p>________________________________________________________</p> <p>Please enter the team and player results in the form below.</p> <p>Team 1 (Home): <form method="post" action="FootyForm_P.php"> <select name="team1" id="team1"> <option value="1" selected="selected">Adelaide Crows</option> <option value="2">Brisbane Lions</option> <option value="3">Carlton</option> <option value="4">Collingwood</option> <option value="5">Essendon</option> <option value="6">Fremantle</option> <option value="7">Geelong</option> <option value="8">Hawthorn</option> <option value="9">Melbourne</option> <option value="10">North Melbourne</option> <option value="11">Port Adelaide</option> <option value="12">Richmond</option> <option value="13">Saint Kilda</option> <option value="14">Sydney Swans</option> <option value="15">West Coast Eagles</option> <option value="16">Western Bulldogs</option> </select> </p> <table width="600" border="1"> <tr> <td><div align="center">Player Name </div></td> <td><div align="center">Goals</div></td> <td><div align="center">Points</div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_1_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_1_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_1_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_2_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_2_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_2_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_3_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_3_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_3_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_4_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_4_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_4_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_5_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_5_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_5_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_6_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_6_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_6_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_7_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_7_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_7_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_8_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_8_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_8_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_9_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_9_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_9_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_10_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_10_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_10_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_11_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_11_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_11_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_12_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_12_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_12_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_13_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_13_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_13_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_14_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_14_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_14_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_15_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_15_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_15_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_16_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_16_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_16_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_17_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_17_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_17_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_18_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_18_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_18_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_19_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_19_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_19_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_20_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_20_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_20_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_21_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_21_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_21_points" type="text" size="2" maxlength="2" /> </div></td> </tr> <tr> <td><div align="center"> <input name="team_1_player_22_name" type="text" size="40" maxlength="40" /> </div></td> <td><div align="center"> <input name="team_1_player_22_goals" type="text" size="2" maxlength="2" /> </div></td> <td><div align="center"> <input name="team_1_player_22_points" type="text" size="2" maxlength="2" /> </div></td> </tr> </table> <p> </p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> </body> </html> Here's what I got so far in my php code for it but its just plain stupid: function playerNameTeam1() { //this begins the loop for ($i = 1; $i <= 22; $i++) { if ($_POST['team_1_player_' . $i.'_name'] == "") { //error message echo "Enter <b>player name " .$i. "</b> for <b>".teams($team1)."</b> on line <b>" .$i ."</b><br /><br />"; } else { // Check whether the player name already exists. if ($_POST['team_1_player_' . $i.'_name'] == $_POST['team_1_player_' . $i.'_name']) { //error message echo "The <b>player name " .$_POST['team_1_player_' . $i.'_name']. "</b> for <b>".teams($team1)."</b> on line <b>" .$i ." already exists.</b><br /><br />"; } else { //Perform no action. } } } } What's it doing is that first it checks if the name has been entered then it looks for a similar name..and obviously it doesnt work. Thanks Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Well you have to store the names in something, like a CSV. e.g. names.txt james,bob,chris,jen <?php $f= fopen("names.txt","r"); $r= fread($f,1024); $names = explode(",",$r); $submitted_name = $_POST['name']; if(in_array($submitted_name,$names)){ echo 'That name is already used'; }else{ echo 'name is available'; } fclose($f); ?> Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 Hi, thanks for the reply, I have modified your code to look like this (so that it contains my variable): function checkIfNameExists() { for ($i = 1; $i <= 22; $i++) { if (trim(strtolower($_POST['team_1_player_' . $i.'_name'])) != "" ) { $f= fopen("names.txt","r"); $r= fread($f,1024); $names = explode(",",$r); $submitted_name = trim(strtolower($_POST['team_1_player_' . $i.'_name'])); if(in_array($submitted_name,$names)) { echo 'That name is already used'; } else { echo 'name is available'; } fclose($f); } else { //Perform no action. } } } But I have entered John Smith three times in separate fields and it says name available.. Thanks Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 did you create a text file? names.txt Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 Yep. Nothing gets stored in there though. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Yep. Nothing gets stored in there though. What do you mean nothing gets stored. Store all the usernames in there seperated by commas. Everything a user registers or w.e this is, add it to the list. Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 Ah I see.. Sorry about the confusion, that wasnt what I was looking for. I needed help with when I enter two same names in two fields, they automatically get stored (array?) and from there the names are compared.. Is there a way to do this? Thanks Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Ah I see.. Sorry about the confusion, that wasnt what I was looking for. I needed help with when I enter two same names in two fields, they automatically get stored (array?) and from there the names are compared.. Is there a way to do this? Thanks Now I feel stupid, you don't need to put them in a array unless its more than 2 names <?php $name = $_POST['name']; $name2 = $_POST['name2']; if($name == $name2){ print "$name1 is the same as $name"; }else{ print"names dont match"; } ?> Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 Ah sorry about this confusion. I have to enter 22 names. I would need an array for that, wont I? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Ah sorry about this confusion. I have to enter 22 names. I would need an array for that, wont I? Sorry for the double confusion, I want to know if you have 22 textfields, or just 1 textfield where you enter all 22 names.. Also do you enter all the names at once, or everything you click the submit button it goes inside the the array? Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 As you can see from the html form, there are 22 text fields each having a different name (team_1_player_1_name, team_1_player_2_name, team_1_player_3_name and so forth..) All 22 fields have to be entered before I submit them and then they go into an array. The validation bit is that two or more same names cant be entered, so need to check for that. (I use the trim and the strtolower() as it makes it easier to compare without spaces at either side and making it lower case means, all the names will be lower case). Thanks. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 okay.. one sec i am cooking something up. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 <?php // put the posts in a array if(isset($_POST['Submit'])){ $players_names = array(); $dup = ""; for($i=1;$i<=22;$i++){ if($_POST["team_1_player_".$i."_name"] != ""){ $players_names[] = $_POST["team_1_player_".$i."_name"]; } } $num_of_duplicates = array_count_values($players_names); foreach($num_of_duplicates as $player => $key){ if($key > 1){ $dup = true; }else{ $dup = false; } } if($dup == true){ print "Sorry, duplicate name found"; } } ?> ?> if there is a duplicate, $dup will be either false or true.... If true there are more than 1 name that is the name, if its false all the names are unique. Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 Works perfectly! Thank you very much. Just one more question I know $player = the player name, how can I find out at what position in the array the duplicate or duplicates exist? Thanks once again! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Ill add that to the script.. one sec Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Hey sorry for keeping you waiting, I had a little errand to run... you can use the array_search function. Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 Cool, thank you. IF it isnt a bother, can I ask you one last question? My code so far looks like this: //Determine if the sumbit button has been clicked. If so, begin validating form data. if ($_POST['Submit'] == "Submit") { //Check if two same teams have been selected. if ($_POST['team1']== $_POST['team2']) { echo "You have selected the same two teams. <br /><br />"; } else { //Determine if a player name for team 1 was entered. playerNameTeam1(); //Determine whether the player name already exists. checkIfNameExistsTeam1(); //Team 1 info. team1Array(); } } //Determine if a Player Name for team 1 was entered. If so, then determine whether the player name already exists. function playerNameTeam1() { //this begins the loop for ($i = 1; $i <= 22; $i++) { if ($_POST['team_1_player_' . $i.'_name'] == "") { //error message echo "Enter <b>player name " .$i. "</b> for <b>".teams($team1)."</b> on line <b>" .$i ."</b><br /><br />"; } else { //Perform no action. } } } function checkIfNameExistsTeam1() { $players_names = array(); for($i = 1; $i <= 22; $i++) { if(trim(strtolower($_POST['team_1_player_' . $i.'_name'])) != "") { $players_names[] = trim(strtolower($_POST['team_1_player_' . $i.'_name'])); } } $num_of_duplicates = array_count_values($players_names); foreach($num_of_duplicates as $player => $key) { if($key > 1) { echo "The <b>player name " .$player. "</b> for <b>".teams($team1)."</b> on line <b>" .$key." already exists.</b><br /><br />"; } else { //Perform no action. } } } function team1Array() { //ARRAY //Go through $_POST array and put all values into $team1Array. foreach ($_POST as $key => $value) { if ($value != "") { $team1Array[$key] = $value; } } //Display values of $team1Array. $i = 0; foreach ($team1Array as $key => $value) { echo $team1Array['team_1_player_' . $i.'_name'] . ' ' . $team1Array['team_1_player_' . $i.'_goals'] . ' ' . $team1Array['team_1_player_' . $i.'_points'] . '<br />'; $i = $i + 1; } } As you can see I have made use of functions. My problem is that I dont want the team1Array(); executing if the validation functions are met. The team1Array function prints out all the values in the array, but if there is an error, it will show both the error/errors and the all the values of the array. How can I make it so that unless all the validation is met, the team1array function wont execute? Thanks very much for your help! Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 What you can do is that if the player name exists or whatever the case is, you can use the RETURN statement in a function. Example <?php function checkName($name){ if($name != ""){ return true; // the name is not empty }else{ return false; // the name is empty } } function checkLastName($lastname){ if($lastname!= ""){ return true; // the lastname is not empty }else{ return false; // the lastname is empty } } $name = "Bob"; $lastname = "Squid"; if((checkName($name)==true)&&(checkLastName($lastname)==true)){ echo "Yurrayyy, hello"; }else{ echp "Please enter your first and lastname"; } ?> Try this for YOUR script <?php //Determine if the sumbit button has been clicked. If so, begin validating form data. if ($_POST['Submit'] == "Submit") { //Check if two same teams have been selected. if ($_POST['team1']== $_POST['team2']) { echo "You have selected the same two teams. <br /><br />"; } else { //Determine if a player name for team 1 was entered. $playerName = playerNameTeam1(); //Determine whether the player name already exists. $nameExist = checkIfNameExistsTeam1(); //Team 1 info. if(($playerName == true)&&($nameExist==true)){ team1Array(); }else{ echo "Sorry, there was a problem processing the script"; } } } //Determine if a Player Name for team 1 was entered. If so, then determine whether the player name already exists. function playerNameTeam1() { //this begins the loop for ($i = 1; $i <= 22; $i++) { if ($_POST['team_1_player_' . $i.'_name'] == "") { //error message echo "Enter <b>player name " .$i. "</b> for <b>".teams($team1)."</b> on line <b>" .$i ."</b><br /><br />"; return false; } else { return true;//Perform no action. } } } function checkIfNameExistsTeam1() { $players_names = array(); for($i = 1; $i <= 22; $i++) { if(trim(strtolower($_POST['team_1_player_' . $i.'_name'])) != "") { $players_names[] = trim(strtolower($_POST['team_1_player_' . $i.'_name'])); } } $num_of_duplicates = array_count_values($players_names); foreach($num_of_duplicates as $player => $key) { if($key > 1) { echo "The <b>player name " .$player. "</b> for <b>".teams($team1)."</b> on line <b>" .$key." already exists.</b><br /><br />"; return false; } else { return true; //Perform no action. } } } function team1Array() { //ARRAY //Go through $_POST array and put all values into $team1Array. foreach ($_POST as $key => $value) { if ($value != "") { $team1Array[$key] = $value; } } //Display values of $team1Array. $i = 0; foreach ($team1Array as $key => $value) { echo $team1Array['team_1_player_' . $i.'_name'] . ' ' . $team1Array['team_1_player_' . $i.'_goals'] . ' ' . $team1Array['team_1_player_' . $i.'_points'] . '<br />'; $i = $i + 1; } } ?> Quote Link to comment Share on other sites More sharing options...
max_power Posted August 4, 2009 Author Share Posted August 4, 2009 It works but there is a minor problem. For the playerNameTeam1(); it doesnt go through the whole 22 fields to see if something was entered. It only displays the error message "Enter player name on line " if the first field is empty. After I enter something in that field, it gives me the "Sorry, there was a problem processing the script". Quote Link to comment Share on other sites More sharing options...
aschk Posted August 4, 2009 Share Posted August 4, 2009 I have another suggestion. You can set an array of input values, like so: <form method="POST" action="FootyForm_P.php"> <input type="text" name="names[]" /> <input type="text" name="names[]" /> <input type="text" name="names[]" /> <input type="text" name="names[]" /> </form> Then you can check for duplicates as follows: <?php if(isset($_POST['names'])){ $names = $_POST['names']; if(count($names) !== count(array_unique($names))){ echo "Duplicate name found"; } } ?> It would probably be more informative however to highlight the boxes with the duplicate names. I shall leave that as an exercise for you to do. Quote Link to comment Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 ^^ I proposed that I think but I was too lazy to change all the field names, because he has things like team_1_player_1_name team_1_player_1_email pain in the ass. uh try this and tell me the output <?php //Determine if the sumbit button has been clicked. If so, begin validating form data. if ($_POST['Submit'] == "Submit") { //Check if two same teams have been selected. if ($_POST['team1']== $_POST['team2']) { echo "You have selected the same two teams. <br /><br />"; } else { //Determine if a player name for team 1 was entered. $playerName = playerNameTeam1(); //Determine whether the player name already exists. $nameExist = checkIfNameExistsTeam1(); //Team 1 info. print $playerName . '<br>'; print $nameExist; if(($playerName == true)&&($nameExist==true)){ team1Array(); }else{ echo "Sorry, there was a problem processing the script"; } } } //Determine if a Player Name for team 1 was entered. If so, then determine whether the player name already exists. function playerNameTeam1() { //this begins the loop for ($i = 1; $i <= 22; $i++) { if ($_POST['team_1_player_' . $i.'_name'] == "") { //error message echo "Enter <b>player name " .$i. "</b> for <b>".teams($team1)."</b> on line <b>" .$i ."</b><br /><br />"; return false; } else { return true;//Perform no action. } } } function checkIfNameExistsTeam1() { $players_names = array(); for($i = 1; $i <= 22; $i++) { if(trim(strtolower($_POST['team_1_player_' . $i.'_name'])) != "") { $players_names[] = trim(strtolower($_POST['team_1_player_' . $i.'_name'])); } } $num_of_duplicates = array_count_values($players_names); foreach($num_of_duplicates as $player => $key) { if($key > 1) { echo "The <b>player name " .$player. "</b> for <b>".teams($team1)."</b> on line <b>" .$key." already exists.</b><br /><br />"; return false; } else { return true; //Perform no action. } } } function team1Array() { //ARRAY //Go through $_POST array and put all values into $team1Array. foreach ($_POST as $key => $value) { if ($value != "") { $team1Array[$key] = $value; } } //Display values of $team1Array. $i = 0; foreach ($team1Array as $key => $value) { echo $team1Array['team_1_player_' . $i.'_name'] . ' ' . $team1Array['team_1_player_' . $i.'_goals'] . ' ' . $team1Array['team_1_player_' . $i.'_points'] . '<br />'; $i = $i + 1; } } ?> Quote Link to comment Share on other sites More sharing options...
max_power Posted August 5, 2009 Author Share Posted August 5, 2009 Same thing. -the for loop for each function doesnt seem to work. It will only validate the first field and that's about it. -It will show the error message along with the values of the array. I entered a in the first field and this is the output I got: 1 1 a Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.