Jump to content

Archived

This topic is now archived and is closed to further replies.

xyn

Error Reporting...

Recommended Posts

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:
[code]$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>";
}[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[code]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;
}[/code]

Share this post


Link to post
Share on other sites
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:
[code]<?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>[/code]

Share this post


Link to post
Share on other sites

×

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.