Contact Form Question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

mjk87
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Apr 01, 2011 5:41 am

Contact Form Question

Postby mjk87 » Fri Apr 01, 2011 5:52 am

Greetings Guys(and Gals!)

First time on here so excuse me if I've not posted on the correct forum.

I've got a contact form, that works fine. The form has three required fields out of a possible five. When the required fields are not completed it sends you back to the original contact page, wiping the form clean. The issue is that the fields are wiped clear, so the user has no idea whether the form has sent or not. I built a work around to redirect to a page that says "Contact form Error etc." and that they should return to the form and try again.

I'm no PHP expert so is it possible to place it on the page with a warning sign saying complete required fields? Or could I use an alert box(personally I HATE this so try to avoid it)? My understanding is that this form will only work with this page because of the $formpage declaration?

Any help would be appreciated,

M

Html:

Code: Select all

<form action="php/contact.php" method="post" name="ContactForm">
                                <label>Name*:</label>
                                   <br /><input name="Name" type="text" />
                                <br /><label>Company:</label>
                                   <br /><input name="Company" type="text" />
                                <br /><label>Email*:</label>
                                   <br /><input name="Email" type="text" />
                                <br /><label>Phone:</label>
                                   <br /><input name="Phone" type="text" />
                                <br /><label>Enquiry*:</label>
                                   <br /><textarea name="Enquiry"></textarea>
                                <br /><input name="submit" type="submit" value="Submit" class="btn" />
                                *Required Field
                             </form>

PHP:

Code: Select all

<?

$recipient = 'info@email.co.uk' ;
$subject = "Contact Form Enquiry" ;
$formpage = "http://www.awebsite/contact.html" ;
$thankyoupage = "http://www.awebsite.co.uk/contactResponse.html" ;

$Name = $_POST['Name'] ;
$Company = $_POST['Company'] ;
$Email = $_POST['Email'] ;
$Phone = $_POST['Phone'] ;
$Enquiry = $_POST['Enquiry'] ;

if (empty($Name) || empty($Email) || empty($Enquiry)) {
header( "Location: http://www.awebsite.co.uk/contactFail.html" );
exit ;
}



$mailheaders="From: Contact Form Enquiry - $Email \r\n ";

$messageproper = "This message was sent from:\n" .
"Name: $Name\n" .
"Company: $Company\n" .
"Email: $Email\n" .
"Phone: $Phone\n" .

"------------------------- Enquiry -------------------------\n\n" .
$Enquiry .
"\n\n------------------------------------------------------------\n" ;

mail($recipient, $subject, $messageproper, $mailheaders );
header( "Location: $thankyoupage" );
exit ;

?>

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Contact Form Question

Postby Nullsig » Fri Apr 01, 2011 6:20 am

You could just do the verification before the form is sent with JavaScript like so:

Code: Select all

   
<html>
<head>
<script type="text/javascript">
function validate_required(field,alerttxt){
         with (field)
           {
           if (value==null||value=='')
            {
            alert(alerttxt);return false;
            }
           else
            {
            return true;
            }
           }
      }
      
      function validate_form(thisform)
      {
         with (thisform)
           {
           if (validate_required(name,'You must enter your name')==false)
           {name.focus();return false;}
           if (validate_required(Email,'You must enter your e-mail address')==false)
           {Email.focus();return false;}
                          if (validate_required(Enquiry,'You must supply an Enquiry')==false)
           {Enquiry.focus();return false;}
           }
      }
      </script>
   </head>
<body>
 <form action="php/contact.php" OnSubmit="return validate_form(this)" method="post" name="ContactForm">
                                    <label>Name*:</label>
                                       <br /><input name="Name" type="text" />
                                    <br /><label>Company:</label>
                                       <br /><input name="Company" type="text" />
                                    <br /><label>Email*:</label>
                                       <br /><input name="Email" type="text" />
                                    <br /><label>Phone:</label>
                                       <br /><input name="Phone" type="text" />
                                    <br /><label>Enquiry*:</label>
                                       <br /><textarea name="Enquiry"></textarea>
                                    <br /><input name="submit" type="submit" value="Submit" class="btn" />
                                    *Required Field
                                 </form>


Pardon the formatting but essentially what I did was took your code and added 2 JavaScript Functions. The first "validate_required" takes a form element and checks if data has been entered into it, if not it pops up a box with an error message and focuses on the offending form element.

The second "validate_form" takes a form as an argument and checks each element that is declared as required using the validate_required function.

There is a pure html solution for just checking if a form is blank but it has compatibility issues. This solution also allows you to validate elements with other functions. I use this basic function to validate zipcodes, phone numbers, and e-mail addresses using regular expressions just by having validate_form call other functions I write.


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron