Hash Change on Forum

I got it working :)I had to remove the action to go to the thank-you.html page.

I have a question, I’m not receiving any emails ! I’m current looking into this so I may solve before you read this but maybe not.

I did a PHP test and that worked. My only conclusion has to do with the form recipient but their is only one option for the email address.

It might be worthwhile just displaying what is return by submit.php

$("#contact_form").before('<p>' + data + '</p>');

Sorry that is not right. The data will be either

A -1 will indicate an error with the form validation.

A 1 will indicate success, but if you are not receiving the email then it will indicate an error with the code sending the email.

Since you are getting the Thank you message we know that it is 1. Therefore the problem must be with the email sending. That part of the script has a statement

$mail->AddAddress($emailAddress);

Silly question : Do have you got $emailAddress defined to your email address?

No, I was going to place it between one of these lines, but I don’t think it may be effective.

	$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
		$body = "A user  $name submitted the contact form:\
".
		"Name: $name\
".
		"Email: $visitor_email \
".

I checked again and I have

$mail->AddAddress($emailAddress);

located in submit.php.

I thought I solved it now I’m getting another PHP error.

Philip, I assume I can style the gray box that with the thank you message ? What about a redirect after 5-6 seconds to another part of my page ?

I thought I solved it now I’m getting another PHP error.

Have you resolved your PHP problem yet? Do you want to post your submit.php code on this post?

$mail->AddAddress($emailAddress);

It was the presence of an assignment to $emailAddress that I was concerned about.

The PHPmailer has a config.php file where the configuration parameters such as the smtp server are defined. Have you set up this file correctly?

Have you resolved your PHP problem yet? Do you want to post your submit.php code on this post?

I’ve fixed one problem, but the PHP error persists. Here is the error I currently get;

Warning: require(phpmailer/class.phpmailer.php) [function.require]: failed to open stream: No such file or directory in /home/thecreat/public_html/construction/submit.php on line 2

Here is the file submit.php.


<?php
require "phpmailer/class.phpmailer.php";

session_name("fancyform");	/* starting the session */
session_start();

foreach($_POST as $k=>$v)
{
	/* if magic_quotes is enabled, strip the post array */
	if(ini_get('magic_quotes_gpc'))
	$_POST[$k]=stripslashes($_POST[$k]);

	$_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
	/* escape the special chars */
}

$err = array();

/* some error checks */
if(!checkLen('name'))
	$err[]='The name field is too short or empty!';

if(!checkLen('email'))
	$err[]='The email field is too short or empty!';
else if(!checkEmail($_POST['email']))
	$err[]='Your email is not valid!';

if(!checkLen('subject'))
	$err[]='You have not selected a subject!';

if(!checkLen('message'))
	$err[]='The message field is too short or empty!';

/* compare the received captcha code to the one in the session array */
if((int)$_POST['captcha'] != $_SESSION['expect'])
	$err[]='The captcha code is wrong!';

/* if there are errors */
if(count($err))
{
	/* if the form was submitted via AJAX */
	if($_POST['ajax'])
	{
		echo '-1';
	}

	/* else fill the SESSION array and redirect back to the form */
	else if($_SERVER['HTTP_REFERER'])
	{
		$_SESSION['errStr'] = implode('<br />',$err);
		$_SESSION['post']=$_POST;

		header('Location: '.$_SERVER['HTTP_REFERER']);
	}

	exit;
}

/* the email body */
$msg=
'Name:	'.$_POST['name'].'<br />
Email:	'.$_POST['email'].'<br />
IP:	'.$_SERVER['REMOTE_ADDR'].'<br /><br />

Message:<br /><br />

'.nl2br($_POST['message']).'

';

$mail = new PHPMailer();	/* using PHPMailer */
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "A new ".mb_strtolower($_POST['subject'])." from ".$_POST['name']." | contact form feedback";

$mail->MsgHTML($msg);

$mail->Send();

unset($_SESSION['post']);	/* unsetting */

/* the form was successfully sent */
if($_POST['ajax'])
{
	echo '1';
}
else
{
	$_SESSION['sent']=1;

	if($_SERVER['HTTP_REFERER'])
		header('Location: '.$_SERVER['HTTP_REFERER']);

	exit;
}

/* some helpful functions */
function checkLen($str,$len=2)
{
	return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
	return preg_match("/^[\\.A-z0-9_\\-\\+]+[@][A-z0-9_\\-]+([.][A-z0-9_\\-]+)+[A-z]{1,4}$/", $str);
}
?>

