rbarnett Posted July 25, 2008 Share Posted July 25, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
rbarnett Posted July 25, 2008 Author Share Posted July 25, 2008 Nevermind. I answered my own question. I added the following conditional to display the results: if (document.getElementById('user').value != '') { Thanks anyway Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.