Hi I am prety new to ajax and was hoping someone could help me with the following:

I have a form that gets posted through ajax. This works fine. However I want to have some form validation, for example to check the email is in correct format, and if there is an error I want the error message to display underneath the field with the error.

Here is where the problem begins. The data of the form is posted to the contact.php file which does the validation and if all is ok it emails the data to me. However because all the information is retrieved by javascript with the following statement:

Code:
result = http_request.responseText;
I cannot use this. I want to send a variable back from the php file that shows there is an error with the email and then use .innerHTML to print the error in the errEmail div.. and so on for the other fields.

Here is my code so far:

Form.php:
PHP Code:
<form action="javascript:get(document.getElementById('form1'));" name="form1" id="form1" enctype="text/plain" lang="he">
                <
fieldset>
                    <
div><label for="name"><span class="red">*</span>Name:</label></div>
                    <
div><input type="text" name="name" id="name" onBlur="checkField('name', 'errName')" /></div>
                    <
div class="form-padding"></div>
                    <
div><label for="company">Company:</label></div>
                    <
div><input type="company" name="company" id="company"  /></div>
                    <
div class="clear"></div>
                    
                    <
div id="errName" style="color:#CC0000;"></div>
                    <
div class="clear"></div>
                    
                    <
div><label for="email"><span class="red">*</span>Email:</label></div>
                    <
div><input type="text" name="email" id="email"  onBlur="checkField('email', 'errEmail')" /></div>
                    <
div class="form-padding"></div>
                    <
div><label for="address">Address:</label></div>
                    <
div><input type="text" name="address" id="address"  /></div>
                    <
div class="clear"></div>
                    
                    <
div id="errEmail" style="color:#CC0000;"></div>
                    <
div class="clear"></div>
                    
                    <
div><label for="phone"><span class="red">*</span>Phone:</label></div>
                    <
div><input type="text" name="phone" id="phone"  onBlur="checkField('phone', 'errPhone')" /></div>
                    <
div class="form-padding"></div>
                    <
div><label for="fax">fax:</label></div>
                    <
div><input type="text" name="fax" id="fax"  /></div>
                    <
div class="clear"></div>
                    
                    <
div id="errPhone" style="color:#CC0000;"></div>
                    <
div class="clear"></div>
                    
                    <
div class="long-div"><label for="form_subject">Subject:</label></div>
                    <
div class="long-div"><input type="text" name="form_subject" id="form_subject" class="long-field" /></div>
                    <
div class="clear"></div>
                    <
div class="long-div"><label for="form_content">Message:</label></div>
                    <
div class="long-div"><textarea name="form_content" id="form_content" ></textarea></div>
                    <
div class="clear"></div>
                    <
div class="clear"></div>
                    <
div class="form-bottom"><input type="image" src="images/contact-cube-send.gif" style="width: 43px; height: 20px;" value="" onclick="javascript:get(this.parentNode);" /></div>
                </
fieldset>
            </
form
The Javascript:
Code:
// JavaScript Document
   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
     
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('content').innerHTML = result;           
         } else {
            alert('There was a problem with the request.');
         }
		
      }
	  if (http_request.readyState == 1 || http_request.readyState=="loading")
			//if AJAX state is loading (loading)
			{
			 document.getElementById('load').innerHTML = "<div style=\"width: inherit; text-align: center;\"><img src=\"images/load.gif\" alt=\"Loading...\" /></div>"
			//get element where the id is ôResult", in this case it goes to the <div> tag
			//send the loading image (loading.gif) that show us the data is being prepared
		}
   }
   
   function get(obj) {
      var poststr = "Name=" + escape(encodeURI(document.getElementById("name").value ))
        +"&Company=" +
      escape(encodeURI( document.getElementById("company").value ))
      +"&Email=" +
      escape(encodeURI( document.getElementById("email").value ))
	  +"&Address=" +
      escape(encodeURI( document.getElementById("address").value ))
	  +"&Phone=" +
      escape(encodeURI( document.getElementById("phone").value ))
	  +"&Fax=" +
      escape(encodeURI( document.getElementById("fax").value ))
	  +"&Form_subject=" +
      escape(encodeURI( document.getElementById("form_subject").value ))
	  +"&Form_content=" +
      escape(encodeURI( document.getElementById("form_content").value ));

	
      makePOSTRequest('Scripts/sendContact.php', poststr);

   }
And the form processing:
PHP Code:
<?php

        $sender 
urldecode($_POST['Name']);
        
$company urldecode($_POST['Company']);
        
$address urldecode($_POST['Address']);
        
$subjectF urldecode($_POST['Form_subject']);
        
$content urldecode($_POST['Form_content']);
        
$telephone $_POST['Phone'];
        
$fax $_POST['Fax'];

        
$telLength 8;

        
//CHECK EMAIL FORMAT
        
$isEmail ereg("^[^@ ]+@[^@ ]+\.[^@ ]+$",$_POST['Email'],$trashed);
        
        
//CHECK TELEPHONE FORMAT
        
$isTelephone is_numeric($telephone);
        
//CHECK TELEPHONE LENGTH
        
if (strlen($telephone) > 7) { 
            
$numLength 1;
        } else { 
            
$numLength 0;
        }

if (
$sender == ""){
echo 
"Name is Blank";
}
if (
$_POST['Email'] == "" || !$isEmail){
echo 
"Email is Blank or Wrong";

}
if (
$telephone == "" || !$isTelephone){
echo 
"Telephone blank or to short";

}
else{

        require_once 
"Mail.php";
        
        
// GET FORM DATA
        
        
        
$from $_POST['Email'];
        
$to "";
        
$subject $subjectF;
        
$body "Name: "$sender "<br /> Company: "$company ."<br> Email: "$from ."<br> Address: "$address ."<br> Phone: "$telephone ."<br /> Fax: "$fax ."<br>  Message: "$content ."<br> <br /><br /> IP: "$_SERVER['REMOTE_ADDR'];
        
        
$host "host";
        
$username "username";
        
$password "Passowrd";
        
        
$headers = array ('Content-type' => 'text/html; charset=utf-8''From' => $from'To' => $to'Subject' => $subject);
        
$smtp Mail::factory('smtp',
          array (
'host' => $host,
            
'auth' => true,
            
'username' => $username,
            
'password' => $password));
        
        
$mail $smtp->send($to$headers$body);
        
        if (
PEAR::isError($mail)) {
          echo(
"<p>" urldecode($_POST['Name']) . "</p>");
         } else {
          echo(
"<p>Thank you, sent</p>");
         }
}
?>
Basically in short I want to split up the results i get from form processing php page so i can dispaly the seperate errors in their correct place.

I know its a long post but i would really appreciate any suggestions!