Bounty Posted March 12, 2011 Share Posted March 12, 2011 Ok so i have registration page on my webs but i didn't work on empty fields so how could i check all fields? //if no name if(!$name){ $error = "Username missing."; } //if no pass if($pass == false){ $error = "$error,Password missing"; } //if no pass conf if($pass_conf == false) { $error = "$error,Pass conf missing."; } //if no email if($email == false){ $error = "$error,Email missing."; } //pass conf if($pass != $pass_conf){ $error = "$error,Passwords do not match."; } echo "<script>alert('$error');</script>;"; //header("Location: register.php"); My script gives same results every time (even if i fill any of fields)...also if i un-comment header command it wont show alert..it just reloads the page :'( So can anyone help me with this? Thanks.. Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 12, 2011 Share Posted March 12, 2011 I would try this: <?php $fileds = array(0=>$name,1=>$pass,2=>$pass_conf,3=>$email); $formGood = true; foreach($fileds as $x){ if(empty($x)){ $formGood = false; } } if($fileds[1] != $fileds[2]){ $formGood = false; } if($formGood == true){ echo "<script>alert('$error');</script>;"; } else{ header("Location: register.php"); } ?> Let me know what happens Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 12, 2011 Author Share Posted March 12, 2011 I think you didn't understand what i wanna achieve... If just email and username i empty and rest are filled it should alert error "Username and email fields are empty.." (for each false different error and after user clicks on "ok" button reload page,or just stop script... Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 12, 2011 Share Posted March 12, 2011 Try this: <?php $fileds = array('Username'=>$name,'Password'=>$pass,'Password Confirmation'=>$pass_conf,'Email'=>$email); $formGood = ''; foreach($fileds as $x){ if(empty($x)){ $formGood .= 'Your '.key($x).' is empty!!!!'; } } if($fileds[1] != $fileds[2]){ $formGood .= key($fields[1]).' must equal '.key($fields[2]).'.'; } if($formGood != ''){ echo "<script>alert('$error');</script>;"; } else{ header("Location: register.php"); } ?> Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 12, 2011 Author Share Posted March 12, 2011 Warning: key() expects parameter 1 to be array, string given in C:\xampp\htdocs\login\check.php on line 13 Warning: key() expects parameter 1 to be array, string given in C:\xampp\htdocs\login\check.php on line 13 Notice: Undefined offset: 1 in C:\xampp\htdocs\login\check.php on line 16 Notice: Undefined offset: 2 in C:\xampp\htdocs\login\check.php on line 16 Notice: Undefined variable: error in C:\xampp\htdocs\login\check.php on line 20 ; Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 12, 2011 Share Posted March 12, 2011 Try this: <?php $fileds = array('Username'=>$name,'Password'=>$pass,'Password Confirmation'=>$pass_conf,'Email'=>$email); $formGood = ''; foreach($fileds as $x){ if(empty($x)){ $formGood .= 'Your '.array_search($x).' is empty!!!!'; } } if($fileds[1] != $fileds[2]){ $formGood .= array_search($fields[1]).' must equal '.array_search($fields[2]).'.'; } if($formGood != ''){ echo "<script>alert('$error');</script>;"; } else{ header("Location: register.php"); } ?> Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 12, 2011 Author Share Posted March 12, 2011 Similar errors as the minute ago...couldn't it be done via my first script? Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 12, 2011 Share Posted March 12, 2011 Sorry to have posted so much. Here is the script again. I tested and debugged so I know it works: <?php $name = 'asdf'; $pass = 'dan'; $pss_conf = 'd'; $fields = array('Username'=>$name,'Password'=>$pass,'Password Confirmation'=>$pass_conf,'Email'=>$email); $formGood = ''; foreach($fields as $key=>$x){ if(empty($x)){ $formGood .= 'Your '.$key.' is empty!!!!'; } } if($fields['Password'] != $fields['Password Confirmation']){ $formGood .= 'Your Password must be equal to the Password Confirmation.'; } if($formGood != ''){ echo "<script>alert('$formGood');</script>"; } else{ header("Location: register.php"); } ?> I removed the ';' that you had after the '</script>' tag. empty() is a better method than false as it will check for more values that are considered 'empty'. See: http://www.php.net/manual/en/function.empty.php Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 12, 2011 Share Posted March 12, 2011 Did you get it to work? Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 13, 2011 Author Share Posted March 13, 2011 Works great even if i do not understand the script but nvm about that...anyhow could a page be reloaded after ok is pressed? Thanks for the help so far Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 13, 2011 Share Posted March 13, 2011 You should be able to go to a different location via javascript. The alert function is syncronous, meaning that it will halt execution of all other javascript untill the ok button is pressed. If you want to redirect the client after they receive the warning try replacing the bottom part of the above script with the following: continued from above... _____________________________ if($formGood != ''){ echo "<script>alert('$formGood');window.location=register.php;</script>"; } ?> Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 13, 2011 Share Posted March 13, 2011 sorry, you need to wrap the register.php in quotes. Thus: if($formGood != ''){ echo "<script>alert('$formGood');window.location="register.php";</script>"; } Quote Link to comment Share on other sites More sharing options...
cs.punk Posted March 13, 2011 Share Posted March 13, 2011 An empty string should return false, however when assigned the value to an empty $_POST value it returns true (for some reason). You basically needed one function, empty() which returns true if it is indeed empty. So: if (empty($name)) {echo "The username is empty"; } You can also add ! before empty: !empty($name) which does the opposite (checks if it is NOT empty). Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 13, 2011 Author Share Posted March 13, 2011 I needed to backslash quotes in echo line...it works now just there is an error :S After alert message shows up and after i press ok it reloads the page as nothing happened,on the first sight it looks great,but i've just checked my database and saw that it actually crates a user with empty fields (even if the page is redirected)... Can it be made for script to stop at one place? like: if($formGood != ''){ echo "<script>alert('$formGood');window.location=\"register.php\";</script>"; //stop here! } Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 14, 2011 Author Share Posted March 14, 2011 bump Quote Link to comment Share on other sites More sharing options...
phpJoeMo Posted March 14, 2011 Share Posted March 14, 2011 Backslashes can be voided by using single quotes (I should've used them to begin with). Using the die() function will make a final output and then halt the script from further exectuion: -------Code------------ if($formGood != ''){ die("<script>alert('$formGood');</script>"); } echo 'foobar rama!'; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 14, 2011 Share Posted March 14, 2011 That's not a very good way to do it. The user should be presented with the original form, with the previously entered values pre-filled in their respective fields, and either a list of errors, or the individual errors near each field instead of having to fill out everything again. Not to mention that a user without javascript enabled will get nothing but a blank screen using the method above. Each field should be validated to make sure it contains the expected data. Store the errors in an array, and if the array is empty after the validation routine, there were no errors detected, and the code can proceed. Moreover, using die() when there's an error is like slamming the door in a customer's face. There are much more user friendly ways of handling it. Take the following code as an example, and you should have a good start on the logic needed to do this properly. <?php // Uncomment next line for debug. //echo '<pre>'; print_r($_POST); echo '</pre>'; if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted $errors = array(); // initialize an array to hold validation errors $_POST = array_map('trim', $_POST); // trim all $_POST array values if( !empty($_POST['name']) ) { // validate the name field if( !ctype_alpha($_POST['name']) ) { $errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error } if( strlen($_POST['name']) < 3 || strlen($_POST['name'] > 20) ) { $errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error } } else { $errors[] = 'Name is a required field.'; // if name is empty, store error } if( !empty($_POST['number']) ) { // same validations as in name, above. if( !ctype_digit($_POST['number']) ) { $errors[] = 'Number must be numeric.'; } if( strlen($_POST['number']) < 5 || strlen($_POST['number']) > 10 ) { $error = 'Number must be from 3 to 20 digits. It is currently ' . strlen($_POST['number']) . ' digit'; $error .= strlen($_POST['number']) == 1 ? '.' : 's.'; $errors[] = $error; } } else { $errors[] = 'Number is a required field.'; } if( !empty($errors) ) { // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form echo "<font color=\"red\">The following errors were detected:<br>"; echo implode("<br>\n", $errors); echo '</font>'; } } if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) { ?> <form method="post"> Name (3-20 letters): <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>"><br> Number (5-10 numbers): <input type="text" name="number" value="<?php echo isset($_POST['number']) ? $_POST['number'] : ''; ?>"><br> <input type="hidden" name="submitted" value="yes"> <input type="submit" name="submit" value="Submit"> </form> <?php } else { // Form was submitted, and validated with no errors. OK to run db insert, display success message, etc. echo "Successful submission!"; } ?> Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 14, 2011 Author Share Posted March 14, 2011 I think that's all i need...just could you guide me through the code...i understand most of it but..what would commented part of the code do? What this first line means? and what should 'triming' mean? if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { $errors = array(); $_POST = array_map('trim', $_POST); And this one if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) { ?> And i should add to <form method="post"> action="do_reg.php" ? Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 17, 2011 Author Share Posted March 17, 2011 Ok,forget about last post,i get it all now...anyhow i made it work on simple html form but i can't add this php to my form on my page: <?php // Uncomment next line for debug. echo '<pre>'; print_r($_POST); echo '</pre>'; if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted $errors = array(); // initialize an array to hold validation errors $_POST = array_map('trim', $_POST); // trim all $_POST array values if( !empty($_POST['username']) ) { // validate the name field if( !ctype_alnum($_POST['username']) ) { $errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error } if( strlen($_POST['username']) < 3 || strlen($_POST['username'] > 20) ) { $errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error } } else { $errors[] = 'Name is a required field.'; // if name is empty, store error } if( !empty($_POST['password']) ) { // same validations as in name, above. if( !ctype_alnum($_POST['password']) ) { $errors[] = 'Password must be alphanumeric charachters only.'; } if( strlen($_POST['password']) < 3 || strlen($_POST['password']) > 20 ) { $error = 'Password must be from 3 to 20 charachters. It is currently ' . strlen($_POST['password']) . ' digit'; $errors[] = $error; } } else { $errors[] = 'Password is a required field.'; } if( !empty($_POST['password2']) ) { // same validations as in name, above. if($_POST['password2'] != $_POST['password']) { $error = 'Passwords do not match.' . $errors[] = $error; } } else { $errors[] = 'Confirm your password.'; } if(empty($_POST['email']) ) { // same validations as in name, above. $errors[] = 'Email is a required field.'; } if( !empty($errors) ) { // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form echo "<font color=\"red\">The following errors were detected:<br>"; echo implode("<br>\n", $errors); echo '</font>'; } } if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) { ?> <html> <head> <title>Register!</title> </head> <body> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <table border="5" align="center" cellpadding="5" cellspacing="5" bordercolor="#333333"> <tr> <td width="400" align="left" valign="middle"> <table border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#333333"> <tr> <td align="left" valign="middle"> <form action="do_reg.php" method="post" id="info"> <h2 align="center" class="style10">Register:</h2> <div id="username-wrap" class="slider"> <label for="username">Username:</label> <input type="text" id="name" name="username" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#username-wrap--> <div id="pass-wrap" class="slider"> <label for="password">Password:</label> <input type="password" id="pass" name="password" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#password-wrap--> <div id="pass2-wrap" class="slider"> <label for="password2">Repeat:</label> <input type="password" id="pass2" name="pass_conf" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#password2-wrap--> <div id="email-wrap" class="slider"> <label for="email">Email:</label> <input type="text" id="email" name="email" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#email-wrap--> <div align="center"> <input type=hidden name=ip value='<?php echo $ip ?>'/> <input type="submit" id="btn" name="submit" value="Submit" autocomplete="off" style="font-size: 17px; font-weight: bold; background-color: #000000; color: #CCCCCC;"/> </div> </form></td> </tr> </table> </td> </tr> </table> </body> </html> I get error on last line (</html>) how to make this work??? :S Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 17, 2011 Share Posted March 17, 2011 You've left a curly brace open. Add the closing curly after the html block. <?php } ?> Quote Link to comment Share on other sites More sharing options...
aabid Posted March 17, 2011 Share Posted March 17, 2011 Can it be made for script to stop at one place? like: if($formGood != ''){ echo "<script>alert('$formGood');window.location=\"register.php\";</script>"; //stop here! } Yes u can use exit(); function wherever and whenever you want your script to stop and don't continue any further. Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 17, 2011 Author Share Posted March 17, 2011 @Pikachu When i do this: <?php //php code { <html> //html block </html> } ?> I get this error: Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\login\register1.php on line 54 54 line = <html> And when i do this: <?php //php code { ?> <html> //html block </html> <?php } ?> It doesn't show any errors but it wont echo errors...i just echoes debug lines... Array ( [username] => dsadsa [password] => dasdas [pass_conf] => dsadas => dasdas [submit] => Submit ) @aabid Thanks thats useful info Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 17, 2011 Share Posted March 17, 2011 Post your code in its current form . . . Quote Link to comment Share on other sites More sharing options...
Bounty Posted March 17, 2011 Author Share Posted March 17, 2011 <?php // Uncomment next line for debug. echo '<pre>'; print_r($_POST); echo '</pre>'; if( isset($_POST['submitted']) && $_POST['submitted'] == 'yes' ) { //check for hidden field value to indicate form has been submitted $errors = array(); // initialize an array to hold validation errors $_POST = array_map('trim', $_POST); // trim all $_POST array values if( !empty($_POST['username']) ) { // validate the name field if( !ctype_alnum($_POST['username']) ) { $errors[] = 'Name must be alphabetic characters only.'; // if name has non alpha chars, store error } if( strlen($_POST['username']) < 3 || strlen($_POST['username'] > 20) ) { $errors[] = 'Name must be from 3 to 20 characters.'; // if name has too many/few chars, store error } } else { $errors[] = 'Name is a required field.'; // if name is empty, store error } if( !empty($_POST['password']) ) { // same validations as in name, above. if( !ctype_alnum($_POST['password']) ) { $errors[] = 'Password must be alphanumeric charachters only.'; } if( strlen($_POST['password']) < 3 || strlen($_POST['password']) > 20 ) { $error = 'Password must be from 3 to 20 charachters. It is currently ' . strlen($_POST['password']) . ' digit'; $errors[] = $error; } } else { $errors[] = 'Password is a required field.'; } if( !empty($_POST['password2']) ) { // same validations as in name, above. if($_POST['password2'] != $_POST['password']) { $error = 'Passwords do not match.' . $errors[] = $error; } } else { $errors[] = 'Confirm your password.'; } if(empty($_POST['email']) ) { // same validations as in name, above. $errors[] = 'Email is a required field.'; } if( !empty($errors) ) { // if the $errors array is not empty, display the errors to allow the user to correct them and resubmit the form echo "<font color=\"red\">The following errors were detected:<br>"; echo implode("<br>\n", $errors); echo '</font>'; } } if( (isset($_POST['submitted']) && !empty($errors)) || !isset($_POST['submitted']) ) { ?> <html> <head> <title>Register!</title> </head> <body> <table border="5" align="center" cellpadding="5" cellspacing="5" bordercolor="#333333"> <tr> <td width="400" align="left" valign="middle"> <table border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#333333"> <tr> <td align="left" valign="middle"> <form method="post" id="info"> <h2 align="center" class="style10">Register:</h2> <div id="username-wrap" class="slider"> <label for="username">Username:</label> <input type="text" id="name" name="username" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#username-wrap--> <div id="pass-wrap" class="slider"> <label for="password">Password:</label> <input type="password" id="pass" name="password" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#password-wrap--> <div id="pass2-wrap" class="slider"> <label for="password2">Repeat:</label> <input type="password" id="pass2" name="pass_conf" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#password2-wrap--> <div id="email-wrap" class="slider"> <label for="email">Email:</label> <input type="text" id="email" name="email" autocomplete="off" style="background-color: #000000; color: #999999;" /> </div><!--/#email-wrap--> <div align="center"> <input type="submit" id="btn" name="submit" value="Submit" autocomplete="off" style="font-size: 17px; font-weight: bold; background-color: #000000; color: #CCCCCC;"/> </div> </form></td> </tr> </table> </td> </tr> </table> </body> </html> <?php } ?> Sorry for bothering so much :S Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted March 17, 2011 Share Posted March 17, 2011 You removed the hidden field that is checked for to see if the form has been submitted. Add it in to the form again, and that should take care of that. <input type="hidden" name="submitted" value="yes"> And why did you remove the code that allows the values that were submitted to be echoed into the fields if there was a validation error? Now you're right back to square one; if the user makes an error in any of the fields, they have to fill everything out all over again. 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.