Link to home
Start Free TrialLog in
Avatar of eggstoastbacon
eggstoastbacon

asked on

Simple Contact Form Script with Password Box

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

Avatar of h4hardy
h4hardy
Flag of United States of America image

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

Avatar of eggstoastbacon
eggstoastbacon

ASKER

The same problem, with or without the correct password I get "Robot Alert!" as if if ($_POST['norobots'] != $password) is always failing regardless.
ASKER CERTIFIED SOLUTION
Avatar of h4hardy
h4hardy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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



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!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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


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