Jump to content

Contact form required fields


dgow13

Recommended Posts

On my contact us page I have a form that, when submitted, sends visitor to thank you page. I am not great at php so, how do I make all or some of the fields in the contact form required? script from both pages is below...

 

From contact us page:

 

<div class="contact_form" style="left: 578px; top: 318px">

<form method="post" action="thankyou.php">

<input name="first_name" style="width: 200px" type="text" /><br />

<br />

<input name="last_name" style="width: 200px" type="text" /><br />

<br />

<input name="email" style="width: 200px" type="text" /><br />

<br />

<input name="phone" style="width: 200px" type="text" /><br />

<br />

<input name="radio" value="Website Design and Development"type="radio" /><span class="style3"><strong>

Website Design and Development<br />

<input name="radio" value="Graphic and Logo Designs" type="radio" /> Graphic and Logo Designs<br />

<input name="radio" value="Animation" type="radio" /> Animation<br />

<input name="radio" value="Web Hosting" type="radio" /> Web Hosting<br />

<input name="radio" value="Domain Name Registration" type="radio" /> Domain Name Registration<br />

<input name="radio" value="Search Engine Optimization" type="radio" /> Search Engine Optimization<br />

<input name="radio" value="Business Card Print Media Designs" type="radio" /> Business Card Print Media

Designs<br />

<input name="radio" value="General Questions" type="radio" /> General Questions<br />

<br />

</strong></span>

<textarea name="message" style="width: 300px; height: 40px"></textarea><br />

<br />

<input name="Submit" style="width: 100px" type="submit" value="Submit" />  

<input name="Reset" style="width: 100px" type="reset" value="Reset" />

</form>

</div>

 

From thank you page:

 

 

<?php

//php form data to email code//

$first_name = $_REQUEST['first_name'];

$last_name = $_REQUEST['last_name'];

$email = $_REQUEST['email'];

$phone = $_REQUEST['phone'];

$radio = $_REQUEST['radio'];

$message = $_REQUEST['message'];

$headers = "From: $email";

 

$email_message = "First Name: {$first_name}\n\rLast Name: {$last_name}\n\rEmail: {$email}\n\rPhone: {$phone}\n\rInterested In: {$radio}\n\rMessage: {$message}";

mail('dgow@simplewebs13.com',$radio,$email_message,$headers);

?>

Link to comment
Share on other sites

theres a few ways to go about something like this.

 

I'd probably use something along the lines of

 