The configuration file for the form does not have any SMTP settings.

I would recommend that you post this as a problem on the PHP forum. Somebody there will undoubtedly be able to advise you better.

Philip I’m in the process but can you remove the gray background where the line ‘thank you message sent’ appears?

the grey background is from the “background” style in

#Contact {
	background: #C0C0C0;
	overflow: hidden;
	width: 650px;
	margin: 40px 35px 0 300px;
}

changing it to

background: #000000;

should resolve the problem

Philip, I need to have the form check that the fields are filled in before sending the form then displaying a thank you message, if a field is not filled in the form can’t be sent.

Try replacing the javascript we added early with

<script type="text/javascript">
<!--
$(document).ready(function(){

		// Code for validation of form content
		var name = $("#idname");
		var email = $("#idemail");
		var message = $("#idmessage");
		var captcha = $("#id6_letters_code");

		name.blur(validateName);
		email.blur(validateEmail);
		message.blur(validateMessage);
		captcha.blur(validateCaptcha);

		function validateName() {
			var a = name.val();
			if (a.length < 3) {
				name.focus();
				alert("Please supply your name");
				return false;
			}
			return true;
		}

		function validateEmail() {
			var a = email.val();
			if (a.length == 0) {
				email.focus();
				alert("An Email address must be supplied");
				return false;
			}
			if (!/^[\\w\\.\\-]+@([\\w\\-]+\\.)+[a-zA-Z]+$/.test(a)) {
				email.focus();
				alert("Email address is invalid");
				return false;
			}
			return true;
		}


		function validateMessage() {
			var a = message.val();
			if (a.length < 10) {
				message.focus();
				alert("Please complete the message field");
				return false;
			}
			return true;
		}

		function validateCaptcha() {
			var a = captcha.val();
			if (a.length != 6) {
				captcha.focus();
				alert("Captcha code is invalid");
				return false;
			}
			return true;
		}


		// End of Code for validation of form content


    $("#contact_form").submit(function(e){

		// Plus validation in submit
		var invalid = !(validateName() && validateEmail() && validateMessage() && validateCaptcha());
		if (invalid) return e.preventDefault();

        $.post('/construction/submit.php', $(this).serialize(),
            function(data){
                var message;
                if(parseInt(data)==-1)
                    message = "error";
                else
                {
                    $("#contact_form").hide('slow');
                    message = "Thank your for the message";
                }
                $("#contact_form").before('<p>' + message + '</p>');
            }
        );
        e.preventDefault();
    });
});
// -->
</script>

Your html for the form should become

<form method="POST" id="contact_form" action="">
<div class="name">
<label for="idname">Name: </label>
<input type="text" name="name" id="idname" value="">
</div>
<div class="name">
<label for="idemail">Email: </label>
<input type="text" name="email" id="idemail" value="">
</div>
<div class="message">
<label for="idmessage">Message:</label>
<textarea name="message" id="idmessage" rows="5" cols="30"></textarea>
</div>
<div class="captcha"><img src="./site_construction_a.php_files/captcha_code_file.php" id="captchaimg"></div>
<div class="capcode">
<label for="id6_letters_code">Enter (above) code here :</label>
<input id="id6_letters_code" name="6_letters_code" type="text">
</div>
<div class="submit">
<input type="submit" value="Submit" name="submit">
</div>
</form>

Hi, that works but could the notification be on the right hand side of each of the form fields then highlight the form field ? Also the CAPTCHA is not displaying, although it’s in the HTML, unless I’m over looking something ?

I looked over the HTML and fixed what I could but the CAPTCHA continues not to show on the page.

What is going on with this line;

(!/^[\\w\\.\\-]+@([\\w\\-]+\\.)+[a-zA-Z]+$/.test(a)) { 

I did a save as from your original page to develop the new code, this constructs a sub directory and puts associated files in it. I have then cut and pasted the html code which is where the problem has crept in.

The src attribute of the img tag needs to be changed to reflect where it is on your site.

Hi, Philip could you past that part of the code so I can see which path it is, I’ve already tried a few but it’s not working :slight_smile:

Also would it be possible to get the form fields that are not filled out not to have a pop up but instead on the right hand side of the form a text message and the form hightlights in red ?

Philip I have solved the CAPTCHA problem. There remains something, the javascript is not submitted the form data using ajax as it should. If you want a link to the page in progress here.