Dart (1) [Avatar] Offline
#1
I'm using the DoubleCombo to allow users to select their state and then a list of counties should populate the second drop box. There are some states where the second box will not populate.

Below is the code in DoubleComboXML.php:


$state = $_POST['q'];

echo "<?xml version="1.0"?>";

$query = "SELECT school_num, county FROM school WHERE state = '$state' GROUP BY county";
$result = @mysql_query($query);


echo '<selectChoice>
<selectElement><formName>form1</formName><formElem>county_select</formElem></selectElement>';

//while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo '<entry><optionText>';
echo "Select A County";
echo '</optionText><optionValue>';
echo "-1";
echo '</optionValue></entry>';
echo '<entry><optionText>';
echo "All Counties";
echo '</optionText><optionValue>';
echo "0";
echo '</optionValue></entry>';

//}
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
$opt_text = htmlspecialchars($row[1]);
$val_text = htmlspecialchars($row[1]);
echo '<entry><optionText>';
echo $opt_text;
echo '</optionText><optionValue>';
echo $val_text;
echo '</optionValue></entry>';
}


echo '</selectChoice>';
Pascarello (208) [Avatar] Offline
#2
Re: DoubleCombo
Take a look at this article I posted on my blog:
http://radio.javaranch.com/pascarello/2006/09/12/1158096122600.html

See if that helps you spot the error.

In your case the last question is probbaly what you are after. Hardcode a value with one of the ones giving you the error.

Eric
Colleen (12) [Avatar] Offline
#3
Re: DoubleCombo
Hi,
I was wondering whether Dart actually found the reason for his DoubleCombo Drop down list not working. The reason I ask is because I seem to have the same problem. The responseXML is null. I don't know why. I read your article on your blog and I ran my DoubleCombo.php and saw the XML it produced and it looked fine (even though I am very dodgy on the XML). I also downloaded FireBug and looked in the 'Inspector' screen to give me some clue but there didn't seem to be anything wrong.

Below is the code. I am pretty sure it is not a database problem because I get an XML file when I run it which I will also paste below.

Thanks for having a squiz at it. I have been at this for a few weeks and am about to give up.

