chris_161 Posted July 7, 2010 Share Posted July 7, 2010 Hi there. Im quite new to php but slowly getting there lol. Below is a bit of code ive written, but for some reason the invalid characters bit does not work properly and i cannot see what is wrong. Any help would be much appricated. Thanks in advance. CODE*** <title>PHP</title><center> <?php if (empty($_POST['username'])) { $errors[] = 'Please enter a Username'; } if (empty($_POST['SID'])) { $errors[] = 'Please enter a SID'; } else if (!is_numeric($_POST['SID'])) { $errors[] = 'Please enter a valid SID with numeric values'; } if (empty($_POST['comments'])) { $errors[] = 'Please enter some comments'; } else if (strlen( $_POST['comments'] )> 50) { $errors[] = 'comments is too long'; } else if (false == ereg( '^[A-Za-z0-9_-]+$', $comments )) { $errors[] = 'comments contains invalid characters'; } else $mail= $_POST["mail"]; if(!filter_has_var(INPUT_POST,"mail")) { $errors[] = "Input type does not exists"; } else { if(!filter_input(INPUT_POST,"mail",FILTER_VALIDATE_EMAIL )) { $errors[] = "Email address is not valid"; } else { echo "Form successully submitted"; } } if (count($errors) == 0) { // Process form } else { echo $errors[0]; } ?> <form method="post"> <table> <tr> <td>Username:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>SID:</td> <td><input type="text" name="SID"></td> </tr> <tr> <td>Comments:</td> <td colspan="2"><textarea name="comments"></textarea></td> </tr> <tr> <td>Enter an Email address :</td> <td><input type="text" name="mail"/></td> </tr> <tr> <td colspan="2"><hr><center><input type="submit" name="submit"></center></td> </tr> </table> </form> </center> Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/ Share on other sites More sharing options...
myrddinwylt Posted July 7, 2010 Share Posted July 7, 2010 Hello, Try replacing the way you are checking the contents of the field as follows: Current empty($_POST['username']) Replace with trim($_POST['username'] . "") != "" The . "" is a bit redundant, but I have had some cases where I needed to use that to ensure no errors. It's really out of laziness ^.^ trim Will remove any of the trailing spaces, so the string will be truncated. != "" means that the value is not Short and sweet, should fix your problem. EDIT: In the future, you might want to put a subject which is more descriptive of your problem than "HELP", or such. In this case, a good subject would be something like "IF statements not processing" Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082327 Share on other sites More sharing options...
chris_161 Posted July 7, 2010 Author Share Posted July 7, 2010 Thanks for you quick reply. So where i have the code--> if (false == ereg( '^[A-Za-z0-9_-]+$', $comments )) { $errors[] = 'comments contains invalid characters'; Shall i do the same process and put the trim value there? Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082329 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 @myrddinwylt Why is that method of evaluating if the string is empty, better than the function actually designed for it? Plus if you look at the regexp he's already performing a check to make sure there's no spaces, so the trim() call's redundant. @chris_161 Use empty() as you were.. ereg however is now deprecated, you should be using preg_match: else if (!preg_match('/^[A-Za-z0-9_-]+$/', $comments)) { $errors[] = 'comments contains invalid characters'; } Edit: where are you defining $comments? On the previous line you're using $_POST['comments'] .. Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082333 Share on other sites More sharing options...
chris_161 Posted July 7, 2010 Author Share Posted July 7, 2010 Thanks for your reply but its still not working, e.g if i enter the word HELLO it says invalid character and if i enter %$^&$ it says invalid character. Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082338 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 Sorry I edited my post a little later, have you seen it? Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082340 Share on other sites More sharing options...
chris_161 Posted July 7, 2010 Author Share Posted July 7, 2010 Yeah thanks Adam, I did what you listed but I still get the same outcome as i listed above. Sorry to be a pain. Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082342 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 No problem. Could you post the code as you have it now? Tested separately the preg_match() works fine. Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082343 Share on other sites More sharing options...
chris_161 Posted July 7, 2010 Author Share Posted July 7, 2010 <title>PHP</title><center> <?php if (empty($_POST['username'])) { $errors[] = 'Please enter a Username'; } if (empty($_POST['SID'])) { $errors[] = 'Please enter a SID'; } else if (!is_numeric($_POST['SID'])) { $errors[] = 'Please enter a valid SID with numeric values'; } if (empty($_POST['comments'])) { $errors[] = 'Please enter some comments'; } else if (strlen( $_POST['comments'] )> 50) { $errors[] = 'comments is too long'; } if (!preg_match('/^[A-Za-z0-9_-]+$/', $comments)) { $errors[] = 'comments contains invalid characters'; } else $mail= $_POST["mail"]; if(!filter_has_var(INPUT_POST,"mail")) { $errors[] = "Input type does not exists"; } else { if(!filter_input(INPUT_POST,"mail",FILTER_VALIDATE_EMAIL )) { $errors[] = "Email address is not valid"; } else { echo "Form successully submitted"; } } if (count($errors) == 0) { // Process form } else { echo $errors[0]; } ?> <form method="post"> <table> <tr> <td>Username:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>SID:</td> <td><input type="text" name="SID"></td> </tr> <tr> <td>Comments:</td> <td colspan="2"><textarea name="comments"></textarea></td> </tr> <tr> <td>Enter an Email address :</td> <td><input type="text" name="mail"/></td> </tr> <tr> <td colspan="2"><hr><center><input type="submit" name="submit"></center></td> </tr> </table> </form> </center> Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082344 Share on other sites More sharing options...
Adam Posted July 7, 2010 Share Posted July 7, 2010 Edit: where are you defining $comments? On the previous line you're using $_POST['comments'] .. As hinted in the edit, you need to change $comments to $_POST['comments'] as in the previous conditions. Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082345 Share on other sites More sharing options...
chris_161 Posted July 7, 2010 Author Share Posted July 7, 2010 Adam your the man, Cheers for you help very much appricated. Quote Link to comment https://forums.phpfreaks.com/topic/206982-helpp/#findComment-1082347 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.