---
title: "typo3 und xajax dropdown listen"
url: "https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen"
source: "typo3.net"
tags: ["Extensions","Entwicklung","Frage","JavaScript & AJAX","PHP"]
created: "2007-05-26"
last_reply: "2007-06-07"
replies: 5
solved: false
page: 1
pages: 1
lang: "de"
---

# typo3 und xajax dropdown listen

Extensions › Entwicklung · 5 Antworten · gestartet 2007-05-26

## Eröffnungsbeitrag

**dirk01** · 2007-05-26 · [Beitrag #1](https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen/1)

Hallo ich bin nicht so fit in dem Theama. Kann mir bitte jemand helfen dropdown lists in typo3 zu implementieren. Es müssen "nur" die nachfolgenden zwei Codes zusammengeführt werden.

Ich habe eine Extension im Kickstarter erstellt und möchte die Klassendatei (class.tx_xxx__p1.php) modifizieren.

Wenn das geschafft ist, möchte ich die statischen Arrays durch DB Abfragen ersetzen.

Danke schon mal, falls sich ein Hilfsbereiter Typo3ler finder!!!

Gruß,
Dirk

```php
<?php
require_once(PATH_tslib.'class.tslib_pibase.php');

class tx_productfinder_pi1 extends tslib_pibase {
	var $prefixId = 'tx_productfinder_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_productfinder_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey = 'productfinder';	// The extension key.
	var $pi_checkCHash = TRUE;
	
	/**
	 * [Put your description here]
	 */
  var $xajax;
  function main($content,$conf)	{
    $this->conf=$conf;
    $this->pi_setPiVarDefaults();
    $this->pi_loadLL();
    $this->pi_USER_INT_obj = 1;
    $content = '';

    require_once(t3lib_extMgm::extPath('xajax') . 'class.tx_xajax.php');
    $this->xajax = t3lib_div::makeInstance('tx_xajax');
    $this->xajax->setWrapperPrefix($this->prefixId);
  
    $this->xajax->registerFunction(array('ladeUndZeigeZeit', &$this, 'ladeUndZeigeZeit'));

    $this->xajax->processRequests();

    $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] = $this->xajax->getJavascript(t3lib_extMgm::siteRelPath('xajax'));
    $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] .= '<script type="text/javascript">
      window.onload = function() {
        window.setInterval("' . $this->prefixId . 'ladeUndZeigeZeit();", 1000);
      }</script>';

    $content .= '<div>Serverzeit: <span id="Serverzeit"></span></div>';
    return $this->pi_wrapInBaseClass($content);
  }
  function ladeUndZeigeZeit() {
    $content = date("H:i:s");
    $antwort = new tx_xajax_response();
    $antwort->addAssign("Serverzeit", "innerHTML", $content);
    return $antwort->getXML();    
  }

}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/productfinder/pi1/class.tx_productfinder_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/productfinder/pi1/class.tx_productfinder_pi1.php']);
}

?>
```

Nachfolgender Quellcode ist von hier: <http://www.fornewbie.com/article/php/create_ajax_dynamic_drop_down_list_using_php_-_xajax.html>

```php
<?php
require_once('xajax.inc.php');
class myXajaxResponse extends xajaxResponse  {
  function addCreateOptions($sSelectId, $options) {
    $this->addScript("document.getElementById('".$sSelectId."').length=1");
    if (sizeof($options) >0) {
       foreach ($options as $option) {
         $this->addScript("addOption('".$sSelectId."','".$option."','".$option."');");
       }
     }
  }
}
?>
<?
$mode = array('air','water','land');
$transports = array(
  'air'=>array('plane','helicopter','air-baloon'),
  'water'=>array('boat','watersky','submarine'),
  'land'=>array('motorcycle','car')
);
$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")
);
$xajax = new xajax();
//$xajax->debugOn();
 
// 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();
}
function addBrands($selectId, $transport) {
  global $brands;
  $objResponse = new myXajaxResponse();
  $data = $brands[$transport];
  $objResponse->addCreateOptions($selectId, $data);
  return $objResponse->getXML();
}
$xajax->registerFunction("addTransports");
$xajax->registerFunction("addBrands");
$xajax->processRequests();
?>
<?
if (isset($_POST['Submit'])) {
  print_r($_POST);
}
?>
<html>
<head>
<title>AJAX Dynamic Drop Down Tutorial</title>
<?
$xajax->printJavascript();
?>
<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="">
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>
```

## Antworten

### [#2](https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen/2) · dirk01 · 2007-05-28

Hallo,

hier ein erstes Ergebnis. Die erste Dropdown Liste wird gefüllt. Bei den letzten beiden passiert nichts.

```php
<?php
require_once(PATH_tslib.'class.tslib_pibase.php');

class tx_productfinder_pi1 extends tslib_pibase {
	var $prefixId = 'tx_productfinder_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_productfinder_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey = 'productfinder';	// The extension key.
	var $pi_checkCHash = TRUE;
	

	/**
	 * [Put your description here]
	 */
  var $xajax;
  function main($content,$conf)	{
    $this->conf=$conf;
    $this->pi_setPiVarDefaults();
    $this->pi_loadLL();
    $this->pi_USER_INT_obj = 1;
    $content = '';

    require_once(t3lib_extMgm::extPath('xajax') . 'class.tx_xajax.php');
    $this->xajax = t3lib_div::makeInstance('tx_xajax');
    $this->xajax->setWrapperPrefix($this->prefixId);
  	
	$mode = array('air','water','land');
	$transports = array(
	  'air'=>array('plane','helicopter','air-baloon'),
	  'water'=>array('boat','watersky','submarine'),
	  'land'=>array('motorcycle','car')
	);
	$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")
	);
	//$this->xajax = new xajax();
    
  
	$this->xajax->registerFunction('addTransports');
	$this->xajax->registerFunction('addBrands');
	$this->xajax->processRequests();

	if (isset($_POST['Submit'])) {
	  print_r($_POST);
	}

    $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] = $this->xajax->getJavascript(t3lib_extMgm::siteRelPath('xajax'));
	$GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] .= '<script type="text/javascript">
          function addOption(selectId, val, txt) {
	      var objOption = new Option(txt, val);
	      document.getElementById(selectId).options.add(objOption);
		  }
		  </script>';

	$content .= '
		<form name="form1" method="POST" action="">
		Type : 
		<select name="type" id="type" onchange="addTransports(\'transports\', document.form1.type.value)">
		<option value="">--select--</option>
			';
	foreach($mode as $mod) {
	$content .= '<option value=" '. $mod .' "> '. $mod .' </option> ';
			};			
	$content .= 
		'</select>
		Transportation : 
		<select name="transports" id="transports" onchange="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>
	';
	
	return $this->pi_wrapInBaseClass($content);
  }
 
	function addTransports($selectId, $mode) {
	  global $transports;
	  $objResponse = new tx_xajax_response();
	  $data = $transports[$mode];
	  $objResponse->addCreateOptions($selectId, $data);	  
	return $objResponse->getXML();
	}
  
	function addBrands($selectId, $transport) {
	    global $brands;
	  $objResponse = new tx_xajax_response();
	  $data = $brands[$transport];
	  $objResponse->addCreateOptions($selectId, $data);
	return $objResponse->getXML();
	}
	
	function addCreateOptions($sSelectId, $options) {
    $this->addScript("document.getElementById('".$sSelectId."').length=1");
    if (sizeof($options) >0) {
       foreach ($options as $option) {
         $this->addScript("addOption('".$sSelectId."','".$option."','".$option."');");
		   }
		}
	}

}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/productfinder/pi1/class.tx_productfinder_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/productfinder/pi1/class.tx_productfinder_pi1.php']);
}

?>
```

### [#3](https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen/3) · dirk01 · 2007-05-28

Hallo,

nun bereitet mir die Fehlermeldung "Call to undefinded function: addcreateoptions()" nach der Wahl im ersten DropDown Menu Probleme.
Weiß jemand weiter?

```php
<?php
require_once(PATH_tslib.'class.tslib_pibase.php');

class tx_productfinder_pi1 extends tslib_pibase {
	var $prefixId = 'tx_productfinder_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_productfinder_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey = 'productfinder';	// The extension key.
	var $pi_checkCHash = TRUE;
	

	/**
	 * [Put your description here]
	 */
  var $xajax;
  function main($content,$conf)	{
    $this->conf=$conf;
    $this->pi_setPiVarDefaults();
    $this->pi_loadLL();
    $this->pi_USER_INT_obj = 1;
    $content = '';

    require_once(t3lib_extMgm::extPath('xajax') . 'class.tx_xajax.php');
    $this->xajax = t3lib_div::makeInstance('tx_xajax');
    $this->xajax->setWrapperPrefix($this->prefixId);
  	
	$mode = array('air','water','land');
	$transports = array(
	  'air'=>array('plane','helicopter','air-baloon'),
	  'water'=>array('boat','watersky','submarine'),
	  'land'=>array('motorcycle','car')
	);
	$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")
	);

	// Instantiate the tx_xajax object
    $this->xajax = new tx_xajax();
    
	$this->xajax->registerFunction(array('addTransports',&$this,'addTransports'));
	$this->xajax->registerFunction(array('addBrands',&$this,'addBrands'));
	$this->xajax->processRequests();

	if (isset($_POST['Submit'])) {
	  print_r($_POST);
	}

    $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] = $this->xajax->getJavascript(t3lib_extMgm::siteRelPath('xajax'));
	$GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] .= '<script type="text/javascript">
          function addOption(selectId, val, txt) {
	      var objOption = new Option(txt, val);
	      document.getElementById(selectId).options.add(objOption);
		  }
		  </script>';

	$content .= '
		<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) {
	$content .= '<option value=" '. $mod .' "> '. $mod .' </option> ';
			};			
	$content .= 
		'</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>
	';
	
	return $this->pi_wrapInBaseClass($content);
  }
 
	function addTransports($selectId, $mode) {
	  global $transports;
	  $objResponse = new tx_xajax_response();
	  $data = $transports[$mode];
	  $objResponse->addCreateOptions($selectId, $data);	  
	return $objResponse->getXML();
	}
  
	function addBrands($selectId, $transport) {
	  global $brands;
	  $objResponse = new tx_xajax_response();
	  $data = $brands[$transport];
	  $objResponse->addCreateOptions($selectId, $data);
	return $objResponse->getXML();
	}
	
	function addCreateOptions($sSelectId, $options) {
    $this->addScript("document.getElementById('".$sSelectId."').length=1");
    if (sizeof($options) >0) {
       foreach ($options as $option) {
         $this->addScript("addOption('".$sSelectId."','".$option."','".$option."');");
		   }
		}
	}
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/productfinder/pi1/class.tx_productfinder_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/productfinder/pi1/class.tx_productfinder_pi1.php']);
}

?>
```

### [#4](https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen/4) · steffenk · 2007-05-29

Du solltest Dir auch den richtigen Bereich aussuchen, sonst führst Du hier nur Selbstgespräche ;)
ich verschieb mal ...

