Solved

Simple Contact Form Script with Password Box

Posted on 2011-09-14
8
462 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

623 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