Jump to content

Recommended Posts

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>");




?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/221916-instead-of-die/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148353
Share on other sites

<?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";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148373
Share on other sites

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";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148397
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/221916-instead-of-die/#findComment-1148441
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.