Forum Moderators: coopster

Message Too Old, No Replies

PHP HELP showing multiple errors at once

PHP HELP showing multiple errors at once

         

migzwebhost 1

11:33 am on Feb 3, 2012 (gmt 0)

10+ Year Member



Please help me with my PHP script because when I tested it, it only shows one error at a time, I want them all to show when a visitor sends me an invalid entry..



Here's my script:


<?php
/* Set e-mail recipient */
$myemail = "migzwebhost@gmail.com";
$subject = "Contact Form Reply";

/* Check all form inputs using check_input function */
$fullname = check_input($_POST['fullname'], "Your full name!");
$email = check_input($_POST['email'], "Your Email Address!");
$message = check_input($_POST['message'], "Your Message!");

/* If email is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Your Email Address!");
}

/* Let's prepare the message for the e-mail */
$message = "Hello Migz!

Your contact form has been submitted by:

Name: $fullname
E-mail: $email
Message: $message

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect Visitor to the thank you page */
header('Location: /thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>


<html>

<head>
:
:
</head>
<body>
:
<div class="error_message">
<h3><font style="color:#D62026;">Please correct the following error:</font></h3>
<ul>
<li><?php echo $myError; ?></li>
</ul>
</div>
:
:
</body>
</html>


<?php
exit();
}
?>

[edited by: eelixduppy at 2:40 pm (utc) on Feb 3, 2012]
[edit reason] no personal URLs, please [/edit]

rocknbil

5:29 pm on Feb 3, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Weclome aboard migzwebhost, you're on the right track, but back up a step.

You have a function check_input, but are checking other input outside that function. Why not move all your input checks into the function? This puts all that logic in one place.

Second, your show_error function does programmatic reactions - that is, it prints and exits. A function should be like a black box, you send parameters to it and get a result. I propose you change that so it does one thing: reads input and returns an error, if it exists. Otherwise it returns null.

The below may contain errors, typed on the fly from your example, but look at it's logic. It separates specific functions from the inline program logic.


<?php
/* Set e-mail recipient */
$myemail = 'migzwebhost@example.com'; // SEE TOS
$subject = 'Contact Form Reply';
$this_script = 'myscript.php';
$ty_url = '/thanks.html';
//
// Let's put the error responses and fields in an array.
// We'll use it twice.
//
$responses = array(
'fullname' => 'Your full name!',
'email' => 'Your Email Address!',
'message' => 'Your Message'
);
//
// Now check it all at once
$errors = check_input($responses);
if ($errors) { echo output_form($errors,$this_script); }
else {
$errors = process_form($myemail, $subject, $responses);
// These will be DIFFERENT errors
if ($errors) { echo output_form($errors,$this_script); }
else { process_redirect($ty_url); }
}
//
///// END of entire program logic - functions follow ////
//
// Check input function. Requires an associative array of fields and messages.
// A better way would be also to pass an association of field type, e.g.,
// where I have 'email' below, one of the passed parameters defines it as email.
//
function check_input($fields) {
// set this to NULL at the outset. If no errors are found, that's what it
// will return, otherwise it returns a string containing the errors
$errors = null;
foreach ($fields as $key => $value) {
// Apply changes directly to post values so
// you can use them cleansed later
$_POST[$key] = trim($_POST[$key]);
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars($_POST[$key]);
if (empty($_POST[$key])) {
$errors .= "<li>$value</li>\n";
}
}
// Only check email if no errors yet
if (! $errors and !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $_POST['email'])) {
$errors .= "<li>Please enter a valid email address.</li>\n";
}
return $errors;
}
//
// A function to output the form if there are errors.
//
function output_form ($errors,$script) {
// just in case. Error trapping is your friend
if (! $errors) { echo "<p>WHOOPS! How did we get here?</p>"; exit; }
$form = '
<html>
<head>
<-- blah -->
</head>
<body>
<div class="error_message">
<p style="color:#D62026;">Please correct the following errors:</p><ul>' . $errors . '</ul></div>
<!-- OUTPUT YOUR FORM AGAIN HERE -->
<form method="post" action="' . $script . '">
<p><label for="email">Email:</label><span class="required">*</span>
<input type="text" name="email" id="email" value="' . $_POST['email'] . '"></p>
<p><label for="fullname">Full Name:</label><span class="required">*</span>
<input type="text" name="fullname" id="fullname" value="' . $_POST['fullname'] . '"></p>
<p><label for="message">Message</label><span class="required">*</span>
<textarea name="message" id="message">' . $_POST['message'] . '</textarea></p>
<p><input type="submit" name="submitButton" value="Send Message"></p>
</form>
';
return $form;
}
//
// Now process the form. It will also compile errors if found, and
// if so, the above logic will again use output_form.
//
function process_form($email, $subj, $fields) {
//
$errors = null;
$message = "Hello Migz!
Your contact form has been submitted by:
";
/* Let's prepare the message for the e-mail */
foreach ($fields as $key => $value) {
$message .= ucfirst($key) . ": " . $_POST[$key] . "\n";
}
$message .= "\n\n\nEnd of message\n";
// Our friend again, error trapping. :-)
if ! (mail($myemail, $subject, $message)) {
$errors .= "<li>It appears the mail function failed.</li>\n";
}
return $errors;
}
//
// After processing the form, you have two options: output the
// form again if there are mail errors, or redirect.
//
function process_redirect ($url) {
header("Location:$url"); // automatically exits
}


THIS IS NOT COPY AND PASTE CODE and is typed on the fly, but examine it for the difference in logic. Note that all of each task is in one place. If there's any problem, you print out the errors and the form so the user doesn't have to use their back button. The only global variable used in any function is $_POST (and even that can be eliminated with more work.) It's still not perfect, but it's better than strict inline linear programming.

migzwebhost 1

5:59 am on Feb 4, 2012 (gmt 0)

10+ Year Member



Hello! Thank you for your quick response.

Can you do the replacing of variables for me?
I can't do it myself because I'm a greenhorn in the PHP field.

These are the informations:
Thank you url: thanks.html
This script name: contact-process.php

I've tried to figure out what's wrong but nothing happened. I need a professional programmer who can help me and that's you. Thanks a lot and GOD Bless You!

migzwebhost 1

6:00 am on Feb 4, 2012 (gmt 0)

10+ Year Member



By the way, here's my website and you can test it. I'm still using my old script because I have problems with your script.

Thanks!