[SOLVED] Firing an Ajax Request From PHP generated pulldowns

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

[SOLVED] Firing an Ajax Request From PHP generated pulldowns

Post by PastorHank »

SOLVED - The problem was in the quotation marks around the onchange call

Not sure exactly which forum to post this in, but since my php seems to be working I thought I'd try here :)

I have 5 pulldowns which are populated by PHP/MySql queries - the queries work and give me the correct data and here is an example of where I am placing the results

Code: Select all

	<td width="90">
		<form>
		<p><select size="1" name="D1" onchange=”showStudents(this.value);”> 
			<?php while(list($id, $student_id)=mysql_fetch_row($result1)) {
  			echo "
			<option value=\"".$id."\">".$student_id."";
			 }
			?>
			 </select></p>
		</form>	</td>
 
So far so good, I get the right students, now when I make a selection this is the <script> it's supposed to be firing (sorry I used the php tags) didnt see a 'code' tag

Code: Select all

 <script type="text/javascript">

function CreateXmlHttpObject() { //fuction to return the xml http object
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();//creates a new ajax object
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");//this is for IE browser
			}
			catch(e){
				try{
				req = new ActiveXObject("Msxml2.XMLHTTP");//this is for IE browser
				}
				catch(e1){
					xmlhttp=false;//error creating object
				}
			}
		}
		 	
		return xmlhttp;
	}
	
 function showStudents(str)
 {
	alert("Made it to show student"); 
 var req=CreateXmlHttpObject();
	if(req) 
	{
	req.onreadystatechange=function()
	{
	if (req.readState=4)(
	if(req.status=200){
	 document.getElementById("student_data").innerHTML=xmlhttp.responseText;
    }
    else
    {alert("there was a problem using XMLHTTP:\n");
  }
}
}
	req.open("GET","editstudents.php?q="+str,true);
	rq.send(null);
}
}
</script>
I'm not getting any firing of the function, at least it never gets to the alert inside the function.

would someone mind telling me what I'm missing or doing wrong?
TIA
Last edited by PastorHank on Wed May 09, 2012 4:28 pm, edited 3 times in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Firing an Ajax Request From PHP generated pulldowns

Post by pickle »

I think the "change" event isn't fired until the select loses focus.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
PastorHank
Forum Contributor
Posts: 117
Joined: Sat Jun 03, 2006 7:58 am
Location: Texas Hill Country

Re: Firing an Ajax Request From PHP generated pulldowns

Post by PastorHank »

If I put this in

Code: Select all

function getValue(selectObject) {
if( selectObject.selectedIndex>-1)
	alert("List " + selectObject.name + ": " + selectObject.options[selectObject.selectedIndex].value + " " + selectObject.options[selectObject.selectedIndex].text )
}
</script>


and modify the onchange command to read onchange="getValue(this);" it will return the selection for the 1st list, however if I modify the onchange for any of the remaining pulldowns, it returns nothing.
Post Reply