mat3000000 Posted December 16, 2010 Share Posted December 16, 2010 Here is my code... I am using this in HTML and if I put it near my form, the html after this code will not run??? Can someone suggest an alternative to die? (That's if that is the problem, I am guessing it is?!) <?php $username = $_POST['username']; $password = $_POST['password']; if ($username=='User Name') die ("<br /><br /><div align='center' class='errorbox'>Please enter a valid username!</div>"); if ($password=='Password') die ("<br /><br /><div align='center' class='errorbox'>Please enter a valid password!</div>"); if ($username=='') die ("<br /><br /><div align='center' class='errorbox'>Please enter a valid username!</div>"); if ($password=='') die ("<br /><br /><div align='center' class='errorbox'>Please enter a valid password!</div>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/ Share on other sites More sharing options...
trq Posted December 16, 2010 Share Posted December 16, 2010 echo. Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148352 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 Store error messages in an array, then if the array is empty allow the code to proceed, otherwise display the errors, and redisplay the form with the entered values pre-populated so the user can make corrections. Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148353 Share on other sites More sharing options...
QuickOldCar Posted December 16, 2010 Share Posted December 16, 2010 <?php $username = $_POST['username']; $password = $_POST['password']; if ($username == 'User Name' || ''){ echo ("<br /><br /><div align='center' class='errorbox'>Please enter a valid username!</div>"); echo "<meta http-equiv='refresh' content='5;url=http://somesite.com/somepage.php"; } if ($password == 'Password' || ''){ echo ("<br /><br /><div align='center' class='errorbox'>Please enter a valid password!</div>"); echo "<meta http-equiv='refresh' content='5;url=http://somesite.com/somepage.php"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148373 Share on other sites More sharing options...
QuickOldCar Posted December 16, 2010 Share Posted December 16, 2010 Or you can just send them back a page using javascript if ($usename == 'User Name' || '') { die("You didn't insert a user name."<br /><a href=\"javascript: history.go(-1)\">Click here to try again </a>."); } Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148379 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 Surely, you mean: if ($username == 'User Name' || $username == ''){ Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148390 Share on other sites More sharing options...
QuickOldCar Posted December 16, 2010 Share Posted December 16, 2010 Yes I did, just got up, sorry Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148393 Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2010 Share Posted December 16, 2010 It's also a good idea to validate and display the errors for all of the form fields at once, rather than displaying only the first error found, fix that, resubmit, fix the next error, resubmit... Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148394 Share on other sites More sharing options...
QuickOldCar Posted December 16, 2010 Share Posted December 16, 2010 the corrected of my mistake above, can also do stuff like adding $checked=0; for each Then at the end do a if ($checked == 0){ echo ("<br /><br /><div align='center' class='errorbox'>You didn't properly fill out the form!</div>"); echo "<meta http-equiv='refresh' content='5;url=http://somesite.com/somepage.php"; } <?php $username = $_POST['username']; $password = $_POST['password']; if ($username == 'User Name' || $username == ''){ echo ("<br /><br /><div align='center' class='errorbox'>Please enter a valid username!</div>"); echo "<meta http-equiv='refresh' content='5;url=http://somesite.com/somepage.php"; } if ($password == 'Password' || $password == ''){ echo ("<br /><br /><div align='center' class='errorbox'>Please enter a valid password!</div>"); echo "<meta http-equiv='refresh' content='5;url=http://somesite.com/somepage.php"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148397 Share on other sites More sharing options...
Anti-Moronic Posted December 17, 2010 Share Posted December 17, 2010 DIE principle comes to mind in the above. It's really quite simple. Use an errors array, loop through. Also, use a list for errors as it will format a lot better by default. $username = $_POST['username']; $password = $_POST['password']; $errors = array(); if($username == 'User Name' || $username == ''){ $errors[] = 'Please enter a valid username!'; } if($password == 'Password' || $password == ''){ $errors[] = 'Please enter a valid password!'; } $errorHtml = '<ul id="errorlist">'; foreach($errors as $error){ $errorHtml .= '<li class="errorbox">'.$error.'</li>'; } $errorHtml .= '</ul>'; echo $errorHtml; Now you can add lots of messages without repeating the same html over and over. Note that I also stored the html in a variable. Sometimes you might not want to echo immediately. Now you can easily include that html any where. You're not using dodgy <br tags to format a list and you can redirect using php if need be because you haven't immediately outputted the html. Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148441 Share on other sites More sharing options...
Anti-Moronic Posted December 17, 2010 Share Posted December 17, 2010 That would be DRY, by the way. (don't repeat yourself) Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148461 Share on other sites More sharing options...
mat3000000 Posted December 18, 2010 Author Share Posted December 18, 2010 Thanks for all the help Guys!!! Sorted it now Quote Link to comment https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1149072 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.