I need my project done in a form that includes 3 related lists them.
My first I select to choose a province (until hopefully).
My second I select should display all the cities of the province selected (it starts to misfire)Then I select the 3rd displays all districts of the city selected.
So I create my form: Form_ajouter_poste.php
- Code: Select all
<?php
Class FormAjouterPoste extends Zend_Form {
/**
* Fonction _initForm() : Initialise le formulaire
*
*/
public function _initForm()
//Nom du formulaire
$this->setName('Formulaire_Ajouter_Poste');
// Liste des provinces
$province = new Zend_Form_Element_Select('province');
$province->setLabel ('Province : ')
->addMultiOptions($this->ListeProvince())
->setAttrib('id','province')
->setOptions(array('onChange' => 'selectVille();'))
;
// Liste des villes
$ville = new Zend_Form_Element_Select('ville');
$ville->setLabel ('Ville : ')
->setAttrib('id','ville')
->addMultiOptions($this->SelectVille())
;
// Liste des communes
$commune = new Zend_Form_Element_Select('commune');
$commune->setLabel ('Commune : ')
->setAttrib('id','commune')
->addMultiOptions($this->ListeCommune())
;
// le bouton envoi
$submit = new Zend_Form_Element_Submit('Ajouter');
$submit ->setLabel('Ajouter')
->setAttrib('class','bouton')
->setDecorators(array('ViewHelper',array(array('td' => 'HtmlTag'), array('tag' => 'td', 'colspan' => 2)),array(array('tr' => 'HtmlTag'), array('tag' => 'tr'))));
//On ajoute les élèments au formulaire
$this->addElements(array(
$province,
$ville,
$commune,
$submit
));
}
/**
* Fonction SelectVille() : Affiche les villes en fonction de la province sélectionnée
*
*/
public function SelectVille()
{
//Initialisation des models
$BureauVilleModel = new BureauVilleModel(); // Model Ville
//On récupere l'idprovince envoyer en ajax
if(isset($_REQUEST["idprovince"])){
$id = $_REQUEST["idprovince"];
$Villes = $BureauVilleModel->getVilleProvince( $id );
}else{
$Villes = $BureauVilleModel->selectData();
}
//On remplit la liste
$ListeVille = array();
$ListeVille[""] = "-- Choisissez --";
foreach ($Villes as $Ville) {
$ListeVille[$Ville->ville_id] = $Ville->ville_nom;
}
return $ListeVille;
}
}
In my controller I displays the following javascript (call when I choose a province)
- Code: Select all
<script language="Javascript">
var xhr = null;
function getXhr(){
if(window.XMLHttpRequest) // Firefox et autres
xhr = new XMLHttpRequest();
else if(window.ActiveXObject){ // Internet Explorer
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
else { // XMLHttpRequest non supporté par le navigateur
alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
xhr = false;
}
}
/**
* Méthode qui sera appelée sur le click du bouton
*/
function selectVille(){
getXhr();
// On défini ce qu'on va faire quand on aura la réponse
xhr.onreadystatechange = function(){
// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
if(xhr.readyState == 4 && xhr.status == 200){
leselect = xhr.responseText;
// On se sert de innerHTML pour rajouter les options a la liste
document.getElementById('ville').innerHTML = leselect;
}else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
// Ici on va voir comment faire du post
xhr.open("POST","../forms/bureau/Form_ajouter_poste.php",true);
// ne pas oublier ça pour le post
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
// ne pas oublier de poster les arguments
sel = document.getElementById('province');
idprovince = sel.options[sel.selectedIndex].value;
xhr.send("ville_id_province="+idprovince);
alert(idprovince);
}
</script>
In my form, when I select a province, city empties my field and I did not cut that appears.
Someone an idea? Thank you

