Solved

Simple Contact Form Script with Password Box

Posted on 2011-09-14
8
440 Views
Last Modified: 2013-11-19
I am trying to create a simple password check on my contact form and I cannot seem to make it do anything but fail on the password check.

I have attached my php code and my form code, can someone tell me what I am doing wrong?

<?php


$password = "pass";



if ($_POST['norobots'] != $password)
{
	$error.="Robot Alert!";
        $errors=1;
}
if($errors==1) echo $error;
else{


	$values = array ('name','email','message');
	$required = array('name','email','message','norobots');
	 
	$your_email = "my@email.com";
	$email_subject = "New Message: ".$_POST['subject'];
	$email_content = "new message:\n";
	
	foreach($values as $key => $value){
	  if(in_array($value,$required)){
		if ($key != 'subject' && $key != 'company') {
		  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
		}
		$email_content .= $value.': '.$_POST[$value]."\n";
	  }
	}
	 
	if(@mail($your_email,$email_subject,$email_content)) {
		echo 'Message sent!'; 
	} else {
		echo 'ERROR!';
	}
}

?>

Open in new window

<form action="contact.php" method="post" id="contactform">
        <ol>
          <li>
            <label for="name">First Name<a href="#">*</a><br />
            </label>
            <input name="name" class="text" id="name" />
          </li>
          <li>
            <label for="email">Your e-mail<a href="#">*</a><br />
            </label>
            <input id="email" name="email" class="text" />
          </li>

          <li>
            <label for="subject">Subject<br />
            </label>
            <input id="subject" name="subject" class="text" />
          </li>
          <li>
            <label for="message">Message<a href="#">*</a><br />
            </label>
            <textarea id="message" name="message" rows="6" cols="50"></textarea>
            <br />

          </li>
           <li>
            <label for="norobots">Robot check<a href="#">*</a><br />
            </label>
            <input id="norobots" name="norobots" class="text" />
            <br />

          </li>
         
         
          
      
        


          <li class="buttons">
            <input type="image" name="imageField2" id="imageField2" src="images/but_send_message.gif" />
          </li>
        </ol>
      </form>

Open in new window

0
Comment
Question by:eggstoastbacon
  • 5
  • 2
8 Comments
 
LVL 4

Expert Comment

by:h4hardy
ID: 36535304
try with the below code..

 
<?php
$password = "pass";

if($_POST)
{
	if ($_POST['norobots'] != $password)
	{
		$error.="Robot Alert!";
		$errors=1;
	}
	if($errors==1) echo $error;
	else{


		$values = array ('name','email','message');
		$required = array('name','email','message','norobots');
		 
		$your_email = "my@email.com";
		$email_subject = "New Message: ".$_POST['subject'];
		$email_content = "new message:\n";
		
		foreach($values as $key => $value){
		  if(in_array($value,$required)){
			if ($key != 'subject' && $key != 'company') {
			  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
			}
			$email_content .= $value.': '.$_POST[$value]."\n";
		  }
		}
		 
		if(@mail($your_email,$email_subject,$email_content)) {
			echo 'Message sent!'; 
		} else {
			echo 'ERROR!';
		}
	}
}

?>

Open in new window

0
 

Author Comment

by:eggstoastbacon
ID: 36535333
The same problem, with or without the correct password I get "Robot Alert!" as if if ($_POST['norobots'] != $password) is always failing regardless.
0
 
LVL 4

Accepted Solution

by:
h4hardy earned 250 total points
ID: 36535406
may be you leaving the space when you type the password in textbox.. try with the below things

1. replace the  if  "trim($_POST['norobots'])!=$password) "

2. check the wat you post so for sure just print the posted value and check by  

<?php print_r($_POST);?>

still you have issue post the modified code here will check.
      
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:eggstoastbacon
ID: 36535523
Please let me know the full code you want me to try so I can be sure I am doing it correctly.

Below is what I have done;
I get this error now:
Parse error: syntax error, unexpected T_STRING, expecting '(' in /home1/aleapahe/public_html/contact.php on line 6

<?php
$password = "pass";