DoubleCombo.php
---------------------------
<?php <br /> header("Content-type: text/xml"smilie;
session_start();
ob_start();
require_once 'functions.php';
require_once 'checkfunctions.php';
require_once 'conn.php';
require_once 'http.php';
require_once 'constants.php';
// include 'mime_type.php';

global $xml_str;
global $xml_while;
global $xml_total_str;

get_system_paths();
$image_dir = prepare_directory($_SESSION['clipart_path']);


if (isset($_POST['q'])) {
$state = $_POST['q'];
echo $state;
}

$state = "VIC";

$conn = mysql_connect(SQL_HOST, SQL_USER, SQL_PASS)
or die('Could not connect to the database; ' . mysql_error());

$sql = "select suburb_desc " .
"FROM suburb " .
" WHERE state_code = '$state' " .
" AND country_code = 'AU'" .
" AND suburb_desc like 'A%' " .
" ORDER by suburb_desc ";

$result = mysql_query($sql, $conn)
or die('Could not select from suburb table; ' . mysql_error());


$xml_str = "<?xml version="1.0" encoding="utf-8"?>";
$xml_str .= "<selectChoice>";
$xml_str .= "<selectElement>";
$xml_str .= "<formName>Form1</formName>";
$xml_str .="<formElem>ddlTerritory</formElem>";
$xml_str .="</selectElement>";
$xml_str .="<entry>";
$xml_str .="<optionText>";
$xml_str .="Select A Suburb";
$xml_str .="</optionText>";
$xml_str .="<optionValue>";
$xml_str .="-1";
$xml_str .="</optionValue>";
$xml_str .="</entry>";

while ($row=mysql_fetch_array($result)) {
// Escaping illegal characters
$row['suburb_desc'] = str_replace("&", "&", $row['suburb_desc']);
$row['suburb_desc'] = str_replace("<", "<", $row['suburb_desc']);
$row['suburb_desc'] = str_replace(">", ">", $row['suburb_desc']);

$opt_text = $row['suburb_desc'];
$val_text = $row['suburb_desc'];

$xml_while = $xml_while .
"<entry>" .
"<optionText>" .
$opt_text .
"</optionText>" .
"<optionValue>" .
$val_text .
"</optionValue>" .
"</entry>";
}

$xml_total_str = $xml_str . $xml_while . "</selectChoice>";

echo $xml_total_str;

?>

The XML file it produces
--------------------------------------

<?xml version="1.0" encoding="utf-8" ?>
- <selectChoice>
- <selectElement>
<formName>Form1</formName>
<formElem>ddlTerritory</formElem>
</selectElement>
- <entry>
<optionText>Select A Suburb</optionText>
<optionValue>-1</optionValue>
</entry>
- <entry>
<optionText>ABBOTSFORD</optionText>
<optionValue>ABBOTSFORD</optionValue>
</entry>
- <entry>
<optionText>ABECKETT STREET</optionText>
<optionValue>ABECKETT STREET</optionValue>
</entry>
- <entry>
<optionText>ABERFELDIE</optionText>
<optionValue>ABERFELDIE</optionValue>
</entry>
- <entry>
<optionText>AIRPORT WEST</optionText>
<optionValue>AIRPORT WEST</optionValue>
</entry>
- <entry>
<optionText>ALBANVALE</optionText>
<optionValue>ALBANVALE</optionValue>
</entry>
- <entry>
<optionText>ALBERT PARK</optionText>
<optionValue>ALBERT PARK</optionValue>
</entry>
- <entry>
<optionText>ALBION</optionText>
<optionValue>ALBION</optionValue>
</entry>
- <entry>
<optionText>ALPHINGTON</optionText>
<optionValue>ALPHINGTON</optionValue>
</entry>
- <entry>
<optionText>ALTONA</optionText>
<optionValue>ALTONA</optionValue>
</entry>
- <entry>
<optionText>ALTONA EAST</optionText>
<optionValue>ALTONA EAST</optionValue>
</entry>
- <entry>
<optionText>ALTONA GATE</optionText>
<optionValue>ALTONA GATE</optionValue>
</entry>
- <entry>
<optionText>ALTONA MEADOWS</optionText>
<optionValue>ALTONA MEADOWS</optionValue>
</entry>
- <entry>
<optionText>ALTONA NORTH</optionText>
<optionValue>ALTONA NORTH</optionValue>
</entry>
- <entry>
<optionText>ARDEER</optionText>
<optionValue>ARDEER</optionValue>
</entry>
- <entry>
<optionText>ARMADALE</optionText>
<optionValue>ARMADALE</optionValue>
</entry>
- <entry>
<optionText>ARMADALE NORTH</optionText>
<optionValue>ARMADALE NORTH</optionValue>
</entry>
- <entry>
<optionText>ARTHURS CREEK</optionText>
<optionValue>ARTHURS CREEK</optionValue>
</entry>
- <entry>
<optionText>ASCOT VALE</optionText>
<optionValue>ASCOT VALE</optionValue>
</entry>
- <entry>
<optionText>ASHBURTON</optionText>
<optionValue>ASHBURTON</optionValue>
</entry>
- <entry>
<optionText>ASHWOOD</optionText>
<optionValue>ASHWOOD</optionValue>
</entry>
- <entry>
<optionText>ASPENDALE</optionText>
<optionValue>ASPENDALE</optionValue>
</entry>
- <entry>
<optionText>ASPENDALE GARDENS</optionText>
<optionValue>ASPENDALE GARDENS</optionValue>
</entry>
- <entry>
<optionText>ATTWOOD</optionText>
<optionValue>ATTWOOD</optionValue>
</entry>
- <entry>
<optionText>AUBURN</optionText>
<optionValue>AUBURN</optionValue>
</entry>
- <entry>
<optionText>AUBURN SOUTH</optionText>
<optionValue>AUBURN SOUTH</optionValue>
</entry>
- <entry>
<optionText>AVALON</optionText>
<optionValue>AVALON</optionValue>
</entry>
- <entry>
<optionText>AVONDALE HEIGHTS</optionText>
<optionValue>AVONDALE HEIGHTS</optionValue>
</entry>
</selectChoice>

and for what it's worth - here's list.php which is the form calling DoubleCombo.php
----------------------------------------------------------------------------------------------------------------------
<?php <br /> session_start();
ob_start();
require_once 'functions.php';
require_once 'checkfunctions.php';
require_once 'conn.php';
require_once 'http.php';
require_once 'constants.php';

get_system_paths();
$image_dir = prepare_directory($_SESSION['clipart_path']);

/************************ start functions ************************/

/********************************* end functions ****************************************************/
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<script type="text/javascript" src="net.js"></script>

<title></title>

<script type="text/javascript">
function FillTerritory(oElem,oTarget)
{
var strValue = oElem.options[oElem.selectedIndex].value;
var url = "DoubleComboXML.php";
// var url = "DoubleComboXML_hard.php";
var strParams = "q=" + strValue +
"&f=" + oTarget.form.name +
"&e=" + oTarget.name;

alert("strValue:" + strValue);
alert("url:" + url);
alert("strParams:" + strParams);

var loader1 = new net.ContentLoader(url,
FillDropDown,
null,
"POST",
strParams,
"text/xml");
alert("after declaration of the loader");
}

function FillDropDown() {
//alert("FILLDROPDOWN responseText from the xml:" + this.req.responseText.documentElement);
var xmlDoc = this.req.responseXML.documentElement;
var xSel= xmlDoc.getElementsByTagName('selectElement')[0];
var strFName = xSel.childNodes[0].firstChild.nodeValue;
var strEName = xSel.childNodes[1].firstChild.nodeValue;

alert(" FILL DROPDOWN xmlDoc:" + xmlDoc);
alert("FILLDROPDOWN xSel:" + xSel);
alert("FILLDROPDOWN strFName:" + strFName);
alert("FILLDROPDOWN strEName:" + strEName);

var objDDL = document.forms[strFName].elements[strEName];
objDDL.options.length=0;

var xRows = xmlDoc.getElementsByTagName('entry');

for (i=0;i<xRows.lenth; i++) { >
var theText = xRows[i].childNodes[0].firstChild.nodeValue;
alert("....theText:" + theText);
var theValue = xRows[i].childNodes[1].firstChild.nodeValue;
alert("....theValue:" + theValue);

var option = new Option(theText, theValue);
try {
objDDL.add(option,null);
}
catch(e) {
objDDL.add(option,-1);
}
}
}
</script>

</head>

<body>

<form name="Form1">
<select name="ddlRegion" >
onchange="FillTerritory(this,document.Form1.ddlTerritory)">
<option value="-1">Pick A Region</option>
<option value="Vic">Vic</option>
<option value="NSW">NSW</option>
<option value="SA">SA</option>
<option value="WA">WA</option>
</select>

<select name="ddlTerritory"></select>

</form>

</body>
</html>

----------------------------------------------------------------------------------------
Thanks for your help.
kurinosuke (156) [Avatar] Offline
#4
Re: DoubleCombo
>xRows.lenth
This is a typo, isn't it ? It should be "length".
Colleen (12) [Avatar] Offline
#5
Re: DoubleCombo
Thanks, you are an absolute DOLL! That typo was the problem. I must admit I didn't think it was the javascript. I think it was something more sinister. Thanks again!
Colleen (12) [Avatar] Offline
#6
Re: DoubleCombo
Now, I have another problem with the DoubleCombo drop down list.

Previously in DoubleComboXML.php I hardcoded a parameter that I was supposed to retrieve from the first option pick list. $state="VIC" - only because I wanted to eliminate any problems clouding the XML.

When I take out my hard -coding and retrieve my variable by $state=$_POST['q'] I get a very ugly error.

I am sure I have downloaded the correct net.js:
/*************************************************************************************/

/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net=new Object();

net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader=function(url,onload,onerror,method,params,contentType){
this.req=null;
this.onload=onload;
this.onerror=(onerror) ? onerror : this.defaultError;
this.loadXMLDoc(url,method,params,contentType);
}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){
if (!method){
method="GET";
}
if (!contentType && method=="POST"){
contentType='application/x-www-form-urlencoded';
}
if (window.XMLHttpRequest){
this.req=new XMLHttpRequest();
} else if (window.ActiveXObject){
this.req=new ActiveXObject("Microsoft.XMLHTTP");
}
if (this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
net.ContentLoader.onReadyState.call(loader);
}
this.req.open(method,url,true);
if (contentType){
this.req.setRequestHeader('Content-Type', contentType);
}
this.req.send(params);
}catch (err){
this.onerror.call(this);
}
}
}


