Jump to content

help with my contact form script


CyberShot

Recommended Posts

Here is the html for the form. When I push the submit button, it just goes to a white screen. Its not refreshing the screen back to the index which is the last line of code

 

<form method="post" action="action.php">
		<h3 id="contactIcon">Contact us</h3>
			<div id="col1">
				<label for="name">Name</label><br /><input type="text" id="name" name="name" /><br />
				<label for="email">Email</label><br /><input type="text" id="email" name="subject" /><br />
			</div><!--END col1-->

			<div id="col2">
				<label id="comments" for="comments">Comments</label><br /><textarea rows="6" cols="15" name="comments"></textarea>
			</div><!--END col2-->
			<div class="clear"></div>
			<input id="send" type="submit" name="submit" value=""  />
		</form>

 

and the php

 

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "my_email.com";
$email_subject = "test email";


function died($error) {
	// your error code can go here
	echo "We are very sorry, but there were error(s) found with the form your submitted. ";
	echo "These errors appear below.<br /><br />";
	echo $error."<br /><br />";
	echo "Please go back and fix these errors.<br /><br />";
	die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
	!isset($_POST['email']) ||
	!isset($_POST['comments'])) {
	died('We are sorry, but there appears to be a problem with the form your submitted.');		
}

$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
  	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  
  if(strlen($comments) < 2) {
  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  
  if(strlen($error_message) > 0) {
  	died($error_message);
  }
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<meta http-equiv="Refresh" content="0;URL=index.html" />

<?
}
?>

Link to comment
https://forums.phpfreaks.com/topic/198944-help-with-my-contact-form-script/
Share on other sites

Try this

<?php
if(isset($_POST['submit'])) {
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "my_email.com";
    $email_subject = "test email";
   
    $first_name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $comments = $_POST['comments']; // required
   
    // validation expected data exists
    if(empty($first_name) || empty($email_from) || empty($comments)) {
       $error_message[] = 'You must fill in all required fields!';      
    } else {
      $error_message = '';
      $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
      if(!eregi($email_exp,$email_from)) {
         $error_message[] = 'Your Email Address includes invalid characters!.<br />';
      }
      
      $string_exp = "^[a-z .'-]+$";
      if(!eregi($string_exp,$first_name)) {
         $error_message[] = 'Your First Name includes invalid characters! <br />';
      }
    
      if(strlen($comments) < 2) {
         $error_message[] = 'The Comments you entered do not appear to be valid.<br />';
      }
      
      if(strlen($error_message) > 0) {
          echo "We are very sorry, but there were error(s) found with the form your submitted. ";
         echo "These errors appear below.<br /><br />";
         foreach($error_message as $error) {
            echo $error."<br /><br />";
         }
         echo "Please go back and fix these errors.<br /><br />";
         die();
      } else {
         $email_message = "Form details below.\n\n";
    
         function clean_string($string) {
            $bad = array("content-type","bcc:","to:","cc:","href");
            return str_replace($bad,"",$string);
         }
   
         $email_message .= "First Name: ".clean_string($first_name)."\n";
         $email_message .= "Email: ".clean_string($email_from)."\n";
         $email_message .= "Comments: ".clean_string($comments)."\n";
   
   
         // create email headers
         $headers = 'From: '.$email_from."\r\n".
         'Reply-To: '.$email_from."\r\n" .
         'X-Mailer: PHP/' . phpversion();
         $mail = @mail($email_to, $email_subject, $email_message, $headers);
         if($mail) {
            header("LOCATION: /index.php");
         } else {
            echo 'Something appeared to have gone wrong, contact the administrator';
         }
      }
   }
}
?>

yes, but I am messing with two different scripts right now. this one I have been using but for some reason just stopped working for this site. I do get emails from it but they are blank. Here is the error message I got from it

 

 

 

Warning: reset() [function.reset]: Passed variable is not an array or object in /home7/public_html//test/action.php  on line 14

Warning: Variable passed to each() is not an array or object in /home7/public_html/test/action.php on line 16

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<TITLE>thanks</TITLE>

</HEAD>
<BODY style="background-color : transparent;" >
<?PHP
$email = $HTTP_POST_VARS[email];
$mailto = "my-email.net";
$mailsubj = "information from contact page";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values submitted from contact page:\n";
while (list ($key, $val) = each ($HTTP_POST_VARS))
{
if ($key!="submit")
{
$mailbody .= "$key : $val\n";
}
}
mail($mailto, $mailsubj, $mailbody, $mailhead);
?>



<META HTTP-EQUIV="REFRESH" CONTENT="100;URL=index.html"> 



</dIV>
</BODY>
</HTML>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.