if($_POST)
{
	if trim($_POST['norobots'])!=$password) "
	{
		$error.="Robot Alert!";
		$errors=1;
	}
	if($errors==1) echo $error;
	else{


		$values = array ('name','email','message');
		$required = array('name','email','message','norobots');
		 
		$your_email = "my@email.com";
		$email_subject = "New Message: ".$_POST['subject'];
		$email_content = "new message:\n";
		
		foreach($values as $key => $value){
		  if(in_array($value,$required)){
			if ($key != 'subject' && $key != 'company') {
			  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
			}
			$email_content .= $value.': '.$_POST[$value]."\n";
		  }
		}
		 
		if(@mail($your_email,$email_subject,$email_content)) {
			echo 'Message sent!'; 
		} else {
			echo 'ERROR!';
		}
	}
}

?>

Open in new window


I get this error now:
Parse error: syntax error, unexpected T_STRING, expecting '(' in /home1/aleapahe/public_html/contact.php on line 6



0
 

Author Comment

by:eggstoastbacon
ID: 36535561
Here is my error, it looks like it's not even reading my "norobots" form field.

Array ( [name] => eggstoast [email] => my@email.com [company] => undefined [subject] => test [message] => message ) Robot Alert!
0
 
LVL 3

Assisted Solution

by:garykristen
garykristen earned 250 total points
ID: 36535582
Hi eggstoastbacon,

try with this code



<?php
$password = "pass";

if($_POST)
{
	if(trim($_POST['norobots'])!=$password))
	{
		$error.="Robot Alert!";
		$errors=1;
	}
	if($errors==1) echo $error;
	else{


		$values = array ('name','email','message');
		$required = array('name','email','message','norobots');

		$your_email = "my@email.com";
		$email_subject = "New Message: ".$_POST['subject'];
		$email_content = "new message:\n";

		foreach($values as $key => $value){
		  if(in_array($value,$required)){
			if ($key != 'subject' && $key != 'company') {
			  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
			}
			$email_content .= $value.': '.$_POST[$value]."\n";
		  }
		}

		if(@mail($your_email,$email_subject,$email_content)) {
			echo 'Message sent!';
		} else {
			echo 'ERROR!';
		}
	}
}

?>

Open in new window

0
 

Author Comment

by:eggstoastbacon
ID: 36535603
Array ( [name] => eggstoast [email] => my@mail.com [company] => undefined [subject] => my subject [message] => my message ) Robot Alert!

I did remove a ")" from the code to avoid syntax error and used print_r($_POST); to see what I am passing. The script is still entirely missing or ignoring the "norobots" part of my form.

<?php
$password = "pass";

if($_POST)
{
	if(trim($_POST['norobots'])!=$password)
	{
		$error.="Robot Alert!";
		$errors=1;
                print_r($_POST);

	}
	if($errors==1) echo $error;
	else{


		$values = array ('name','email','message');
		$required = array('name','email','message','norobots');

		$your_email = "my@email.com";
		$email_subject = "New Message: ".$_POST['subject'];
		$email_content = "new message:\n";

		foreach($values as $key => $value){
		  if(in_array($value,$required)){
			if ($key != 'subject' && $key != 'company') {
			  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
			}
			$email_content .= $value.': '.$_POST[$value]."\n";
		  }
		}

		if(@mail($your_email,$email_subject,$email_content)) {
			echo 'Message sent!';
		} else {
			echo 'ERROR!';
		}
	}
}

?>

Open in new window


0
 

Author Comment

by:eggstoastbacon
ID: 36535644
I figured it out I needed to add my norobots to my header code. Thanks for helping me narrow it down.

jQuery(document).ready(function(){
	$('#contactform').submit(function(){				  
		var action = $(this).attr('action');
		$.post(action, { 
			name: $('#name').val(),
			email: $('#email').val(),
			company: $('#company').val(),
			subject: $('#subject').val(),
			message: $('#message').val(),
			norobots: $('#norobots').val()
		},
			function(data){
				$('#contactform #submit').attr('disabled','');
				$('.response').remove();
				$('#contactform').before('<p class="response">'+data+'</p>');
				$('.response').slideDown();
				if(data=='Message sent!') $('#contactform').slideUp();
			}
		); 
		return false;

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question