Page 1 of 1

[SOLVED] Firing an Ajax Request From PHP generated pulldowns

Posted: Wed May 09, 2012 11:58 am
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

Re: Firing an Ajax Request From PHP generated pulldowns

Posted: Wed May 09, 2012 12:01 pm
by pickle
I think the "change" event isn't fired until the select loses focus.

Re: Firing an Ajax Request From PHP generated pulldowns

Posted: Wed May 09, 2012 1:17 pm
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.