Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Form Validation Feedback Problem

the mail() function

Moderators: macek, egami, gesf

Form Validation Feedback Problem

Postby ag555 » Thu May 31, 2012 1:20 pm

Hello,

I have created a form and the problem I am having is that if the user does not enter a required field, then they press submit and it tells them they missed the field out on a new page, therefore they have to click the back button.

I am wondering if it can be done in a way which the user would see that it has not been entered on the actual page, without taking them to a new one.

Below is the php file code:


Code: Select all
 <?php
/* Set e-mail recipient */
$myemail  = "random@email";


/* Check all form inputs using check_input function */
$firstname = check_input($_POST['firstname'], "Enter your first name");
$lastname  = check_input($_POST['lastname']);
$email    = check_input($_POST['email']);
$number  = check_input($_POST['number']);
$comments = check_input($_POST['comments'], "Write your comments");


// Where to redirect after form is processed.
$url = 'http://www.....com/thanks.html';

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



/* Let's prepare the message for the e-mail */
$message = "Message from the ... website enquiry form.

Your contact form has been submitted by:

First Name: $firstname
Last Name: $lastname
E-mail: $email
Number: $number


Comments:
$comments


Do you agree to the terms and conditions? $terms

End of message
";

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



/* Redirect visitor to the thank you page */
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
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>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>



And here is the HTML code for the form:

Code: Select all
<form action="enquiry_form.php" method="post">
<p><b>First Name:</b> <input type="text" name="firstname" /><br /><br />
Last Name: <input type="text" name="lastname" /><br /> <br />
<b>E-mail:</b> <input type="text" name="email" /><br /><br />
Number: <input type="text" name="number" /><br /><br />


<p><b>Your comments:</b><br /><br />
<textarea name="comments" rows="10" cols="40"></textarea></p><br /><br />

<p><b>Do you agree to the terms and conditions?</b>
<input type="radio" name="terms" value="Yes" checked="checked" /> Yes
<input type="radio" name="terms" value="No" /> No

<br /><br />

<p><input type="submit" value="Send"></p>

<p> </p>


</form>


The help would be much appreciated. Thanks!
ag555
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu May 31, 2012 10:38 am

Re: Form Validation Feedback Problem

Postby egami » Fri Jun 01, 2012 4:40 am

Sure.

You have form.html
and process.php

The form is displayed.
They fill out the form.
They submit the form, which goes to process.php


process.php takes all of the POST variables and checks them.
ie..
if (!isset($_POST['fullname'])) {
$error['fullname'] = "Full Name is required.";
} elseif (isset($_POST['fullname']) && $_POST['fullname'] != NULL || $_POST['fullname'] != '') {
$fullname = trim(mysql_real_escape_string(strip_tags($_POST['fullname'])));
} else {
$error['fullname'] = 'Full Name is required.";
}

.... do this for each one you want to check/clean..



if (isset($error)) {
include ('form.html');
die();
}





Your form.html should have traps to catch errors in it.. like so..

echo '
<form name="name" method="post" action="process.php">
<input type="text" name="fullname" value="'.@$fullname.'">'.((isset($error['fullname']) ? "$error[fullname]" : "").'<br />
</form>
';




Make sense?
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Form Validation Feedback Problem

Postby ag555 » Tue Jun 05, 2012 3:38 pm

Hey, thanks for the reply. I've had internet problems so not been able to post. :s

I tried this but it completely stops working when I use this method. I am not really a great programmer and this is my first time using PHP, so I am probably doing it wrong though :s

Thanks

A.
ag555
New php-forum User
New php-forum User
 
Posts: 2
Joined: Thu May 31, 2012 10:38 am

Re: Form Validation Feedback Problem

Postby egami » Thu Jun 07, 2012 4:55 am

parse through your error log file, and see where it's breaking, one fix at a time.
User avatar
egami
php-forum GURU
php-forum GURU
 
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron