Hallo @,
ich habe hier ein PHP Script mit Ajax welches ich bei mir einbinden möchte. Dazu muss es aber für Smarty "zerlegt" werden.
Kann mir jemand dabei helfen, sprich mir Ansätze geben?
<?
if ( !defined( 'SMARTY_DIR' ) )
include_once( 'init.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['txt']."','".$option['val']."');");
}
}
}
}
$modesRs = mysql_query("SELECT DISTINCT(name) FROM countries") or die(mysql_error());
while ($rs=mysql_fetch_assoc($modesRs)) {
$modes[]=$rs["name"];
}
function addtxtstateprovince($selectId, $mode) {
global $txtstateprovince;
$objResponse = new myXajaxResponse();
$txtstateprovinceRs = mysql_query(sprintf(
"SELECT DISTINCT(name) FROM states WHERE countrycode = '%s'", $mode));
while ($rs=mysql_fetch_assoc($txtstateprovinceRs)) {
$txtstateprovince[]=array("txt"=>$rs["name"], "val"=>$rs["name"]);
}
$objResponse->addCreateOptions($selectId, $txtstateprovince);
return $objResponse;
}
function addtxtcity($selectId, $transport) {
global $txtcity;
$objResponse = new myXajaxResponse();
$txtcityRs = mysql_query(sprintf
("SELECT name FROM cities WHERE statecode = '%s'", $transport));
while ($rs=mysql_fetch_assoc($txtcityRs)) {
$txtcity[]=array("txt"=>$rs["name"], "val"=>$rs["name"]);
}
$objResponse->addCreateOptions($selectId, $txtcity);
return $objResponse;
}
$xajax->registerFunction("addtxtstateprovince");
$xajax->registerFunction("addtxtcity");
$xajax->processRequest();
// Testausgabe
if (isset($_POST['send'])) {
print_r($_POST);
}
?>
<html>
<?
$xajax->printJavascript("js/");
?>
<script type="text/javascript">
function addOption(selectId, txt, val) {
var objOption = new Option(txt, val);
document.getElementById(selectId).options.add(objOption);
}
</script>
</head>
<body>
<form name="frmPlan" method="post" action="">
Land :
<select name="txtfrom" id="txtfrom"
onchange="xajax_addtxtstateprovince('txtstateprovince', document.frmPlan.txtfrom.value)">
<option value="">--select--</option>
<? foreach ($modes as $mod) { ?>
<option value="<?= $mod?>"><?= $mod?></option>
<? } ?>
</select>
Bezirk :
<select name="txtstateprovince" id="txtstateprovince"
onchange="xajax_addtxtcity('txtcity', document.frmPlan.txtstateprovince.value)">
<option value="">--select--</option>
</select>
Stadt :
<select name="txtcity" id="txtcity"><option value="">--select--</option>
</select>
<input type="submit" value="Submit" name="send">
</form>
</body>
</html>
Speziell weiss ich nicht wie ich die foreach Schleife im Template darstellen muss und die Ausgabe "$xajax->printJavascript("js/");".
Bei den anderen beiden Dropdownmenues müsste doch normalerweise (ohne den Gebrauch von Ajax) auch eine Variable für die Werte stehen. Wie wird das mit Smarty und Ajax gelöst?
HeikoH