Jump to content

AJAX not updating properly


esport

Recommended Posts

Hi Guys, I have a drop down list when selected populates a text area from data from a database. However, when I select an option from the list the first time, it says 'undifined'. When I select another item, the original item I selected data appears. Below is th javascript I am using:

 

<script language="javascript">

var xmlHttp = createXmlHttpRequestObject();
var response;

function get_template(){


var code = document.getElementById('email_templates').value;
if(code){	

	var url = "get_email_templates.php?ID="+code; 
			xmlHttp.open("GET", url, true);
			xmlHttp.onreadystatechange = handleRequestStateChange;				
			xmlHttp.send(null); 

			document.getElementById('comments').value=response;

}
}

function createXmlHttpRequestObject()
{
// will store the reference to the XMLHttpRequest object
var xmlHttp;
// this should work for all browsers except IE6 and older
try
{
// try to create XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// assume IE6 or older
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
	"MSXML2.XMLHTTP.5.0",
	"MSXML2.XMLHTTP.4.0",
	"MSXML2.XMLHTTP.3.0",
	"MSXML2.XMLHTTP",
	"Microsoft.XMLHTTP");
// try every prog id until one works
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
{
	try
	{
	// try to create XMLHttpRequest object
		xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
	}
	catch (e) {}
	}
}
// return the created object or display an error message
if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
else
	return xmlHttp;
}



// function that handles the HTTP response
function handleRequestStateChange()
{
//var response='';
// display the status of the request
if (xmlHttp.readyState == 4)
{
// continue only if HTTP status is "OK"
	if (xmlHttp.status == 200)
	{
		try
		{
			// read the message from the server
				response = xmlHttp.responseText;
			//	alert(response);
				return response; 
		}
		catch(e)
		{
			// display error message
			alert("Error reading the response: " + e.toString());
		}
	}
	else
	{
		// display status message
		alert("There was a problem retrieving the data:\n" +
		xmlHttp.statusText);
	}
}
}




</script>

 

And here is the html:

 

<textarea name="comments" cols="90" rows="7" id="comments"></textarea>
  <br />
                                                                                                      
<select name="select" id="email_templates"  onchange="get_template()">
   <option value=''>:::: Please Select a Template :::: </option>
<?php
$allTemplates = mysql_query("SELECT * FROM email_templates WHERE category='warranty'",$server);
echo mysql_error();
while($getTemplates = mysql_fetch_array($allTemplates)){
?>
           <option value="  <?= $getTemplates['ID'] ?>">
            <?= $getTemplates['name'] ?>
          </option>
   <?
  }
 ?>
   </select>

 

I have test the PHP that it calls and the is outputing correctly.

Can someone please advise why it does this.

 

Thanks in advance

 

Daniel

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.