Solved

Simple Contact Form Script with Password Box

Posted on 2011-09-14
8
447 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

839 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