Topic: xajax PHP Dynamic Drop Down Menu

Hi there,
I have posted this message in PHP help but didn't get a response in a week so I thought of putting it here as well hoping for a reply :

__________________________________________________________________________________________

I have been struggeling lately to adapt the tutorialized script to pull data from my database, but to no avail.
It doesn't work. Therefore I am posting here my code so that someone could tell me what am I doing wrong.
So here it goes :

1. I have 2 tables in my database, table "province" and table "city".
2. Table province has 2 rows: "id" and "name"
3. Table city has 3 rows: "id", "fk_province_id" and "name"
4. I have a functions repository file called delegate.php that is referenced in every file on my site.
5. Obviously I want that when a dropdown # 1 (province) changes, the respective cities in that province show up in dropdown # 2

In my delegate.php file I have 2 functions that I reference in dropdown.php file:

// get provinces
 
function getProvinces(){
    $provinces = array();
    $con = getConnection();
 
    $query = "SELECT * FROM province";
    $result = executeSQL($query, $con);
 
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        //get province
        $province = new Province($row['id'], $row['name']);
        $provinces[] = $province;
        
    }
 
    return  $provinces;
}
 
//get cities for province
 
function getCitiesForProvince($provinceId){
    $cities = array();
    $con = getConnection();
 
    $query = "SELECT * FROM city WHERE fk_province_id=$provinceId";
    $result = executeSQL($query, $con);
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        $city = new City($row['id'], $row['name'], $row['fk_province_id']);
        $cities[] = $city;
    }
 
    return $cities;
}

And now here's the code of my dropdown.php file:

<?php
require('common/delegate.php');
session_start();
 
 
require('assets/js/xajax_core/xajax.inc.php');
$xajax = new xajax();
//$xajax->configure('debug',true);
 
class myXajaxResponse extends xajaxResponse {
 
  function addCreateOptions($sSelectId, $options) {
    $this->script("document.getElementById('".$sSelectId."').length=0");
    if (sizeof($options) >0) {
       foreach ($options as $option) {
         $this->script("addOption('".$sSelectId."','".$option."','".$option."');");
       }
     }
  }
}
      $provinces = getProvinces();
      $cities = getCitiesForProvince($provinces[0]->id);
 
      // adds an option to the select 
      function addCities($selectId, $provinces) {
        global $cities;
        $objResponse = new myXajaxResponse();
        $data = $cities[$provinces];
        $objResponse->addCreateOptions($selectId, $data);
//        return $objResponse->getXML();
                return $objResponse;
      }
 
      $xajax->registerFunction("addCities");
      $xajax->processRequest();
      ?>
      <?
      if (isset($_POST['Submit'])) {
        print_r($_POST);
      }
      ?>
      <html>
      <head>
      <title>AJAX Dynamic Drop Down Tutorial</title>
      <?
      $xajax->printJavascript("assets/js/");
      ?>
<script type="text/javascript">
  function addOption(selectId, val, txt) {
    var objOption = new Option(txt, val);
     document.getElementById(selectId).options.add(objOption);
   }
</script>
      </head>
      <body>
      <form name="form1" method="POST" action="">
      Province : 
      <select name="province" id="province" onchange="xajax_addCities('cities', document.form1.province.value)">
<? foreach ($provinces as $province) { ?>
        <option value="<?= $province->id?>"><?= $province->name?></option>
<? } ?>
      </select>
      City : 
      <select name="city" id="city">
<? foreach ($cities as $city) { ?>
        <option value="<?= $city->id?>"><?= $city->name?></option>
<? } ?>
      </select>
 
      <input type="submit" value="Submit" name="Submit" id="Submit">
      </form>
      </body>
      </html>

Output looks well but when I change a province, the cities dropdown doesn't change AT ALL.

I have enabled the xajax debugger and here's what I get the moment I change the province dropdown :

xajax debug output
Fri Sep 12 23:45:15 UTC+0300 2008
DONE [156ms]Fri Sep 12 23:45:15 UTC+0300 2008
ERROR: ExecuteCommand (#2, ):TypeError: Object doesn't support this property or methodFri Sep 12 23:45:15 UTC+0300 2008
ERROR: ExecuteCommand (#1, ):TypeError: Object doesn't support this property or methodFri Sep 12 23:45:15 UTC+0300 2008
ERROR: ExecuteCommand (#0, ):TypeError: Object doesn't support this property or methodFri Sep 12 23:45:15 UTC+0300 2008
RECEIVED [status: 200, size: 240 bytes, time: 78ms]:<?xml version="1.0" encoding="utf-8" ?><xjx><cmd cmd="js">Sdocument.getElementById('cities').length=0</cmd><cmd cmd="js">SaddOption('cities','6','6');</cmd><cmd cmd="js">SaddOption('cities','Aiudul de Sus','Aiudul de Sus');</cmd></xjx>Fri Sep 12 23:45:15 UTC+0300 2008
SENT [66 bytes]Fri Sep 12 23:45:15 UTC+0300 2008
SENDING REQUESTFri Sep 12 23:45:15 UTC+0300 2008
CALLING: xjxfun: addCitiesURI: http://localhost/v1/dropdown.phpFri Sep 12 23:45:15 UTC+0300 2008
POST: xjxfun=addCities&xjxr=1221252315703&xjxargs[]=Scities&xjxargs[]=S5Fri Sep 12 23:45:15 UTC+0300 2008
INITIALIZING REQUEST OBJECTFri Sep 12 23:45:15 UTC+0300 2008
PREPARING REQUESTFri Sep 12 23:45:15 UTC+0300 2008
PROCESSING PARAMETERS [2]Fri Sep 12 23:45:15 UTC+0300 2008
INITIALIZING REQUESTFri Sep 12 23:45:15 UTC+0300 2008
STARTING XAJAX REQUEST

Can somebody enlighten me PLEEEEEEASE why this doesn't work?

Thanks a zzzillion guys.

Luc

Re: xajax PHP Dynamic Drop Down Menu

Here's an example of cascading, dynamic dropdowns where the onchange of the first dropdown triggers the
generation of the second:

<?php
require_once("xajax_0.5_beta4b/xajax_core/xajax.inc.php");
$xajax = new xajax();

function AddCities($dta)
{
    $province = $dta;
        $objResponse = new xajaxResponse();
        //Here you do the DB query to get this province's citys.
        $s = "<select id=\"city\" name=\"city\">";
        $s .= "<option value=''>--- Select a City ---</option>";
        $s .= "<option value=\"1\">Province $province City 1</option>";
        $s .= "<option value=\"2\">Province $province City 2</option>";
        $s .= "<option value=\"3\">Province $province City 3</option>";
        $s .= "</select>";
        $objResponse->assign("cities", "innerHTML", $s);
    return $objResponse;
}

function ProvinceList()
{
    $s = "<select id=\"province\" name=\"province\" onchange=\"xajax_AddCities(this.value)\">\n";
    $s .= "<option value=''>--- Select a Province ---</option>\n";
    //Here do your DB process to get and build provindce dropdown
    $s .= "<option value=\"1\">Province 1</option>\n";
    $s .= "<option value=\"2\">Province 2</option>\n";
    $s .= "<option value=\"3\">Province 3</option>\n";
    $s .= "</select>\n";
    echo $s;
}
//$xajax->setflag('debug', true);
$xajax->registerFunction('AddCities');
$xajax->processRequest();

?>

<html>
<head>
<title>test</title>
<?php
   $xajax->printJavascript('xajax_0.5_beta4b');
?>

</head>
<body>
<div style="display: inline">
<? ProvinceList(); ?>
</div>
<div style="display: inline" id="cities" name="cities"></div><br><br>
</body>
</html>

Hope this helps...

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

Thank you Ed ...

Works like a charm wink

Luc

Re: xajax PHP Dynamic Drop Down Menu

Hi,

Very sorry about this - I am a relative newbie to classes, functions, javascript, xajax, et al, and have recently tried to get the lists to work, which I have done, but I can't see a value for the item that I've selected in the second select element.

I have main product categories and sub categories.  I do have a DWMX plugin that creates these combos, but I want to move away from relying on DWMX and the Adobe clowns and thought that AJAX was the way forward.  After nearly a week of finding a suitable framework; my hosting provider's still on PHP4.3.1 and MySQL 1 - well, 3.23 really, but it might as well have joined Noah on his travels.... tongue  I do apologise for my absolute ignorance.  It's got to the point where I've been sat there with tears in my eyes with the sheer frustration.  It's like my old school reports: "Could do better". F

The code I've used is (apologies up front for the bastardisation of your source code):

    require_once('src/xajax/xajax_core/xajax.inc.php');
    require_once('library/egedb.php');
    $xajax = new xajax();
    
    function SubCatList($dta) {
        $mcat = $dta;
        $objResponse = new xajaxResponse();
        
        //Here you do the DB query to get this province's citys.
        $query2 = "SELECT id_scat,title_scat FROM subcats WHERE id_mcat='$mcat'";
        $result = mysql_query($query2);
        $s = "<select id=\"scatId\" name=\"scatId\">";
        $s .= "<option value=''>--- Select a Sub Category ---</option>";
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $s .= "<option value=\"".$row['id_scat']."\">".$row['title_scat']."</option>\n";
        }
        $s .= "</select>";
        
        $objResponse->assign("subcats", "innerHTML", $s);
        return $objResponse;
        
        }
        
        function MCatList() {
            $s = "<select id=\"mcatId\" name=\"mcatId\" onchange=\"xajax_SubCatList(this.value)\">\n";
            $s .= "<option value=''>--- Select a Main Category ---</option>\n";
            //Here do your DB process to get and build provindce dropdown
            $query = "SELECT id_mcat, title_mcat FROM maincats";
            $result = mysql_query($query);
                while($row = mysql_fetch_assoc($result)){
            $s .= "<option value=\"".$row['id_mcat']."\">".$row['title_mcat']."</option>\n";
            }
            $s .= "</select>\n";
            echo $s;
            
        }
        
        //$xajax->setflag('debug', true);
        
        $xajax->registerFunction('SubCatList');
        $xajax->processRequest();
?>

<html>
<head>
<title>test</title>
<?php
    $xajax->printJavascript('src/xajax');
?>
</head>

<body>
<form action="xajaxdemo1.php" method="post" name="myForm" >
    <div style="display: inline">
        <? MCatList(); ?>
    </div>
    <div style="display: inline" id="subcats" name="subcats"></div>
    <input type="submit" value="submit">
</form>
The value you selected is <?php echo ($_POST['id_scat']." - ".$_POST['title_scat']); ?>

.

Your help and guidance on this (apart from the usual "...go learn how to program, you bum...!") would be a real help.

BTW - I know this sounds REALLLLLLLLY dumb.... but what should I be looking at first in terms of developing my skills - would focussing on PHP (classes and functions) be more beneficial than looking at, perhaps, JavaScript and AJAX (not the question to ask on an AJAX forum, but I'm sure you've all been in the same boat --- well, some of you at least! wink

Thank you.

Wes

Re: xajax PHP Dynamic Drop Down Menu

BTW, the site I'm building for my client is at http://www.everquip.co.uk.  The pages in question are for the administration section at the back (sorry, can't give you access to it via a link). And, yes, it is all my own work (well apart from the Mootools tips and the Shadowbox effect).

neutral

Wes

Re: xajax PHP Dynamic Drop Down Menu

The $_POST['id_scat'] should be $_POST['scatId'] as that's the name of the subcat SELECT. The $_POST['title_scat'] won't exist.

As for developing your skills, I'd plump for PHP, assuming you have access to PHP5.

I can't believe a hosting site is using MySQL 1 stroke 3.23. Dump them!

Re: xajax PHP Dynamic Drop Down Menu

Hi Wes,

You are pretty close to it.

My original example only went so far. It didn't do anything...

Here's an expanded version of the original that does something:

<?php
/*
    Expansion of the dropdown example to actually do something..
*/
require_once("xajax/xajax_core/xajax.inc.php");
$xajax = new xajax();

//Using the initial choice, generate a second list of choices. 
function AddCities($dta)
{
    $province = $dta;
        $objResponse = new xajaxResponse();
        //Here you do the DB query to get this province's citys.
        $s = "<select id=\"city\" name=\"city\">";
        $s .= "<option value=''>--- Select a City ---</option>";
        $s .= "<option value=\"$province City 1\">Province $province City 1</option>";
        $s .= "<option value=\"$province City 2\">Province $province City 2</option>";
        $s .= "<option value=\"$province City 3\">Province $province City 3</option>";
        $s .= "</select>";
        $objResponse->assign("cities", "innerHTML", $s);
        $objResponse->script("xajax.$('subbtn').disabled = false");
    return $objResponse;
}

//Generate the initial list of choices. 
function ProvinceList()
{
    $s = "<select id=\"province\" name=\"province\" onchange=\"xajax_AddCities(this.value)\">\n";
    $s .= "<option value=''>--- Select a Province ---</option>\n";
    //Here do your DB process to get and build province dropdown
    $s .= "<option value=\"Province 1\">Province 1</option>\n";
    $s .= "<option value=\"Province 2\">Province 2</option>\n";
    $s .= "<option value=\"Province 3\">Province 3</option>\n";
    $s .= "</select>\n";
    echo $s;
}

//Function to carry out the page purpose.
function ProcessTheForm($dta)
{
    $resp = new xajaxResponse();
    $city = $dta['city'];
    //Make sure a city is selected.
    if ($city == '')
    {
        $resp->alert("Please select a city and try again.");
        return $resp;
    }
    /*
        Do something with the data here like book a trip...
    */

    $resp->alert("Your trip has been booked for $city\nThank you for using our service.\n Reference 15469");
    return $resp;
}
//$xajax->setflag('debug', true);
$xajax->registerFunction('AddCities');
$xajax->registerFunction('ProcessTheForm');
$xajax->processRequest();
 
?>
<html>
<head>
<title>Multiple Dropdown Lists Example</title>
<?php
   $xajax->printJavascript('xajax');
?>
</head>
<body>
<center>
<h5>Multiple Drop Down List Example</h5><br>
<!-- NOTE: "return false;" below is manditory to keep the page from reloading. -->
<form id="form1" onsubmit="xajax_ProcessTheForm(xajax.getFormValues('form1')); return false;">
<div style="display: inline">
<? ProvinceList(); ?>
</div>
<div style="display: inline" id="cities" name="cities"></div><br><br>
<!-- Submit button is disabled until a city is selected. -->
<input id="subbtn" type="submit" value="Book Your Trip" disabled>
</form>
</center>
</body>
</html>

Here, after the user selects a province AND city, they submit the form to book a trip.

Notice the <form> tag. It defines a handler for the onsubmit which calls the registered  function to do something for the user. Also notice that the onsubmit follows the call with "return false;" This is important because if you do not in clude it, the registered function is called and the page is immediately reloaded thus defeating the essence of AJAX...

You could use a button input with an onclick event rather than the submit button.

Hope this helps you go forward.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

dickdock wrote:

The $_POST['id_scat'] should be $_POST['scatId'] as that's the name of the subcat SELECT. The $_POST['title_scat'] won't exist.

As for developing your skills, I'd plump for PHP, assuming you have access to PHP5.

I can't believe a hosting site is using MySQL 1 stroke 3.23. Dump them!

Thank you very much for your response, dickdock (makes me think of a clock... ticktock... wink )
I have found a hosting provider in Ireland (http://hosting.digiweb.ie/website-hosti … ced-linux/) who I will certainly be moving my own sites to, will probably recommend them to my client also as that would allow the upgrade from 3.23 and PHP4.3.1 to something more in tune with current web trends...

I'll have a play around with the $_POST value.  Thanks.

BTW, I bought myself the book Ajax: A beginner's guide by Steven Holzner - looks okay (seems to be step-by-step at first inspection).

Perhaps we could have a forum area dedicated to book reviews?

Wes

Re: xajax PHP Dynamic Drop Down Menu

edrobinson wrote:

Hi Wes,

You are pretty close to it.

My original example only went so far. It didn't do anything...

Here's an expanded version of the original that does something:...

...You could use a button input with an onclick event rather than the submit button.

Hope this helps you go forward.

Ed

Thanks for getting back in touch with this.  I simply want to embed the dependent drop downs in a larger form for administrators to add new products, and then update the db with the subcategory only.  You know, cascading deletes and all that (not that I've even got to that part of the site development yet -- then again, you spend a week trying to find a simple solution to a simple problem that works on the world's worst (but highly respectable and well known) hosting solution - they do have more modern versions using PHP5 and MySQL5, but my client has never upgraded to those accounts....).

I have decided to give my brain (as well as my health) a break until next week - so going to "tart up" the admin area a bit and will look at your code over the weekend/early next week.

I'll keep you posted on progress.

Once again, thank you very much for your assistance in this matter.

Wes

Re: xajax PHP Dynamic Drop Down Menu

In the expanded Example that you have here Ed, On the second select can you also have an OnChange event that will repopulate an already previously defined selectbox with one of the options marked as selected


For example I use what you have above.

Province list will populate the Cities select. 
When city list is selected I need to take the city, do a quick query to find the name of a technician based on the selected city.  Once found repopulate a previously built dropdown list with available technicians only mark one "selected" from the quick query accomplished earlier based on the city.

Does that make sense?

Thank you for your ideas.
James

Re: xajax PHP Dynamic Drop Down Menu

You can add an onchange to the second select when it is generated that calls another function to handle the additional stuff. It could do the query and build the new select and put it.

If you want to use the original dropdown position you would probably want to wrap the original select in a div tag with an id value so the new select could be put in it.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

Makes Sense.  Do I need to create a new object response?
The function AddFacility fires and creates that dropdown.  But the third function SetAssignTo does not seem to fire to show the Alert indicating I have made it there. 
Something syntactically incorrect?


require_once("components/com_qatool/xajax_0.5/xajax_core/xajax.inc.php"); //XAJX library
$xajax = new xajax();                           //Instance the xajax class
$xajax->configure('javascript URI', 'components/com_qatool/xajax_0.5/');

//$xajax->configure('debug',true); //Uncomment to use the debuggr



//Basic xajax registered function
function SetAssignTo($facility)

{
        $objResponse2 = new xajaxResponse();       
        alert("Made it inside");
        
        /* 
        $query =  'SELECT SA.massign_id, SA.user_id ';
        $query .= 'FROM #__qatool_supervidor_assign SA '; 
        $query .= 'WHERE SA.modality_id =' .  $modality . ' AND SA.facility_id =' . $facility;   
        $db =& JFactory::getDBO();
        $db->setQuery($query);           */                                                                                                                            
        return $objResponse2; 
}                           
function AddFacility($dta)

{

    $modality = $dta;

        $objResponse = new xajaxResponse();

        //Here you do the DB query to get this province's citys.
        $s = "<select id=\"facility_id\" name=\"facility_id\" onchange=\"xajax_SetAssignTo(this.value)\">\n";
        $s .= "<option value=''>--- Select a Facility ---</option>";
        
        $query =  'SELECT MA.massign_id, MA.modality_id, MA.facility_id, F.facility_id, F.fname, F.active ';
        $query .= 'FROM #__qatool_modality_assign MA ';
        $query .= 'INNER JOIN #__qatool_facility F ON MA.facility_id = F.facility_id ';
        $query .= 'WHERE MA.modality_id =' .  $modality . ' AND F.active = 1 ORDER BY F.fname';   
        $db =& JFactory::getDBO();
        $db->setQuery($query);        
        
        foreach($db->loadAssocList() as $crow)
        {
        $s .= "<option value=\"" . $crow['facility_id']. "\">" . $crow['fname'] . "</option>\n";  
        }
        $s .= "</select>";
        $objResponse->assign("facility_id", "innerHTML", $s);
        //$objResponse->script("xajax.$('subbtn').disabled = false");
    return $objResponse;

}


function ModalityList()

{               
    $s = "<select id=\"modality_id\" name=\"modality_id\" onchange=\"xajax_AddFacility(this.value)\">\n";   
    $s .= "<option value=''>--- Select a Modality ---</option>\n";            
    //Here do your DB process to get and build provindce dropdown
    $query = "SELECT * FROM #__qatool_modality ORDER BY mname";
    $db =& JFactory::getDBO();
    $db->setQuery($query);
    foreach($db->loadAssocList() as $crow)
    {
        $s .= "<option value=\"". $crow['modality_id']. "\">" . $crow['mname'] . "</option>\n";
    }

    $s .= "</select>\n";
    echo $s;
}

$xajax->register(XAJAX_FUNCTION,"AddFacility");        //Register  function with xajax
$xajax->register(XAJAX_FUNCTION,"SetAssignTo");  
$xajax->processRequest();                                          //Invoke the xajax engine. Receives the xajax request.

Re: xajax PHP Dynamic Drop Down Menu

alert("made it inside"); should be $objResponse2->alert("made it inside");

You do need to create a response object inside each registered function or make one globally and use it.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

I  change the Fuction to the following:

function SetAssignTo($modality)

{
        $objResponse2 = new xajaxResponse();       
        $objectResponse2->alert("Made it here");
        return $objResponse2; 
}  

Everything else aboves remains the same.  With the Debug turned on here is the Error that I am getting:

xajax debug output

Thu Apr 01 2010 09:25:49 GMT-0600 (Mountain Daylight Time)
DONE [233ms]
Thu Apr 01 2010 09:25:49 GMT-0600 (Mountain Daylight Time)
ERROR: No response processor is available to process the response from the server.
Content-Type: text/html.
Check for error messages from the server.
Thu Apr 01 2010 09:25:49 GMT-0600 (Mountain Daylight Time)
ERROR: The server returned the following HTTP status: 500
RECEIVED:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>

<title>500 - Internal server error.</title>

<style type="text/css">

<!--

body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}

fieldset{padding:0 15px 10px 15px;} 

h1{font-size:2.4em;margin:0;color:#FFF;}

h2{font-size:1.7em;margin:0;color:#CC0000;} 

h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 

#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;

background-color:#555555;}

#content{margin:0 0 0 2%;position:relative;}

.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}

-->

</style>

</head>

<body>

<div id="header"><h1>Server Error</h1></div>

<div id="content">

 <div class="content-container"><fieldset>

  <h2>500 - Internal server error.</h2>

  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>

 </fieldset></div>

</div>

</body>

</html>

Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
SENT [50 bytes]
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
SENDING REQUEST
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
CALLING: 
xjxfun: SetAssignTo
URI: 
http://10.155.62.74/joomla/index.php?option=com_qatool&view=detail&cid[]=-1&pat_id=45234523452&acc_num=452345234
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
POST: xjxfun=SetAssignTo
&xjxr=1270135548767
&xjxargs[]=S1
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
PREPARING REQUEST
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
PROCESSING PARAMETERS [1]
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
INITIALIZING REQUEST
Thu Apr 01 2010 09:25:48 GMT-0600 (Mountain Daylight Time)
STARTING XAJAX REQUEST

No response processor?  Am I leaving something out?

Re: xajax PHP Dynamic Drop Down Menu

The server returned a 500 - internal server error - to the browser when it was expecting an xajax xml response. Thus the "No Response Processor" error.

The debug display is just what the xajax js received from the server.

Looks like there is a problem on your server. It's probable been there since you started messing around with this dropdown thing. You never saw it until you turned on the debugger - just didn't see anything.

This might be worth noting:
"There is a problem with the resource you are looking for, and it cannot be displayed."

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

hello there, first of all I would like to thank for such a useful forum. I found out xajax quite handy for working with ajax technology though I've been stuck for a while with my code trying to generate a dynamic drop down. I'm learning from your examples but yet I can not find the problem here. I'm getting this error with the debug turned on.

Mon Sep 24 2012 15:52:24 GMT-0400 (Hora de verano oriental)
ERROR: No response processor is available to process the response from the server.
Content-Type: text/html.
Check for error messages from the server.

Would you mind helping me by taking a look to my code. I'll highly appreciate your time. Thanks in advance ARC.

<?php 
session_start();
ob_start();
require('../libraries/xajax/xajax_core/xajax.inc.php');
include('enterprise.php');
include('cnntest.php');
$xajax = new xajax();
$xajax->configure('javascript URI', '../libraries/xajax/');
$xajax->configure('debug',true);
function AddEnterprises($dta){
    $entgroup = $dta;
    $objResponse = new xajaxResponse();
    $s = "<select id=\"ent\" name=\"ent\">";
    $s .= "<option value=\"base\">Seleccione Empresa</option>";
    $consult1="select * from tblenterprise where ententerprisegroupid = $entgroup";
    $entlist = $db->Execute($consult1);
    while (!$entlist->EOF)
     {
       $s .= "<option value=\".$entlist->fields[0].\">\".$entlist->fields[4].\"</option>";
       $entlist->MoveNext();
     }    
    $s .= "</select>";
    $objResponse->assign("enterprises", "innerHTML", $s);
    $objResponse->script("xajax.$('subbtn').disabled = false");
    return $objResponse;
}
function EntGroupList($db){
    $s = "<select id=\"entgroup\" name=\"entgroup\" onchange=\"xajax_AddEnterprises(this.value)\">\n";
    $s .= "<option value=\"base\">Seleccione Grupo</option>\n";
    $consult="select * from pltblenterprisegroup";
    $entgrouplist = $db->Execute($consult);
    while (!$entgrouplist->EOF) 
    {
       $s .= "<option value=".$entgrouplist->fields[0].">".$entgrouplist->fields[1]."</option>";
       $entgrouplist->MoveNext();
     }    
    $s .= "</select>";
    echo $s;
}
 function ProcessTheForm($dta){
    $resp = new xajaxResponse();
    $ent = $dta['ent'];
    if ($ent == '')
    {
        $resp->alert("Please select an enterprise and try again.");
        return $resp;
    }
}
$xajax->registerFunction('AddEnterprises');
$xajax->registerFunction('ProcessTheForm');
$xajax->setflag('debug', true);
$xajax->processRequest();
?>
<html>   
<head>
<?php
$xajax->printJavascript("../libraries/xajax/");
?>   
</head>
<body>
<center>
<h5>Multiple Drop Down List Example</h5><br>
 <!-- NOTE: "return false;" below is manditory to keep the page from reloading. -->
<form id="form1" onsubmit="xajax_ProcessTheForm(xajax.getFormValues('form1')); return false;">
<div style="display: inline">
<?php EntGroupList($db); ?>
</div>
<div style="display: inline" id="enterprises" name="enterprises"></div><br><br>
<!-- Submit button is disabled until a city is selected. -->
<input id="subbtn" type="submit" value="Buscar" disabled>
</form>
</center>
</body>
</html>

Re: xajax PHP Dynamic Drop Down Menu

The only thing I see is that it looks like there is a blank line before the <?php tag and possibly one after the ?> tag.

Also, since you configured the javascript URI in the PHP, you do not need it in the printJavascript() call in the html head area. This is not your problem, however, it just saves a bit of typing.

Use an editor that will show non-printing characters and see if there are any before of after your php tags.

Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

Hi, thank you very much for your answer. I've already checked my code with notepad++ and there is no leading or trailing white spaces. I also  changed the printJavascript() call in the html to
<?php
$xajax->printJavascript();
?>
since I had already configured the javascript URI. However, It remains coming up with the same error. Could this be something related to the server I'm using? XAMPP 1.73. Could it be the php version? PHP 5.3.1
I wouldn't like to quite using xajax because I believe it's easier to implement, but I've been stuck for a while and it doesn't get better. Any last advise?

Thanks for your time and dedication, ARC.

The whole error message

xajax debug output
Wed Sep 26 2012 11:54:29 GMT-0400 (Hora de verano oriental)

DONE [97ms]

Wed Sep 26 2012 11:54:29 GMT-0400 (Hora de verano oriental)

ERROR: No response processor is available to process the response from the server.
Content-Type: text/html.
Check for error messages from the server.

Wed Sep 26 2012 11:54:29 GMT-0400 (Hora de verano oriental)

RECEIVED [status: 200, size: 3513 bytes, time: 96ms]:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>



<body>



</body>

</html><br />
<font size='1'><table dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Call to a member function Execute() on a non-object in C:\xampp\htdocs\cnicproject\source\ajaxtest.php on line <i>15</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0001</td><td bgcolor='#eeeeec' align='right'>326608</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\xampp\htdocs\cnicproject\source\ajaxtest.php' bgcolor='#eeeeec'>..\ajaxtest.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0933</td><td bgcolor='#eeeeec' align='right'>731608</td><td bgcolor='#eeeeec'>xajax->processRequest(  )</td><td title='C:\xampp\htdocs\cnicproject\source\ajaxtest.php' bgcolor='#eeeeec'>..\ajaxtest.php<b>:</b>48</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0933</td><td bgcolor='#eeeeec' align='right'>731752</td><td bgcolor='#eeeeec'>xajaxPluginManager->processRequest(  )</td><td title='C:\xampp\htdocs\cnicproject\libraries\xajax\xajax_core\xajax.inc.php' bgcolor='#eeeeec'>..\xajax.inc.php<b>:</b>513</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>4</td><td bgcolor='#eeeeec' align='center'>0.0933</td><td bgcolor='#eeeeec' align='right'>732368</td><td bgcolor='#eeeeec'>xajaxFunctionPlugin->processRequest(  )</td><td title='C:\xampp\htdocs\cnicproject\libraries\xajax\xajax_core\xajaxPluginManager.inc.php' bgcolor='#eeeeec'>..\xajaxPluginManager.inc.php<b>:</b>241</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>5</td><td bgcolor='#eeeeec' align='center'>0.0933</td><td bgcolor='#eeeeec' align='right'>732680</td><td bgcolor='#eeeeec'>xajaxUserFunction->call(  )</td><td title='C:\xampp\htdocs\cnicproject\libraries\xajax\xajax_core\plugin_layer\xajaxFunctionPlugin.inc.php' bgcolor='#eeeeec'>..\xajaxFunctionPlugin.inc.php<b>:</b>221</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>6</td><td bgcolor='#eeeeec' align='center'>0.0934</td><td bgcolor='#eeeeec' align='right'>732744</td><td bgcolor='#eeeeec'><a href='http://www.php.net/call_user_func_array' target='_new'>call_user_func_array</a>
(  )</td><td title='C:\xampp\htdocs\cnicproject\libraries\xajax\xajax_core\plugin_layer\support\xajaxUserFunction.inc.php' bgcolor='#eeeeec'>..\xajaxUserFunction.inc.php<b>:</b>230</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>7</td><td bgcolor='#eeeeec' align='center'>0.0934</td><td bgcolor='#eeeeec' align='right'>732928</td><td bgcolor='#eeeeec'>AddEnterprises(  )</td><td title='C:\xampp\htdocs\cnicproject\libraries\xajax\xajax_core\plugin_layer\support\xajaxUserFunction.inc.php' bgcolor='#eeeeec'>..\xajaxUserFunction.inc.php<b>:</b>0</td></tr>
</table></font>

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

SENT [53 bytes]

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

SENDING REQUEST

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

CALLING:
xjxfun: AddEnterprises
URI:
http://localhost/cnicproject/source/ajaxtest.php

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

POST: xjxfun=AddEnterprises
&xjxr=1348674868906
&xjxargs[]=S8

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

INITIALIZING REQUEST OBJECT

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

PREPARING REQUEST

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

PROCESSING PARAMETERS [1]

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

INITIALIZING REQUEST

Wed Sep 26 2012 11:54:28 GMT-0400 (Hora de verano oriental)

STARTING XAJAX REQUEST


<?php
session_start();
ob_start();
require('../libraries/xajax/xajax_core/xajax.inc.php');
include('cnntest.php');
$xajax = new xajax();
$xajax->configure('javascript URI', '../libraries/xajax/');
$xajax->configure('debug',true);
function AddEnterprises($dta){
    $entgroup = $dta;
    $objResponse = new xajaxResponse();
    $s = "<select id=\"ent\" name=\"ent\">";
    $s .= "<option value=\"base\">Seleccione Empresa</option>";
    $consult1="select * from tblenterprise where ententerprisegroupid = $entgroup";
    $entlist = $db->Execute($consult1);
    foreach($entlist as $k => $list){
       $s .= "<option value=\".$list->fields[0].\">\".$list->fields[4].\"</option>";
     }
    $s .= "</select>";
    $objResponse->assign("enterprises", "innerHTML", $s);
    $objResponse->script("xajax.$('subbtn').disabled = false");
    return $objResponse;
}
function EntGroupList($db){
    $s = "<select id=\"entgroup\" name=\"entgroup\" onchange=\"xajax_AddEnterprises(this.value)\">\n";
    $s .= "<option value=\"base\">Seleccione Grupo</option>\n";
    $consult="select * from pltblenterprisegroup";
    $entgrouplist = $db->Execute($consult);
    while(!$entgrouplist->EOF)
    {
       $s .= "<option value=".$entgrouplist->fields[0].">".$entgrouplist->fields[1]."</option>";
       $entgrouplist->MoveNext();
    }
    $s .= "</select>";
    echo $s;
}
 function ProcessTheForm($dta){
    $resp = new xajaxResponse();
    $ent = $dta['ent'];
    if ($ent == '')
    {
        $resp->alert("Please select an enterprise and try again.");
        return $resp;
    }
}
$xajax->registerFunction('AddEnterprises');
$xajax->registerFunction('ProcessTheForm');
$xajax->processRequest();
?>
<html>
<head>
<?php
$xajax->printJavascript();
?>
</head>
<body>
<center>
<h5>Multiple Drop Down List Example</h5><br>
 <!-- NOTE: "return false;" below is manditory to keep the page from reloading. -->
<form id="form1" onsubmit="xajax_ProcessTheForm(xajax.getFormValues('form1')); return false;">
<div style="display: inline">
<?php
EntGroupList($db);
?>
</div>
<div style="display: inline" id="enterprises" name="enterprises"></div><br><br>
</form>
</center>
</body>
</html>

Re: xajax PHP Dynamic Drop Down Menu

Hi, it's me again. I guess the problem is that I need to pass $db(the connection string) to AddEnterprises($dta) function which is the one that isn't appear when selecting an option in the first drop down. The problem is that this function is called in the onchange event of the select
$s = "<select id=\"entgroup\" name=\"entgroup\" onchange=\"xajax_AddEnterprises(this.value)\">\n"; so I must include it here as well right?
When I do it this way:
$s = "<select id=\"entgroup\" name=\"entgroup\" onchange=\"xajax_AddEnterprises(this.value".$db.")\">\n";
I get this error message:
Catchable fatal error: Object of class ADODB_postgres7 could not be converted to string in C:\xampp\htdocs\cnicproject\source\ajaxtest.php on line 25

Is there any way I could pass $db as parameter of the function? Because I need it to establish the connection with the data base.

Thanks a bunch... I look forward to hearing from you. ARC

Re: xajax PHP Dynamic Drop Down Menu

Yes. That won't work. $db is probably a database object.

Why can't you establish the database connection when AddEnterprises() runs?

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

I tried all the solution above after setting up xajax properly with 0.6.1 beta. If would help everyone to offer a zip file with examples as ready-to-go.

For e.g. a dependent dropdown for countries, cities and cities based on a file and mysql based connection. Once the admin have two of these examples zip it becomes very easy to further customize instead of going through a time comsuming crash course of fundamentals.

Re: xajax PHP Dynamic Drop Down Menu

Hello architekt_berlin,

I just wrote the following script to demonstrate the dynamic dropdown using xajax 0.6.

<?php
/*
    Dynamic dropwowns with xajax
    7/5/2016
    
    This script uses xajax 0.6 to handle 
    populating 2 dropdowns on the browser.
    The first contains states and the second 
    contains cities in the selected state.
*/    
require 'xajax-master/xajax_core/xajax.inc.php';
$xajax = new Xajax();
$xajax->configure('javascript URI', 'xajax-master/xajax_js/');
$xajax->register(XAJAX_FUNCTION,'getStates');
$xajax->register(XAJAX_FUNCTION,'getCities');
$xajax->processRequest();

function setupDb()
{
    include("dbsettings.php");
      
      $con = mysql_connect($dbhost,$dbuser,$dbpassword);
      if($con){
        mysql_select_db($dbdatabase,$con);
        return $con;
      }
      else{
        die("Could not connect to database");
      }    
}

//Populate the state select
function getStates()
{
    $con = setupDb();
    $resp = new xajaxResponse();
    $sql = "select distinct state_name from zip_code order by state_name";
    $states = mysql_query($sql);
    if(!$states)
    {
        $resp->alert('SQL Query Failed. '.$sql);
        return $resp;
    }
    $s = '<select id="states" name="states" onchange="xajax_getCities(this.value)">';

    while($row = mysql_fetch_array($states))
    {
        $state = $row['state_name'];
        $s .= "<option value='$state'>$state</option>";
    }
    $s .= '</select>';
    
    
    $resp->assign('statesdiv','innerHTML',$s);
    return $resp;
}

//Populate city select
function getCities($state)
{
    $con = setupDb();
    $resp = new xajaxResponse();
    
    $sql = "select distinct city from zip_code where state_name = '$state' order by city";
    $cities = mysql_query($sql);
    if(!$cities)
    {
        $resp->alert('SQL Query Failed. '.$sql);
        return $resp;
    }

    $s = '<select id="cities" name="cities">';

    while($row = mysql_fetch_array($cities))
    {
        $city = $row['city'];
        $s .= "<option value='$city'>$city</option>";
    }
    $s .= '</select>';
    
    $resp->assign('citydiv','innerHTML',$s);
    return $resp;
}
?>
<!doctype html>
<html>
<head>
<title>Xajax Dynamic Dropdowns</title>
<?php $xajax->printJavascript(); ?>
<style>
    div{
        display: inline-block;
        margin-top: 50px;
        width: 400px;
        height: 12px;
    }
</style>
</head>
<body onload="xajax_getStates();">

States:<div id="statesdiv"></div> Cities:<div id="citydiv"></div>
</body>
</html>    
If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

Here's the same functionallity as a class:

<?php
/*
    Dynamic dropwowns with xajax - oo form
    7/5/2016
    
    This script uses xajax 0.6 to handle 
    populating 2 dropdowns on the browser.
    The first contains states and the second 
    contains cities in the selected state.
*/    
require 'xajax-master/xajax_core/xajax.inc.php';

class xajaxDynamicDropdowns
{
    function setupDb()
    {
        include("dbsettings.php");
          
          $con = mysql_connect($dbhost,$dbuser,$dbpassword);
          if($con){
            mysql_select_db($dbdatabase,$con);
            return $con;
          }
          else{
            die("Could not connect to database");
          }    
    }

    //Populate the state select
    function getStates()
    {
        $con = $this->setupDb();
        $resp = new xajaxResponse();
        $sql = "select distinct state_name from zip_code order by state_name";
        $states = mysql_query($sql);
        if(!$states)
        {
            $resp->alert('SQL Query Failed. '.$sql);
            return $resp;
        }
        $s = '<select id="states" name="states" onchange="xajax_xajaxDynamicDropdowns.getCities(this.value)">';

        while($row = mysql_fetch_array($states))
        {
            $state = $row['state_name'];
            $s .= "<option value='$state'>$state</option>";
        }
        $s .= '</select>';
        
        
        $resp->assign('statesdiv','innerHTML',$s);
        return $resp;
    }

    //Populate city select
    function getCities($state)
    {
        $con = $this->setupDb();
        $resp = new xajaxResponse();
        
        $sql = "select distinct city from zip_code where state_name = '$state' order by city";
        $cities = mysql_query($sql);
        if(!$cities)
        {
            $resp->alert('SQL Query Failed. '.$sql);
            return $resp;
        }

        $s = '<select id="cities" name="cities">';

        while($row = mysql_fetch_array($cities))
        {
            $city = $row['city'];
            $s .= "<option value='$city'>$city</option>";
        }
        $s .= '</select>';
        
        $resp->assign('citydiv','innerHTML',$s);
        return $resp;
    }
}

    $xajax = new Xajax();
    $xajax->configure('javascript URI', 'xajax-master/xajax_js/');
    $drop = new xajaxDynamicDropdowns();
    $xajax->register(XAJAX_CALLABLE_OBJECT, $drop);
    $xajax->processRequest();
?>
<!doctype html>
<html>
<head>
<title>Xajax Dynamic Dropdowns</title>
<?php $xajax->printJavascript(); ?>
<style>
    div{
        display: inline-block;
        margin-top: 50px;
        width: 400px;
        height: 12px;
    }
</style>
</head>
<body onload="xajax_xajaxDynamicDropdowns.getStates();">

States:<div id="statesdiv"></div> Cities:<div id="citydiv"></div>
</body>
</html>    
If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: xajax PHP Dynamic Drop Down Menu

Hi edrobinson,

You are just brilliant.

As I was extremely busy, I thought I would answer to your thread. However, things I am doing keeps on extending itself and gives me lots of troubles. Thus, I could not start that project where I could integrate your solution. I cannot even start it now as I need much more time to have the entire environment ready and to work with it.

Thus, I came here to let you know that I am extremely thankful to you for your attempt to help me out. I am sure it will help me. I will definately come here again and place a message when I have prepared the environment and done some testing.

Thanks again so much...

Re: xajax PHP Dynamic Drop Down Menu

You are very welcome.
Thank you for the kind comments.
Ed

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.