Jump to content

Simple task frying my brain.


andrew_biggart

Recommended Posts

Ok this should be straight forward but my tired brain cannot work this out. I'm creating a simple contact form and for some reason the validation isn't working. It wont go past the check fields are filled in validation. Can anyone spot what I'm doing wrong?

 

                <form method="get">
                  <h1 class='contact_form_h'>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>

		<?php
			//Check if form is submitted
			if(isset($_GET['contact'])) {

			//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@xxxx.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"]!='' OR $_GET["name"]!='Name' OR $_GET["email"]!='' OR $_GET["email"]!='Email' )  {

					if (isValidEmail($_GET['email'])){

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

					}
					else {
					echo "1 - not valid";
					}

				}
				else {
				echo"0 - Fill in";
				}


			}	
		?>

 

 

Below is the check email script.

<?php
   // This function tests whether the email address is valid  
   function isValidEmail($email){
      $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
     
      if (eregi($pattern, $email)){
         return true;
      }
      else {
         return false;
      }   
   }
?>  

 

 

 

Link to comment
Share on other sites

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

 

You should use "&&" (or "AND") here, as you want to only continue if ALL of the checks pass. At the moment only one has to evaluate to true for the inputs to be considered valid. Also instead of checking for an empty string like that, use the empty() function:

 

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

 

Currently if $_GET['name'] or $_GET['email'] do not exist, you'll get a PHP error notice. empty() performs an implicit isset() check, so you're checking that it exists, and that it's not empty, in one go.

 

Which of the errors do you get back?

Link to comment
Share on other sites

Yea I noticed that as I posted it. I currently have this.

		<?php
			//Check if form is submitted


			//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="andrew@peppermintdigital.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' )  {

					if (isValidEmail($_GET['email'])){

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

					}
					else {
					echo "1";
					}

				}
				else {
				echo"0";
				}



		?>

 

The problem I am having now is with the ajax part of the form. I have tested the php and it now works but when the form is hooked up by ajax its getting stuck.

 

Here is the ajax part of the form which alway echo's the please fill in all form fields error.

 

/* ---------------------------- */
/* 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();

/* -------------------------- */
/* LOGIN */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function send_email() {
// Optional: Show a waiting message in the layer with ID ajax_response
document.getElementById('login_response').innerHTML = "Loading..."
// Required: verify that all fileds is 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 te random number to add to URL request
nocache = Math.random();
// Pass the login 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 == 0){
	// if fields are empty
	document.getElementById('login_response').innerHTML = 'Please fill in all the fields.';
	}
	else if(response == 1){
	// if email isnt valid
	document.getElementById('login_response').innerHTML = 'Please enter a valid email address.';
	}
	else if(response == 2){
	// if email has been sent
	document.getElementById('login_response').innerHTML = 'Your email has been sent.';
	}
	else if(response == 3){
	// if email hasnt been sent
	document.getElementById('login_response').innerHTML = 'Your email has not been sent.';
	}	
}
}

 

 

Link to comment
Share on other sites

Before:

 

http.open('get', 'send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache);

 

Add:

 

alert('send_email.php?name='+name+'&email='+email+'&enquiry='+enquiry+'&nocache = '+nocache);

 

Check to make you're passing in the data you expect. That will narrow the problem down to between the JavaScript or PHP.

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.