### [#5](https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen/5) · 2kk · 2007-05-29

Hallo Dirk versuche mal eine hint zu geben

Zeile 90

$objResponse->addCreateOptions($selectId, $data);

diese funktion ist ja in deiner Klasse ganz am Ende so würde ich sagen sollte das lauten

$this->addCreateOptions($selectId, $data);

Schleierhaft ist mir wie du ein objekt von der xajax classe erstellen kannst die ist ja nirgends includiert aber vileicht versteh ich da was falsch

und zu guter letzt sollte man nach Coding Guidelinessen Objekte so erstellen t3lib_div::makeInstance(Klassenname);

but im not sure at all

### [#6](https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen/6) · NickD · 2007-06-07

was ich noch gesehen habe, du erstellt in einigen funktion en jedes mal das gleiche objekt. das ist denke ich unnötig. mach dir einfach eine klassenvariable (direkt unter class tx_productfinder_pi1 extends tslib_pibase {) und dann kannst du auf diese variable von jeder funktion mit $this zugreifen (so wie du es auch mit ajax gemacht hast).

---

Quelle: „typo3 und xajax dropdown listen“, typo3.net, https://www.typo3.net/d/63030-typo3-und-xajax-dropdown-listen
Beiträge von Mitgliedern des Forums. Bitte mit Link auf die Diskussion zitieren.
