Solved

Simple Contact Form Script with Password Box

Posted on 2011-09-14
8
418 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:eggstoastbacon
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:eggstoastbacon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now