if (!isset($_REQUEST['first_name'])) {

$errormessage = "Please fill in the name field";

} else {

$first_name = $_REQUEST['first_name'];

....

 

you could elaborate on the if statement to include the required fields by using '&&' (ex. (!isset($_REQUEST['first_name'])) && (!isset($_REQUEST['last_name'])) && ect.. , and echo out the customer error message(s) depending on what wasn't filled in.

 

Using the '!isset' method is like saying "if (is not set) then do this or else this". I find its best to treat things as guilty before proved innocent in php.

 

There's a much easier way to do this in the Zend Framework / Zend Forms, and that is what I am more accustomed to nowadays, so someone else might have a better way than what I have said.

Link to comment
Share on other sites

in the php file where your declaring the values.

 

 

if (!isset($_REQUEST['first_name'] && $_REQUEST['last_name'] && $_REQUEST['email'] &&

$_REQUEST['phone'] && $_REQUEST['radio'] && $_REQUEST['message'])) {

$errormessage = 'some generic message here about not filling all required fields'; // you can check them all separately instead for a unique error message depending on which one wasn't filled in.

 

} else {

 

$first_name = $_REQUEST['first_name'];

$last_name = $_REQUEST['last_name'];

$email = $_REQUEST['email'];

$phone = $_REQUEST['phone'];

$radio = $_REQUEST['radio'];

$message = $_REQUEST['message'];

....

}

Link to comment
Share on other sites

Ok. I copied and pasted your suggestion in the correct place, it's where I thought it should actually go, but it did not work. When I leave the first name area blank on the contact us page and then click "Submit", it brings up a blank page.

Here is the updated code:

 

 

<?php

//php form data to email code//

 

if (!isset($_REQUEST['first_name'] && $_REQUEST['last_name'] && $_REQUEST['email'] &&

$_REQUEST['phone'] && $_REQUEST['radio'] && $_REQUEST['message'])) {

$errormessage = 'Please fill in the required fields';

 

} else {

 

$first_name = $_REQUEST['first_name'];

$last_name = $_REQUEST['last_name'];

$email = $_REQUEST['email'];

$phone = $_REQUEST['phone'];

$radio = $_REQUEST['radio'];

$message = $_REQUEST['message'];

$headers = "From: $email";

 

$email_message = "First Name: {$first_name}\n\rLast Name: {$last_name}\n\rEmail: {$email}\n\rPhone: {$phone}\n\rInterested In: {$radio}\n\rMessage: {$message}";

mail('dgow@simplewebs13.com',$radio,$email_message,$headers);

?>

Link to comment
Share on other sites

I haven't tested any of this to ensure it works properly. And normally I wouldn't be throwing echo's to the screen like this, but like I said, I am used to Zend Framework where I can utilize the views a little differently, but this might be what your looking for, at least the barebones.

 

<?php

//php form data to email code//

 

if (!isset($_REQUEST['first_name'] && $_REQUEST['last_name'] && $_REQUEST['email'] &&

$_REQUEST['phone'] && $_REQUEST['radio'] && $_REQUEST['message'])) {

$errormessage = 'Please fill in the required fields';

echo $errormessage;

 

} else {

 

$first_name = $_REQUEST['first_name'];

$last_name = $_REQUEST['last_name'];

$email = $_REQUEST['email'];

$phone = $_REQUEST['phone'];

$radio = $_REQUEST['radio'];

$message = $_REQUEST['message'];

$headers = "From: $email";

 

$email_message = "First Name: {$first_name}\n\rLast Name: {$last_name}\n\rEmail: {$email}\n\rPhone: {$phone}\n\rInterested In: {$radio}\n\rMessage: {$message}";

mail('dgow@simplewebs13.com',$radio,$email_message,$headers);

 

echo "Successfully submitted!";

?>

Link to comment
Share on other sites

Still getting blank page. The script is on the thank you page and not really associated with the contact us page. To me, if a field is not filled in, once you hit submit it has no idea that you haven't filled in all the required fields. I would think, what little knowledge I have about script, that the required info needs to be on the page that is getting filled out.

Link to comment
Share on other sites

Hi,

You can use javascript validation in that case.

Here is a sample for javascript validation.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
function Formvalidation(form){
if(form.fname.value==""){
	alert("Please Enter First Name");
	form.fname.focus();
	return false;
}

if(form.lname.value==""){
	alert("Please Enter Last Name");
	form.lname.focus();
	return false;
}

//if(form.otherfield.value=="")
//continue.................
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
 <table width="100%" border="0" cellspacing="0" cellpadding="3">
   <tr>
     <td width="23%">First Name</td>
     <td width="77%"><label for="name"></label>
     <input type="text" name="fname" id="fname" /></td>
   </tr>
   <tr>
     <td>Last Name</td>
     <td><label for="email"></label>
     <input type="text" name="lname" id="lname" /></td>
   </tr>
   <tr>
     <td><input type="submit" name="button" id="button" value="Submit" onclick="return Formvalidation(this.form);"/></td>
     <td> </td>
   </tr>
 </table>
</form>
</body>
</html>

Link to comment
Share on other sites

I just added a 5 part video tutorial to the KS University covering Javascript/PHP based form validation. Take a look if you're a KS University subscriber and think it might help.

 

In regards to Bishwadeep's comments above, using Javascript is fine (I'm a fan of the jQuery Validate plugin: http://docs.jquery.com/Plugins/Validation) but keep in mind that it doesn't mean that you shouldn't have PHP validation as well. Javascript can easily be turned off by the visitor, and you don't want validation to be disabled if that is the case.

Link to comment
Share on other sites

  • 2 weeks later...

I finally had time to review those video tutorials you suggested... excellent. Just what I needed. Worked great and now I have the validation I wanted for my contact form.

 

I must say... all of your videos are really user friendly, easy to understand and follow along with for those of us that are not experts. Well worth the monthly cost of being involved in the university. Thank again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...