Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing input type value to a PHP url with AJAX.

Status
Not open for further replies.

milo3169

Programmer
May 2, 2007
42
Hi,

I am a Newbie to AJAX and I'm not really sure if this is an AJAX question, but in this AJAX script I'm using I'm having problems passing a Dropdown list value to a serverpage url in AJAX.

I have this AJAX script where when someone selects a selection from a dropdown list it sends a request to a PHP file to create another dropdown list with specific information relating the first selection that was made. The functionality seems to be working and if I hard code the URL it works like I want it to.
Code:
<select class="formfield" name="item_code"  onchange="makerequest('[COLOR=red]sub_cat_menu.php?item_code=wint[/color]','hw');">
The problem is that in that function call where it says...sub_cat_menu.php?item_code=wint,, I need to have "wint" as a dynamic variable that I can pass to the AJAX function. I think the problem is on where that the function call is on the SELECT field.

Code:
<html>
<head>
<script type="text/javascript">
	<!--
	
	
	//Create a boolean variable to check for a valid Internet Explorer instance.
	var xmlhttp = false;
	
	//Check if we are using IE.
	try {
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		//alert ("You are using Microsoft Internet Explorer.");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			//alert ("You are using Microsoft Internet Explorder");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
		alert ("You are not using Microsoft Internet Explorer");
	}
	
	function makerequest(serverPage, objID) {
		
		var obj = document.getElementById(objID);
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	}
	
	//-->
</script>
</head>

Below is the HTML Dropdown and function call
Code:
 <select class="formfield" name="item_code" onchange="makerequest('sub_cat_menu.php?item_code=wint','hw');">
   <option selected value=""></option>
   <?php echo build_dropdown(); ?>
 </select>

Is there a way that I can change that variable to what ever is selected from that dropdown list so it can be passed to the AJAX function? If so how do you do that?
 
And if you wanted to handle spaces etc... escape the value:
Code:
onchange="makerequest('sub_cat_menu.php?item_code=' + [!]escape([/!]this.value[!])[/!], 'hw');"
Cheers,
Jeff


[tt]Jeff's Page [!]@[/!] Code Couch
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I tried it and it worked!

Thanks a lot BillyRayPreachersSon and babyJeffy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top