Passing Current Value from Get Method Contact Form

Hi all,

I have asked for help concerning a jQuery/PHP contact form previously, but that thread fell idle when no one continued to give suggestions. I’ve still been working on this and will solve this on my own even if I receive no further replies.

I am seeing a response posted to the div with the id of status, but it is obviously not receiving the correct data input for the e-mail. I have set it up to echo the e-mail address value currently in there, and every time the PHP script is receiving “your e-mail” as the data from that input; therefore, the result echoed is always “Submission Failure- Nothing in Box: your e-mail”.

This is the form markup:


 <form id="emailbox" name="form1" method="get" action="Scripts/emailtester.php">
        <div>
          <input type="text" name="email" id="go" value="your e-mail" onclick="input_focus(this)"  onblur="input_reset(this)" maxlength="60"/>
          <input type="submit" id="submit" value="Join" />
        </div>
      </form>

The jQuery:


jQuery(document).ready(function(){
	jQuery("#emailbox").submit(function(event){
		event.preventDefault();
		var ea = $("#go").val();
		
		$.ajax({
    		url: $(this).attr('action'),
			type: $(this).attr('method'),
			data: $(this).serialize(),
			success: function(response){
				$('#status').append(response);
			}
			});
	});
});

emailtester.php


<?php
		require_once('checkfirstsub.php');
		$email = $_GET['email'];
		if (!isset($email) || $email == "your e-mail")
			echo '<p>Submission Failure - Nothing in Box:  ' . $email . '</p>';
		else {
			$email = htmlentities($email);
			$obj_PE = new ProcessEmail();
			$messages = $obj_PE -> processor($email);
			echo $messages;	
		}
?>

checkfirstsub.php


<?php
    class ProcessEmail
    {	
        public function processor($email)
        {
			if (!empty($email)){
				if ($email != "your e-mail"){
						if (isItAValidEmail($email))
						{
							return '<p>Submission Successful</p>';
						} else {
							return '<p>Submission Failure- Invalid E-mail</p>';
						}
				} else {
					return '<p>Submission Failure- Default Value in Box</p>';
				}
			} else {
				return '<p>Submission Failure- Nothing in Box</p>';
			}
        }
        
        public function isItAValidEmail($email)
        {
            if (filter_var($email, FILTER_VALIDATE_EMAIL))
                return true;
            else 
                return false;
        }
    }
?>

If it helps to figure out why “your e-mail” is being sent to the PHP script, below I’ve posted a JavaScript snippet that changes the input value from “your e-mail” to blank when the input element is activated.


function input_focus(obj){
	if ( obj.value == obj.defaultValue ){
		obj.value = ""
	}
}

function input_reset(obj){
	obj.value = obj.defaultValue;
}

Hopefully someone can point me in the right direction.

Okay so the problem lies in checkfirstsub.php

You need to use $this->isItAValidEmail($email);

so your code for checkfirstsub.php look like this below, tested and works for my email account


<?
    class ProcessEmail
    {    
        public function processor($email)
        {
            if (!empty($email)){
                if ($email != "your e-mail"){
                        if ($this->isItAValidEmail($email))
                        {
                            return '<p>Submission Successful</p>';
                        } else {
                            return '<p>Submission Failure- Invalid E-mail</p>';
                        }
                } else {
                    return '<p>Submission Failure- Default Value in Box</p>';
                }
            } else {
                return '<p>Submission Failure- Nothing in Box</p>';
            }
        }
        
        public function isItAValidEmail($email)
        {
            if (filter_var($email, FILTER_VALIDATE_EMAIL))
                return true;
            else 
                return false;
        }
    }
?>

when working with classes within php, you need to use $this to access the classes objects from within the class itself. For instance


<?
$animal = new dog;

echo $animal->get_breed();
echo $animal->call_bark();
echo $animal->call_speak();
echo $animal->get_coat();
class dog
{
   public $breed = 'Boxer';
   public $coat = 'Brindle';
   public $speak = 'Wolf';
   public $bark = 'Bow Wow Wow';
   public function call_bark()
   {
       return $bark;
   }
   
   
   public function call_speak()
   {
        return $this->speak();
   }
   //calling this function will return 'Boxer'
   public function get_breed()
   {
        return $this->breed;
   }
   
   
   //call this function will return an error Undefined Variable
   public function get_coat()
   {
       return $coat;
   }
   
   //calling this function set the name of the class object breed.
   public function set_breed($set)
   {
       $this->breed = $set;
       return $this->breed;
       
   }
   
   //calling this function will set the variable $coat inside this particular
   //function to whatever you set it to but will not affect the class object
   //coat. So you will not be getting the desired functionality.
   public function set_coat($set)
   {
       $coat = $set;
       return $this->coat;
   }
   
   public function speak()
   {
       return 'Woof Woof';
   }
   
   public function bark()
   {
       return 'Woof Woof';
   }
}
?>

Temporarily remove the jQuery code and get the html form / PHP handler working.

Then, when you have that working, add the jQuery back in.

Thanks for both of your proposed solutions, especially robawtic.

The actual solution, however, occurred to me. There was an onblur attribute that instantiated one of those JavaScript methods set for that e-mail input, and once you submit your e-mail, it would reset its value to “your e-mail” every time. Removing that onblur attribute has solved the problem.

I should leave this thread open because the next step in this process is processing the second form that I’m about to program to pop up when the submission is successful. I’m sure I’ll stumble across some similar issue at some point.

when executing the onblur you could check if the field is empty. If it is empty you can put your default value back in. When it is not empty, do nothing.

I’m trying to figure this out, but I haven’t done it right. Weird. It’s super simple to do this… or so it would seem.


function input_reset(obj){
	if (obj.length == 0)
		obj.value = obj.defaultValue;
}

/* I tried writing the if statement like this, too */
if (obj === "")