Jump to content

Send Mail error-handling broken?


WelshPaul

Recommended Posts

Hello everyone,

 

I am using the following script with a contact form. Simply put the user enters their name, email address and a message. The contact form displays correctly and upon filling in all the fields the message is sent and a confirmation displayed on screen instructing so.

 

I have a problem when someone misses a field and enters nothing, No error message is displayed. The message does not get sent which is good but for example if someone fails to enter their name "First and Last name" should be the error displayed.

 

Can you help?

 

<?php


/* Set the email of the recipient (your email) */
$recipient = "emailme@myemailaddress.com";




if ( isset($_POST['submit']) ) // Just send the email if the $_POST variable is set
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];


$subject = "Email From: " . $domain; // Subject of the email message


$errors = array(); // empty array of the error


/*
* The fields
* 1st param: submitted data
* 2nd param: required (TRUE) or (FALSE)  
* 3rd param: the name for the error
*/
$fields = array(
'name' => array($name, TRUE, "First and Last name"),
'email'  => array($email, TRUE, "E-mail Address"),
'message'  => array($message, TRUE, "Your Message"),
);


$i=0;
foreach ($fields as $key => $field)
{
if ( FALSE == test_field( $field[0], $field[1] ) )
{
$errors[$key] = "The field '".$field[2]."' is required.";
}
$i++;
}


//var_dump($errors);


if (empty($errors)) // If there is no errors, we can send the mail
{
$body = "";
$body .= "----- Info about the sender -----\n\n";
$body .= "Name: ".$fields['name'][0]."\n";
$body .= "Email: ".$fields['email'][0]."\n";
$body .= "\n\n----- Message -----\n\n";
$body .= $fields['message'][0];


if( mail( $recipient, $subject, $body, "FROM: ".$fields['email'][0] ) ) // Try to send the message, if not successful, print out the error
{
message_was_sent($fields);
}
else
{
echo "It is not possible to send the email. Check out your PHP settings!";
print_the_form($errors, $fields);
}
}
else // If there are any errors
{
print_the_form($errors, $fields);
} 
}
else
{
print_the_form();
}


function print_the_form($errors = array(), $fields = null) // Display the form
{
?>
<form class="contact-form" action="#contact-form" method="post" id="contact-form">
<div class="input-item"><label>NAME</label><input type="text" name="name" value="<?php inpt_value('name', $fields); ?>" id="inpt-name"></div>
<div class="input-item"><label>EMAIL</label><input type="email" name="email" value="<?php inpt_value('email', $fields); ?>" id="inpt-email"></div>
<div class="textarea-item"><label>MESSAGE</label>
<textarea name="message" id="txtarea"><?php inpt_value('message', $fields); ?></textarea></div>
<input type="hidden" value="1" name="submit" />
<div class="send-button"><button type="submit">SEND</button></div>
</form>
<!--  = /contact form =  -->
<?php
}


function message_was_sent($fields) // Notification that sending the mail was successful
{
?>
<div class="alert alert-success">
Your message was sent successfully!
</div>
<?php
}


/* Returns TRUE if field is required and OK */
function test_field($content, $required)
{
if ( TRUE == $required )
{
if (strlen($content)<1)
{
return FALSE;
}
else
{
return TRUE;
}
} else 
{
return TRUE;
}
}


/* Add the appropriate class name to the specified input field */
function error_class($name, $errors) {
if ( array_key_exists( $name, $errors ) ) {
echo " error";
}
}


/* Repopulate the data when the form is submitted and errors returned */
function inpt_value($name, $fields) {
if ( null === $fields ) {
return;
} else {
echo $fields[$name][0];
}
} 


function show_error( $name, $errors ) {
if ( array_key_exists( $name, $errors ) )
echo '<div class="help-block"> ' . $errors[$name] . ' </div>';
}

 

Link to comment
Share on other sites

You have passed the $errors array to the print_the_form function; but you never output the error messages anywhere in that function.

 

Hi David,

 

Thanks for taking the time to offer me some help. I'm a total noob when it comes to php but i'm trying lol

 

am I correct in thinking it's just a case of altering this

<?php inpt_value('name', $fields); ?>

To something like this:

<?php inpt_value('name', $fields); echo $errors['name']; ?>

Be grateful for any more pointers. Thanks

Link to comment
Share on other sites

I noticed a function show_error() in your source file. It would seem to me, you just need to call this function in the appropriate places. It will output the appropriate error message, if one exists for the specified field:

 

function print_the_form($errors = array(), $fields = null) // Display the form
{
	?>
	<form class="contact-form" action="#contact-form" method="post" id="contact-form">
	<div class="input-item"><label>NAME</label><input type="text" name="name" value="<?php inpt_value('name', $fields); ?>" id="inpt-name"></div>
<?php show_error('name', $errors);?>
	<div class="input-item"><label>EMAIL</label><input type="email" name="email" value="<?php inpt_value('email', $fields); ?>" id="inpt-email"></div>
#MAD: Show the email error here
	<div class="textarea-item"><label>MESSAGE</label>
	<textarea name="message" id="txtarea"><?php inpt_value('message', $fields); ?></textarea></div>
#MAD: Show the message error here
	<input type="hidden" value="1" name="submit" />
	<div class="send-button"><button type="submit">SEND</button></div>
	</form>
	<!--  = /contact form =  -->
	<?php
}

In the code above, I put it immediately after the closing DIV tag for each item. I'm not sure if it belongs there or INSIDE the DIV - The significance (as far as I can tell) would be in how the CSS is written for this form.
Link to comment
Share on other sites

Hey buddy...

 

I altered this:

/* Add the appropriate class name to the specified input field */
function error_class($name, $errors) {
	if ( array_key_exists( $name, $errors ) ) {
		echo " error";
	}
}

To this:

/* Add the appropriate class name to the specified input field */
function error_class($name, $errors) {
	if ( array_key_exists( $name, $errors ) ) {
		echo $errors[$name];
	}
}

I then edited the print the form function to look like this:

<div class="input-item"><label>NAME</label><input type="text" name="name" value="<?php inpt_value('name', $fields); error_class('name', $errors); ?>" id="inpt-name"></div>

and it appears to work, if I use the show_error() function the div breaks the layout so I need to look over the HTML on that one.

 

I want to thank you for pointing me in the right direction, i appreciate it. :)

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.