Jump to content


Photo

Error Reporting...


  • Please log in to reply
3 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 29 June 2006 - 03:07 PM

Hey,

Does anyone know how to make a whole List of errors, Basically If there was a feedback form with fields (name, email, message) and their all left blank. Instead of echoing the error individually just list them all.

I got so far as to this:
$error="";

if( !isset($field))
{
echo "<li><font color=#FF0000>" . $error='error1' . "</font></li>";
}
elseif( !isset($field2))
{
echo "<li><font color=#FF0000>" . $error='error2' . "</font></li>";
}

But that's my guess, i haven't tried this before..

#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 03:20 PM

Instead of saying error = 'new error' concatenate the new messages to the existing string ...

$error .= 'new error'
$error .= 'new error 2'

echoing this would print both those as...
new errornew error2

I suggest you use some formating such as...
$error .= '<li>new error</li>';

Alternatively use an array...
$error = array();

$error[] = 'new error';
$error[] = 'ner error 2';

foreach ($error as $string) echo $string;

That would echo each value in the array.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 thepip3r

thepip3r
  • Members
  • PipPipPip
  • Advanced Member
  • 289 posts

Posted 29 June 2006 - 03:37 PM

if ((!isset($field1)) || (!isset($field2)) || (!isset($field3))) {
  echo "you left a field blank";
  exit;
}

OR

if ((!isset($field1)) || (!isset($field2)) || (!isset($field3))) {
  if (!isset($field1)) {
    echo "$field1 was not set";
  }
  if (!isset($field2)) {
    echo "$field2 was not set";
  }
  if (!isset($field3)) {
    echo "$field3 was not set";
  }
  exit;
}


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 June 2006 - 03:38 PM

Your best of with adding the errors to an array, then at the end check whether there is any errors. If there are echo them out. Like so:
<?php

// check that the form has been submitted:
if(isset($_POST['submit']))
{
    // define our error variabled
    $error = '';

    if(isset($_POST['name']) && empty($_POST['name']))
    {
        // if the name fieled was not filled in, we add the following to our error array
        $error[] = "You have not provided a name. Please enter a name";
    }

    if(isset($_POST['email']) && empty($_POST['email']))
    {
        // if the email fieled was not filled in, we add the following to our error array
        $error[] = "Please provide an email address";
    }

    if(isset($_POST['message']) && empty($_POST['message']))
    {
        // if no message was entered, we add the following to our error array
        $error[] = "You have not provided an message for your email";
    }

    // now we check whether our error variable is an array
    if(is_array($error))
    {
        echo "Please correct the following errors:\n<ul>\n";
        
                // now we loop through our error array and display any errors:
        foreach($error as $key => $value)
        {
            echo '<li>' . $value . "</li>\n";
        }

        echo "</ul>\n";
    }
    else
    {
        // no error was detected continue on
    }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  Name: <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '' ?>" /><br />
  Email addtress: <input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?>" /><br />
  Message: <textarea name="message"><?php echo isset($_POST['message']) ? $_POST['message'] : '' ?></textarea><br />
  <input type="submit" name="submit" value="Submit" />
</form>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users