Ajax and php dynamic dropdown help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ccrevling
Forum Newbie
Posts: 19
Joined: Mon Aug 06, 2007 1:34 pm

Ajax and php dynamic dropdown help

Post by ccrevling »

Hello i am trying to impliment a query for this ajax code but i cant seem to get it to work. i think that my problem is that i dont know enough about arrays and whatnot so i need some assistance...
Here is the code that i am trying to get to work

Code: Select all

 
<?
include("includes/global.inc.php");
include("classes/db.inc.php");
    $db = new db();
include("includes/functions.inc.php");
require('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."');");
       }
     }
  }
}
 $country = $db->select("SELECT * FROM cc_streamrush_iso_countries");
    for($i=0;$i<count($country); $i++){ 
      $contry = array($country[$i]['printable_name']);
    }
        $mode = array(
        $contry
        );
      $transports = array(
        'air'=>array('plane','helicopter','air-baloon'),
        'water'=>array('boat','watersky','submarine'),
      );
      $brands = array(
        'plane'=>array('Boeing707', 'F-16', 'Columbia'),
        'helicopter'=>array('BayWatch Heli', 'Police Heli'),
        'air-baloon'=>array('Zeppelin'),
        'boat'=>array('Baywatch Safeguard Boat', 'JetBoat'),
        'watersky'=>array('JetSky','Board-Sky'),
        'submarine'=>array('K-19'),
        'motorcycle'=>array("Yamaha", "Harley Davidson", "Ducati"),
        'car'=>array("Hummer", "Renault", "Porsche")
      );
 
      // adds an option to the select 
      function addTransports($selectId, $mode) {
        global $transports;
        $objResponse = new myXajaxResponse();
        $data = $transports[$mode];
        $objResponse->addCreateOptions($selectId, $data);
//        return $objResponse->getXML();
                return $objResponse;
      }
      function addBrands($selectId, $transport) {
        global $brands;
        $objResponse = new myXajaxResponse();
        $data = $brands[$transport];
        $objResponse->addCreateOptions($selectId, $data);
        //return $objResponse->getXML();
                return $objResponse;
          }
      $xajax->registerFunction("addTransports");
      $xajax->registerFunction("addBrands");
      $xajax->processRequest();
      ?>
      <?
      if (isset($_POST['Submit'])) {
        print_r($_POST);
      }
      ?>
      <html>
      <head>
      <title>AJAX Dynamic Drop Down Tutorial</title>
      <?
      $xajax->printJavascript("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>
      <?php
      ?>
      <form name="form1" method="POST" action="">
      Type : 
      <select name="type" id="type" 
onchange="xajax_addTransports('transports', document.form1.type.value)">
        <option value="">--select--</option>
        <? foreach ($mode as $mod) { ?>
        <option value="<?= $mod?>"><?= $mod?></option>
        <? } ?>
      </select>
      Transportation : 
      <select name="transports" id="transports"
 onchange="xajax_addBrands('brands', document.form1.transports.value)">
<option value="">--select--</option>
      </select>
      Brands : 
      <select name="brands" id="brands"><option value="">--select--</option>
      </select>
      <input type="submit" value="Submit" name="Submit" id="Submit">
      </form>
      </body>
      </html>
 
ok so here is the part that i am trying to get the query to work for

Code: Select all

 
 $country = $db->select("SELECT * FROM cc_streamrush_iso_countries");
    for($i=0;$i<count($country); $i++){ 
      $contry = array($country[$i]['printable_name']);
    }
        $mode = array(
        $contry
        );
      $transports = array(
        'air'=>array('plane','helicopter','air-baloon'),
        'water'=>array('boat','watersky','submarine'),
      );
      $brands = array(
        'plane'=>array('Boeing707', 'F-16', 'Columbia'),
        'helicopter'=>array('BayWatch Heli', 'Police Heli'),
        'air-baloon'=>array('Zeppelin'),
        'boat'=>array('Baywatch Safeguard Boat', 'JetBoat'),
        'watersky'=>array('JetSky','Board-Sky'),
        'submarine'=>array('K-19'),
        'motorcycle'=>array("Yamaha", "Harley Davidson", "Ducati"),
        'car'=>array("Hummer", "Renault", "Porsche")
      );
 
ok so when i load the page up i just see Array in the drop down list (not alot but one) and i am not sure how to make it so that when i do the for it loads it in the $mode = array('country1','contry2');
does this make any sense?

i guess what i am asking is how can i get the query to represent it self in the $mode area if it is not in the for statement?
Post Reply