net.ContentLoader.onReadyState=function(){
var req=this.req;
var ready=req.readyState;
if (ready==net.READY_STATE_COMPLETE){
var httpStatus=req.status;
if (httpStatus==200 || httpStatus==0){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
}

net.ContentLoader.prototype.defaultError=function(){
alert("error fetching data!"
+"

readyState:"+this.req.readyState
+"
status: "+this.req.status
+"
headers: "+this.req.getAllResponseHeaders());
}
/*****************************************************************************/

I am feeling quite semi-retarded these days and want to kick myself because it is probably something really simple.

Thanks.
Pascarello (208) [Avatar] Offline
#7
Re: DoubleCombo
What is the error?

Eric
Colleen (12) [Avatar] Offline
#8
Re: DoubleCombo
Sorry, my mistake. There is no error (disappeared after a typo error). I am just not getting response from the second drop down list. When I debug my php code I notice there is no $_POST variable.

Then when I debug in Firebug there is something in the responsetext: "<?xml version="1.0" encoding="utf-8"?><selectChoice><selectElement><formName>Form1</formName><formEl..." >

at the "this.req.send(params);" stage when net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType) in net.js is called.....then it disappears into a deep dark pit.

I don't think it's my XML document because I passed it a parameter and ran it in Firefox and there was no problem.

I am not getting my $_POST variable because I am loosing my XMLHttpRequest ....and my mind.
kurinosuke (156) [Avatar] Offline
#9
Re: DoubleCombo
How did you create the ContentLoader ? Can you post your "new ContentLoader..." ?
If you could also show us what you've put into "params" before sending the request.
Colleen (12) [Avatar] Offline
#10
Re: DoubleCombo
Here are the relevant parts of my php file:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>

<script type="text/javascript" src="net.js"></script>

<title></title>

<script type="text/javascript">
function FillTerritory(oElem,oTarget)
{
debugger;
var strValue = oElem.options[oElem.selectedIndex].value;
var url = "DoubleComboXML.php";


var strParams = "q=" + strValue +
"&f=" + oTarget.form.name +
"&e=" + oTarget.name;
/*
alert("strValue:" + strValue);
alert("url:" + url);
alert("strParams:" + strParams);
*/

var loader1 = new net.ContentLoader(url,
FillDropDown,
null,
"POST",
strParams,
"text/xml");
//alert("after declaration of the loader");
}

function FillDropDown() {
//alert("FILLDROPDOWN responseText from the xml:" + this.req.responseText.documentElement);
var xmlDoc = this.req.responseXML.documentElement;
var xSel= xmlDoc.getElementsByTagName('selectElement')[0];
var strFName = xSel.childNodes[0].firstChild.nodeValue;
var strEName = xSel.childNodes[1].firstChild.nodeValue;

/*
alert(" FILL DROPDOWN xmlDoc:" + xmlDoc);
alert("FILLDROPDOWN xSel:" + xSel);
alert("FILLDROPDOWN strFName:" + strFName);
alert("FILLDROPDOWN strEName:" + strEName);
*/

var objDDL = document.forms[strFName].elements[strEName];
objDDL.options.length=0;

var xRows = xmlDoc.getElementsByTagName('entry');

for (i=0;i<xRows.length; i++) { >
var theText = xRows[i].childNodes[0].firstChild.nodeValue;
var theValue = xRows[i].childNodes[1].firstChild.nodeValue;

var option = new Option(theText, theValue);
try {
objDDL.add(option,null);
}
catch(e) {
objDDL.add(option,-1);
}
}
}
</script>

</head>

<body>

<form name="Form1">

<select name="ddlRegion" >
onchange="FillTerritory(this,document.Form1.ddlTerritory)">


<option value="-1">Pick A Region</option>
<option value="Vic">Vic</option>
<option value="NSW">NSW</option>
<option value="SA">SA</option>
<option value="WA">WA</option>
</select>

<select name="ddlTerritory"></select>

</form>

</body>
</html>
***********************************************************************************
Thanks.
kurinosuke (156) [Avatar] Offline
#11
Re: DoubleCombo
The following is wrong :
var loader1 = new net.ContentLoader(url,
FillDropDown,
null,
"POST",
strParams,
"text/xml");

You should not pass "text/xml" as the content type for the request. You are sending form based information to the server, so you should leave this to null. The content type will default to "application/x-www-form-urlencoded".

The response is "text/xml". You set it in your php file on the server side.
The client sends "application/x-www-form-urlencoded" parameters.

So you should change it to :
var loader1 = new net.ContentLoader(url,
FillDropDown,
null,
"POST",
strParams );
Colleen (12) [Avatar] Offline
#12
Re: DoubleCombo
That was the problem. Thank you so much. I really appreciate your help!
Pascarello (208) [Avatar] Offline
#13
Re: DoubleCombo
Both of you need to work in my timezone so I can help out! LOL

Eric
kurinosuke (156) [Avatar] Offline
#14
Re: DoubleCombo
Mouarf smilie
Colleen (12) [Avatar] Offline
#15
Re: DoubleCombo
Thanks guys. It's hard to believe that the problem and support is given from around the world. Such a good buzz...zzz.z..zz.z!!!