dean7 Posted April 27, 2014 Share Posted April 27, 2014 Hey guys, I've recently been getting back into coding after a long break of it, I'm currently finishing coding my Registration Script for my Website, although I've came across a few annoying things I cannot work out a way to sort. Below I have a few things I want to throw out an error if done incorrectly. If the Username field is empty I want it to Display the $Message variable, same as with the Password, CPassword and so on. Some reason in which I don't know why its not actually giving me these errors, its not letting the user Register as it shouldn't but its also not telling them why. I have try it a few other ways with the empty($Password) or !$Password etc but nothing changed. if ($Username == ""){ $Message = "Username field was missed."; $Succ = "no"; }else{ $Succ = "yes"; } if ($Password == ""){ $Message = "Password field was missed."; $Succ = "no"; }else{ $Succ = "yes"; } if ($CPassword == ""){ $Message = "Confirm Password field was missed."; $Succ = "no"; }else{ $Succ = "yes"; } if ($Email == ""){ $Message = "Email Field was missed."; $Succ = "no"; }else{ $Succ = "yes"; } On a different note, I do have if ($Password != $CPassword){ $Message = "Your Passwords don't match."; $Succ = "no"; }elseif .... Which works perfectly fine and shows the error. After I've done all that I check that the $Succ variable is not equal to no like this: if ($Succ != "no") { I am displaying the $Message variable like this: echo $Message; Which nothing is wrong with that? Anyone have an idea what I can do to make it show these errors? Thanks for any help in advance Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted April 27, 2014 Share Posted April 27, 2014 I am displaying the $Message variable like this: echo $Message; That should output the value stored in $Message. Where is that line in your code? Also when coding make sure you have error reporting enabled. Such as make sure you have these settings are in your php.ini display_errors = On error_reporting = E_ALL Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 27, 2014 Share Posted April 27, 2014 because your code is only using one set of variables, the last validation test will win. a better approach would be to use an array to hold the validation messages, one array entry per message. this will let you perform all your validation tests and output a message for each one that failed. you would also use the array as the flag that indicates a success or failure. if the array is empty() there are no validation errors. if the array is not empty, loop over and output the error messages stored in it. Quote Link to comment Share on other sites More sharing options...
dean7 Posted April 27, 2014 Author Share Posted April 27, 2014 That should output the value stored in $Message. Where is that line in your code? Also when coding make sure you have error reporting enabled. Such as make sure you have these settings are in your php.ini display_errors = On error_reporting = E_ALL When I echo out message its at the end of all the PHP. because your code is only using one set of variables, the last validation test will win. a better approach would be to use an array to hold the validation messages, one array entry per message. this will let you perform all your validation tests and output a message for each one that failed. you would also use the array as the flag that indicates a success or failure. if the array is empty() there are no validation errors. if the array is not empty, loop over and output the error messages stored in it. How would I simply change this in my code?. Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 27, 2014 Share Posted April 27, 2014 Can we assume the statement: if ($Username == ""){ has $Username holding the sanitized value of $_POST['Username']? And etc.? Quote Link to comment Share on other sites More sharing options...
dean7 Posted April 27, 2014 Author Share Posted April 27, 2014 (edited) Can we assume the statement: if ($Username == ""){ has $Username holding the sanitized value of $_POST['Username']? And etc.? For example like me doing this? $Email = filter_var($_POST['Email'], FLITER_SANITIZE_EMAIL); $CEmail = filter_var($_POST['CEmail'], FILTER_SANITIZE_EMAIL); But no I haven't got anything like that for the $Username. Same thing when I do my Emails like that they never seem to match :S Little typo Edited April 27, 2014 by dean7 Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 27, 2014 Share Posted April 27, 2014 Yes, something like that. Since you are echoing stuff to the browser, let's try: var_dump($Username); if ($Username == ""){ Let's let PHP tell us what it thinks of $Username. Quote Link to comment Share on other sites More sharing options...
dean7 Posted April 28, 2014 Author Share Posted April 28, 2014 (edited) Yes, something like that. Since you are echoing stuff to the browser, let's try: var_dump($Username); if ($Username == ""){ Let's let PHP tell us what it thinks of $Username. I done that and nothing showed I have realised when I made an account which was successful it told did tell me the username Edited April 28, 2014 by dean7 Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 28, 2014 Share Posted April 28, 2014 var_dump() gives you nothing...? If the unlikely result of var_dump() gives nothing if $Username is not set, please prepare the variable by doing this: $Username = $_POST['Username'] . ""; This will force $Username to equal an empty string if the POST variable is not set. Quote Link to comment Share on other sites More sharing options...
dean7 Posted April 28, 2014 Author Share Posted April 28, 2014 var_dump() gives you nothing...? If the unlikely result of var_dump() gives nothing if $Username is not set, please prepare the variable by doing this: $Username = $_POST['Username'] . ""; This will force $Username to equal an empty string if the POST variable is not set. I've got it as if ($_POST['Username']){ At the start then I've got $Username = safe($_POST['Username']); Safe is just my function for abit of security, if I remove that nothing different happens. So my guessing thats wrong? Should I do it like: $Username = safe($_POST['Username'] . ""; Is that how you mean? Quote Link to comment Share on other sites More sharing options...
bsmither Posted April 28, 2014 Share Posted April 28, 2014 $Username = safe($_POST['Username'] . ""); $Username = safe($_POST['Username']) . ""; Either statement. Let's see what happens. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 28, 2014 Share Posted April 28, 2014 have you even confirmed that your form is submitting the fields with the names and values that you think it is - echo '<pre>',print_r($_POST,true),'</pre>'; beyond that, the problem is in your code, either with mistyped names or code that is setting the value to an empty string or completely unsettling it. in post #2 in this thread Ch0cu3r suggested setting php's error_reporting/display_errors to get php to help you. did you do that? 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.