Jump to content

Ajax undefined issue


rbarnett

Recommended Posts

I am just learning AJAX and created a simple page that is just a form and when you enter a User ID the other text fields will populate based on an onblur event.  Everything works fine except if nothing is entered an you click on another field everything populates with 'undefined' except the first field (result[0]  under http.responseText.split

 

If anyone could tell me how to NOT return anything if you click on another field if there is no data returned instead of 'undefined' I would greatly appreciate the help.

 

Thanks

 

Here is the javascript code:

var url = "getUserInfo.php?param="; 

function handleHttpResponse() {

  if (http.readyState == 4) {

    // Split the comma delimited response into an array
    
    
    results = http.responseText.split(",");

    document.getElementById('first').value = results[0];

    document.getElementById('last').value = results[1];
    
    document.getElementById('empId').value = results[2];
    
    document.getElementById('driver_id').value = results[3];
    
    document.getElementById('contact_id').value = results[4];
    
    document.getElementById('company_id').value = results[5];
    
    document.getElementById('company_name').value = results[6];
    
    document.getElementById('group_id').value = results[7];
    
    document.getElementById('group_name').value = results[8];

document.getElementById('user').value = results[9];

  }

}


function updateUser() {
var userValue = document.getElementById("user").value; 
http.open("GET", url + escape(userValue), true); 
http.onreadystatechange = handleHttpResponse; http.send(null);

}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


var http = getHTTPObject();

 

Here is the html:

<form name="form1" action="post" >

  <table>
	<tr>
  			<td>User ID:</td>
		<td><input type="text" size="10" name="user" id="user" onblur="updateUser();" /></td>
	</tr>
  		<tr>
  			<td>First Name:</td>
		<td><input type="text" name="first" id="first" /></td>
		<td>Last Name:</td>
		<td><input type="text" name="last" id="last" /></td>
	</tr>
	<tr>
  			<td>Employee ID:</td>
		<td><input type="text" name="empId" id="empId" /></td>
	</tr>
	<tr>
		<td>Driver ID:</td>
		<td><input type="text" name="driver_id" id="driver_id" /></td>
	</tr>
	<tr>
		<td>Contact ID:</td>
		<td><input type="text" name="contact_id" id="contact_id" /></td>
	</tr>
	<tr>
		<td>Company ID:</td>
		<td><input type="text" name="company_id" id="company_id" /></td>

		<td>Company Name:</td>
		<td><input type="text" name="company_name" id="company_name" /></td>
	</tr>
	<tr>
		<td>Group ID:</td>
		<td><input type="text" name="group_id" id="group_id" /></td>

		<td>Group Name:</td>
		<td><input type="text" name="group_name" id="group_name" /></td>
	</tr>
</table>
</form>

 

And finally, the php code:

<?php
$username = $_GET['param'];

$query = "SELECT first_name, last_name,
	  client_employee_id, driver_id,
	  contact_id, company_id, driver_group_id, co.name as company_name, dg.name as group_name, username
	  from contacts
	  JOIN users using (contact_id)
	  JOIN drivers using (contact_id)
	  JOIN driver_groups dg using (driver_group_id)
	  JOIN companies co using (company_id)
	  WHERE username = $username";

$getInfo = $oDb->getArrayOfArrays("$query");

foreach($getInfo as $info)
{
  $information = $info['first_name']. ',' . $info['last_name'] . ',' .
              $info['client_employee_id'] . ',' . $info['driver_id'] . ',' .
              $info['contact_id'] . ',' . $info['company_id'] . ',' . $info['company_name'] . ',' .
              $info['driver_group_id'] . ',' . $info['group_name'] . ',' . $info['username'];
              
    if (count($getInfo > 0))
      echo $information;
}
?>

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.