Hello All,
I started to teach myself PHP and MySQL for a small work project that has now turned into a monster project with two new languages that I need to learn namely XML and AJAX!
I would really appreciate help with the following.
I have created a dropdown box with PHP. On changing the box an ajax function is executed to get the result of a PHP query on my database and return a list of values to populate a second dropdown box.
When I use ajax.responseText method I can see that the XML is generated as I expected, however when I use ajax.responseXML nothing happens. I am fairly convinced that my XML is wrong because I have absolutely no clue about XML. Whatever the result I get I want to be able to call my dropdown function again, thus the output from the query needs to give me an array. I believe that it is impossible to pass a complete array structure through AJAX so I thought I should build the XML and then use AJAX to reconstruct the array.
Code snippets below:
All help greatly appreciated.
ceejld
//Create the first dropdown box
function create_dropdown($identifier_1,$pairs_name,$firstentry_name,$functionCall)
{
//start the dropdown list with the select element and title
$dropdown_name = "<select name=\"$identifier_1\" onChange=\"$functionCall\">";
$dropdown_name .= "<option name=\"\">$firstentry_name</option>";
//Create the dropdown elements
foreach($pairs_name AS $empID => $empName)
{
$dropdown_name .="<option name=\"$empID\">$empName</option>";
}
//conclude the dropdown and return it
echo "</select>";
return $dropdown_name;
}
//Close DB connection
mysql_close();
?>
//PHP to run the query*******************************
function getItemList($itemType){
GLOBAL $myList;
//Connect to the database
$linkID = @mysql_connect("localhost", "xxx", "xxx")or die("Cannot connect");
@mysql_select_db("configControl") or die ("could not select database");
//Make the query
$query = "SELECT ITEMID,ITEMNAME FROM CONFIGITEMS WHERE ITEMTYPE LIKE '$itemType'";
// echo $query;
$item_result = mysql_query($query);
$myList = "<item_list>"; //start xml definition of an item list
while($row = mysql_fetch_array($item_result))
{
// echo $myList;
$itemID = $row["ITEMID"];
$item = $row["ITEMNAME"];
$myList .= "<item id =\"$itemID\"><name>\"$item\"</name></item>";
}
$myList .="</item_list>"; // complete the xml definition of an item list
echo $myList;
//Close DB connection
mysql_close();
return;
}
//Ajax Function that should parse some XML and return a list
<!--
//Browser Support Code
function ajaxReturnConfigItems(){
var ajaxRequest_item;
var item_list;
var item_id;
var temp_id;
var item_name;
var theXMLResponse; //variable to contain the XMl response
;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest_item = new XMLHttpRequest();
;
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest_item = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest_item = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}//end catch
// Create a function that will receive item data sent from the server
ajaxRequest_item.onreadystatechange = function(){
if(ajaxRequest_item.readyState == 4){
//assign value to variable containing the output from responseXML
theXMLResponse = ajaxRequest_item.responseXML;
item_list = theXMLResponse.getElementByTagName('item id');
item_id = theXMLResponse.getElementsByTagName('item');
//As test just try and return the name of the first item in the list into a textbox
//called NAMEOFITEM
id = item_id[1].getAttribute('id');
name = item_id[1].getElementByTagName('name').firstChild.data;
document.newCCR.NAMEOFITEM.value = name;
}//end if
}//end readyStateChange
ajaxRequest_item.open("GET", "item.php?itemType="+document.newCCR.TYPE.options[document.newCCR.TYPE.selectedIndex].value, true);
ajaxRequest_item.send(null);
}//end function
//-->