Jump to content

Help with ajax and php contact form please


andrew_biggart

Recommended Posts

Ok I have been trying to get learn ajax and php contact forms for a few days now and I can't seem to get my head around why mine refuses to work. I know the form and the php does what it is suppost to do but I can't understand why when i link the whole thing together it doesn't work. As it sits at the moment its just sticking on the loading part of the ajax and refuses to execute the validate in the php file. Can anyone please point me in the right direction?

 

index.php (the form)

    	<div id="contact_wrapper">
        	<div id="contact_form">
            	<script src="js/ajax.form.js" language="javascript"></script>
                <form action="javascript:contact_form()" method="post">
                    <h1 class='contact_form_h' id='contact-loading'>Contact Us</h1>
                    <div id="login_response"></div>
                  <input type='text' name='name' id='name' class='contact_form_input' value='Name' onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" />
                  <input type='text' name='email' id='email' class='contact_form_input' value='Email' onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" />
                  <textarea name='enquiry' id='enquiry' class='contact_form_textarea' rows='10' cols='10' onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;">Enquiry</textarea>
                    <input type='submit' name='contact' id='contact' class='contact_form_submit' value='Contact Us' />
              </form>
            </div>
        </div>

 

 

ajax.form.js (Ajax)

/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();

/* -------------------------- */
/* Contact */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */

var nocache = 0;
function contact_form() {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('contact_wrapper').innerHTML = "<div id='contact_error'><img src='img/loader.gif' alt='loading' />     Loading...</div>"

// Required: verify that all fileds are not empty. Use encodeURI() to solve some issues about character encoding.
var name = encodeURI(document.getElementById('name').value);
var email = encodeURI(document.getElementById('email').value);
var enquiry = encodeURI(document.getElementById('enquiry').value);

// Set the random number to add to URL request
nocache = Math.random();

// Pass the form variables like URL variable
http.open('get', 'send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache);

http.onreadystatechange = Reply;
http.send(null);
}
function Reply() {
	if(http.readyState == 4){ 
	var response = http.responseText;

	if(response == 1){
	// if fields are empty
	document.getElementById('login_response').innerHTML = 'Please fill in all the fields.';
	}
	else if(response == 2){
	// if email isnt valid
	document.getElementById('login_response').innerHTML = 'Please enter a valid email address.';
	}
	else if(response == 3){
	// if email has been sent
	document.getElementById('login_response').innerHTML = 'Your email has been sent.';
	}
	else if(response == 10){
	// if email hasnt been sent
	document.getElementById('login_response').innerHTML = 'Your email has not been sent.';
	}

}
}

 

send_email.php (php)

		<?php

		//Require check email function
		require "check_email.php";

		//Variables
		$err_name=stripslashes($_GET['name']);
		$err_email=stripslashes($_GET['email']);
		$err_enquiry=stripslashes($_GET['enquiry']); 

		$to="xxx@xxxxxxxxxx.com";																												
		$subject="Website Contact Form";
		$from = stripslashes($_GET['name'])."<".stripslashes($_GET['email']).">";
		$message = $err_enquiry;
		$headers = "From: $from\r\n" .
		"MIME-Version: 1.0\r\n" .
		"Content-Type: multipart/mixed;\r\n" .
		" boundary=\"{$mime_boundary}\"";

		//Check all form fields are filled in

		if ($_GET["name"]!='' && $_GET["name"]!='Name' && $_GET["email"]!='' && $_GET["email"]!='Email' && $_GET["enquiry"]!='' && $_GET["enquiry"]!='Enquiry')  {


			//Check email address is valid
			if (isValidEmail($_GET['email'])){


				//Send Mail
				if (@mail($to, $subject, $message, $headers)) {
				echo "3";
				}
				else{
				echo "10";
				}
			}

			//Email isnt valid
			else{
			echo"2";
			}
		}


		else {
		echo"1";
		}


		?>

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.