Jump to content

Error Reporting...


xyn

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..
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
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.