Can I call two PHP scripts from the same onBlur?

In the code below the preFilladmin function creates a JavaScript error after leaving the admin_uid field

// Set this to your validation PHP script, default is "validate.php?value="
var vUrl = "/validate_assignIP.php?value=";

// This is the array for error handling
var vError =  [];

// We attach to every input field a little js
function getForm() {
   parent.document.title=document.title;

	if (document.getElementsByTagName) {
		var vInput = document.getElementsByTagName("input");

		for (var vCount=0; vCount<vInput.length; vCount++)
			vInput[vCount].onblur = function() { return validateIt(this); }
	}
}

// Refers to the function
http = getHTTPObject();

function getHTTPObject() {

  var xmlhttp;

   if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }

  return xmlhttp;

}

// The main validation-function
function validateIt(vInput) {

	// Each input field's id
	vId = vInput.id;
	vValue = vInput.value;

	// Pre Fill fields using data from preceeding fields
	if (vId == "dev_name") {
	   preFillname();
	}
	else if (vId == "admin_uid") {
	   preFilladmin();
	}

	// Separate the class attr of each input field
	getValue = vInput.className;
	if(getValue.indexOf(",") == -1 ) {
		vType = getValue;
		vRequired = "";
	} else {
		vRules = vInput.className.split(",");
		vRequired = vRules[0];
		vType = vRules[1];
	}

	// The whole URL
	var url = vUrl + (vValue) + "&required=" + (vRequired) + "&type=" + (vType);

	// And finally we send it to the url we specified
	http.open("GET", url, true);

	// The handleHttpResponse is the function we call, when we have an
	// answer back from the PHP script
	http.onreadystatechange = handleHttpResponse;
	http.send(null);

}

// A function to handle the response from the PHP script
function handleHttpResponse() {

	if (http.readyState == 4) {

		// Refering to the PHP script, for every validation we'll get
		// either true or false and apply some visual changes in order to
		// get the user focusing on each field whether it's ok or not
		// If one of the input fields contains an error, the submit button
		// will be disabled, until the error (that means all errors) are
		// solved
		if(http.responseText == "false") {

			var sInput = document.getElementById(vId);
			//var vButton = document.getElementById("submit");

			document[vId].src = "/images/false.png";
			sInput.style.border = "1px solid #d12f19";
			sInput.style.background = "#f7cbc2";
			//vButton.disabled = true;
			vError.push(vId);

		}

		if(http.responseText == "true") {

			var sInput = document.getElementById(vId);

			document[vId].src = "/images/true.png";
			sInput.style.border = "1px solid #338800";
			sInput.style.background = "#c7f7be";

			// We do a check if our element is in the error array, and if
			// so, we can delete it from the array
			if(vError.indexOf(vId) != -1) {
				var aId = vError.indexOf(vId);
				vError.splice(aId, 1);
				if(vError.length > 0) {
					//var vButton = document.getElementById("submit");
					//vButton.disabled = true;
				} else {
					//var vButton = document.getElementById("submit");
					//vButton.disabled = false;
				}
			}
		}

		if(http.responseText == "none") {

			var sInput = document.getElementById(vId);
			//var vButton = document.getElementById("submit");

			document[vId].src = "/images/blank.gif";
			sInput.style.border = "1px solid #aaa";
			sInput.style.background = "#ffffff";
			//vButton.disabled = false;

		}
	}
}

function preFillname() {
   var txtPatrn = /^[\w -\']{3,}/;
   if (txtPatrn.test(document.getElementById("dev_name").value)) {
      document.getElementById("server_name").value=document.getElementById("dev_name").value; // pre-fill field
   }
}

function preFilladmin() {
   //http=getHTTPObject();
   if (http==null) {
     alert ("Your browser does not support AJAX!");
     return;
   }
   var url="/ms_exquery.php";
   url=url+"?uid="+document.getElementById("admin_uid").value;
   url=url+"&sid="+Math.random();

   http.open("GET",url,true);

   http.onreadystatechange = get_email_data;
   http.send(null);
}

function get_email_data() {
   if (http.readyState == 4) {
      var ms_ex_params = http.responseText;
      var splitResult = ms_ex_params.split("~");

      var admin_name = splitResult[0];
      document.getElementById("admin_name").value=admin_name;   // pre-fill the admin_name field
      //var admin_email = splitResult[1];
      //document.getElementById("admin_email").value=admin_email; // pre-fill the admin_email field
      //var admin_phone = splitResult[2];
      //document.getElementById("admin_phone").value=admin_phone; // pre-fill the admin_phone field
   }
}

>Can I call two PHP scripts from the same onBlur?

Yes. You have to use two instances of XMLHttp.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.