Jump to content

Drop Down


dc_jt

Recommended Posts

Hi Im using this tutorial to create two drop downs (the second depending on the whats selected from the first) using this page:

 

http://www.ajax-tutorials.com/tutorial-list/resource/AJAX_Dynamic_Drop_Down_List_PHP_xajax/view.php

 

I have got the two drop downs working however I want to be able to use the value of the second drop down for something else but not sure how I can retrieve this.

 

E.g Ive got drop down 1 consisting of regions - 'North West','North East','South' and if I select 'North West' I get the options of 'Liverpool','Manchester','Preston'.

 

Now say I select Liverpool, how can I use this value to do something else? I dont want another dropdown I basically want to use this Id to collect info about Liverpool.

 

Please see my code below:

 

require($_SERVER['DOCUMENT_ROOT'].'/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."');");
       }
     }
  }
}
//get regions
$aRegion = array();
	foreach($aRegions as $oRegion){
		array_push($aRegion, $oRegion['region_id']);
	}

  //get stores
     $aStore = array();
	foreach ($aRegion as $rRegion){
		$aStores = $oTblStores->GetAjaxStoresByLocation($rRegion);

     		$aStore[$rRegion] = array();
     		if($aStores)
		foreach ($aStores as $oStore){

			array_push($aStore[$rRegion], $oStore['Store']);
		}

	}

// adds an option to the select 
      function addStores($selectId, $aRegion) {

        global $aStore;
        $objResponse = new myXajaxResponse();
        $data = $aStore[$aRegion];
        $objResponse->addCreateOptions($selectId, $data);
	//return $objResponse->getXML();
			return $objResponse;
      }
      function addHours($selectId, $transport) {
      	
        global $aHour;
        $objResponse = new myXajaxResponse();
        $data = $aHour[$transport];
        $objResponse->addCreateOptions($selectId, $data);
        //return $objResponse->getXML();
			return $objResponse;
      }
      $xajax->registerFunction("addStores");
      $xajax->registerFunction("addHours");
      $xajax->processRequest();
      ?>
      <?
      if (isset($_POST['Submit'])) {
        print_r($_POST);
      }

 

Javascript

<?
      $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>

 

Drop down html code

<div class="ordering-step">
          <div class="step-col step-col-border-right"><h2>Select store for collection</h2>    
         <form name="form1" method="POST" action="">
		<input name="sMode" type="hidden" value="save">
		<input name="region" id="region" type="hidden" value="">

		<label for="region">Choose Region</label>
      <select name="region_id" id="region_id" onchange="xajax_addStores('store', document.form1.region_id.value)">
        <option value="">Select Region</option>

        <?php while ($oRegion = mysql_fetch_object($rRegions)) {?>
        <option value="<?= $oRegion->region_id?>"><?= $oRegion->name?></option>
        <? } ?>
      </select><br clear="all" />
      
	<label for="store">Choose Store</label>
      <select name="store" id="store" onchange="xajax_addHours('hours', document.form1.store.value)">
<option value="">Select Store</option>
      </select>
      </form>

 

Any idea how I get the Id of the second drop down to use somewhere else?

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.