[AJAX] Formulaire de commande AJAX
Je souhaite réaliser un formulaire dynamique de type ajax. J'avais réussi a faire fonctionné l'ensemble par rapport à une prestation (tasseau d'après la variable "pack") et lorsque j'essaye dans mettre une seconde dans un select.
On me retourne Nan dans le tarif TTC au lieu de la valeur calculée... Je vous serez très reconnaissant si vous pouviez me venir en aide!
Voici le code php qui reçoit les requetes javascript:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| <?php
if (!isset($_POST['load']))
{
return null;
}
//********** header et systeme anti-cache
header("Content-Type: text/plain ; charset=utf-8");
header("Cache-Control: no-cache , private");
header("Pragma: no-cache");
//********** chargement du fichier xml
$fichierXml = file_get_contents('commandes.xml');
$objetSimpleXML=simplexml_load_string($fichierXml);
$load = $_POST['load'];
{
if($load=='pack')
{
if(!(isset($_POST['pack'])&&isset($_POST['surface'])))
{
return null;
}
$pack_id = $_POST['pack'];
}
//********** remplissage liste deroulante packs disponibles
if($load=='load_packs')
{
echo '<select name="pack" id="pack">';
echo '<option value=0>A definir</option>';
echo '<option selected value=1>';
echo 'Aucun tasseau';
echo "</option>";
echo '<option value=2>';
echo 'Tasseau brut';
echo "</option>";
echo '<option value=3>';
echo 'Tasseau teinte';
echo "</option>";
echo '</select>';
return;
}
if($load=='load_inscri')
{
echo '<select name="inscri" id="inscri">';
echo '<option value=0>A definir</option>';
echo '<option selected value=1>';
echo 'Aucune inscription';
echo "</option>";
echo '<option value=2>';
echo 'Insciption doree';
echo "</option>";
echo '<option value=3>';
echo 'Inscription caligraphie';
echo "</option>";
echo '</select>';
return;
}
//************ preparation et calculs remplissage formulaire
if($load=='pack'||$load=='inscri')
{
$surface = $_POST['surface'];
$compte2=$_COOKIE["compte1"];
$inscri_id = $_POST['inscri'];
$pack_id = $_POST['pack'];
if(!mysql_connect('localhost','root'))
{echo "Connection plantée";
exit(); }
else
{echo " ";}
mysql_select_db('encadrement');
$requete1 = "select * from commercant where nom_entreprise = '$compte2'" ;
$resultat1 = mysql_query($requete1);
$tasseau = mysql_fetch_array($resultat1);
if ("$pack_id"==1)
{
$tarif = 0;
}
if ("$pack_id"==2)
{
$tarif = $tasseau['PRI_TASSEAUBRUTE'];
}
if ("$pack_id "==3)
{
$tarif = $tasseau['PRI_TASSEAUTEINTE'];
}
if ("$inscri_id"==1)
{
$pinscri = 0;
}
if ("$inscri_id"==2)
{
$pinscri = $tasseau['PRI_INSCRI_NOIR'];
}
if ("$inscri_id "==3)
{
$pinscri = $tasseau['PRI_CALLIG'];
}
echo $tarif .":". $pinscri;
}
return;
}
?> |
ET voici le javascript!
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| window.onload=function()
{
document.getElementById('noscript').style.backgroundColor = 'transparent'; // bug IE affichage boite no-script
// declaration des évènements
load_pack();
load_inscri()
document.getElementById("cgv_accept").onclick = valid_Form;
document.getElementById("surface").onchange = verif_surface;
document.getElementById("pack").onchange = choix_pack;
document.getElementById("form_com").onsubmit = formValid;
document.getElementById("inscri").onchange = choix_inscri;
}
function valid_Form() {
radio = document.getElementById("cgv_accept");
if(radio.checked)
{
document.getElementById("form_valid").disabled = false;
}
else {
document.getElementById("form_valid").disabled = true;
}
}
function verif_surface() {
surface = Math.round(document.getElementById("surface").value);
document.getElementById("surface").value = surface;
if(!(surface>15&&surface<10000))
{
alert('Le perimetre doit etre compris entre 15 et 10000 cm²');
document.getElementById("surface").value = 0;
document.getElementById("pack").selectedIndex = 0;
}
if (document.getElementById("pack").selectedIndex != 0)
calcul();
}
function choix_pack() {
verif_surface();
}
function choix_inscri() {
verif_surface();
}
function load_pack() {
//config et envoi d'une requete synchrone moteur ajax
objetXHR = creationXHR();
if(objetXHR==null)
{
alert('Votre navigateur ne prends pas en charge les objets ajax.\nVous ne pourrez pas utiliser ce formulaire.');
return;
}
objetXHR.open("post","calcul.php",false);
objetXHR.setRequestHeader("content-type","application/x-www-form-urlencoded");
objetXHR.send("load=load_packs");
//retour serveur
var liste_pack = objetXHR.responseText;
document.getElementById("select_pack").innerHTML = liste_pack;
}
function load_inscri() {
//config et envoi d'une requete synchrone moteur ajax
objetXHR = creationXHR();
if(objetXHR==null)
{
alert('Votre navigateur ne prends pas en charge les objets ajax.\nVous ne pourrez pas utiliser ce formulaire.');
return;
}
objetXHR.open("post","calcul.php",false);
objetXHR.setRequestHeader("content-type","application/x-www-form-urlencoded");
objetXHR.send("load=load_inscri");
//retour serveur
var liste_inscri = objetXHR.responseText;
document.getElementById("select_inscri").innerHTML = liste_inscri;
}
function calcul() {
//config et envoi d'une requete synchrone moteur ajax
objetXHR = creationXHR();
if(objetXHR==null)
{
alert('Votre navigateur ne prends pas en charge les objets ajax.\nVous ne pourrez pas utiliser ce formulaire.');
return;
}
objetXHR.open("post","calcul.php",true);
objetXHR.onreadystatechange = retour_calcul;
objetXHR.setRequestHeader("content-type","application/x-www-form-urlencoded");
var parametres = "load=pack&pack="+document.getElementById("pack").selectedIndex+"&surface="+document.getElementById("surface").value;
var parametres = "load=inscri&inscri="+document.getElementById("inscri").selectedIndex+"&surface="+document.getElementById("surface").value;
objetXHR.send(parametres);
objetXHR.send(parametres1);
}
function retour_calcul() {
if (objetXHR.readyState == 4)
{
if (objetXHR.status == 200)
{
// recuperation des données du moteur ajax
var $resultat = objetXHR.responseText.split(":");
$tarif = parseFloat($resultat[0]);
$pinscri = parseFloat($resultat[1]);
$surface = document.getElementById("surface").value;
// affectation des elements sans calculs dans la page
document.getElementById("tarif_ht_m").innerHTML = $tarif;
document.getElementById("tarif_ht_m_hid").value = $tarif;
// on determine le type de pack
pack = document.getElementById("pack").value;
inscri = document.getElementById("inscri").value;
$tarif_ht = ($surface/100)*($tarif+$pinscri);
$tarif_ttc = $tarif_ht*1900.6/100+$tarif_ht;
$tva = $tarif_ht*1900.6/100;
// arrondissement des elements à 2 decimales
$tarif_ht = Math.round($tarif_ht*100)/100;
$tva = Math.round($tva*100)/100;
$tarif_ttc = Math.round($tarif_ttc*100)/100;
// affectation des elements dans la page
document.getElementById("tarif_ht").innerHTML = $tarif_ht; document.getElementById("tarif_ht_hid").value = $tarif_ht;
document.getElementById("tva").innerHTML = $tva; document.getElementById("tva_hid").value = $tva;
document.getElementById("tarif_ttc").innerHTML = $tarif_ttc; document.getElementById("tarif_ttc_hid").value = $tarif_ttc;
}
else
{
alert('erreur lors de la reception des données du serveur');
}
}
}
// fonction de vérification du formulaire, renvoie false si invalide
function formValid() {
if(document.getElementById("societe").value == '')
{
alert('Vous devez indiquer le nom de la societé');
return false;
}
else if(document.getElementById("dirigeant").value == '')
{
alert('Vous devez indiquer le nom du dirigeant ou donneur d\'ordre');
return false;
}
else if(document.getElementById("cp").value == '')
{
alert('Vous devez indiquer le code postal');
return false;
}
else if(document.getElementById("surface").value == '')
{
alert('Vous devez indiquer la surface');
return false;
}
else if(document.getElementById("pack").selectedIndex == 0)
{
alert('Vous devez choisir un pack/kit');
return false;
}
else
{
return true;
}
}
/********************** moteur ajax ***********************/
function creationXHR() {
var resultat = null;
try
{ //test pour les navigateurs firefox ou opera
resultat = new XMLHttpRequest();
}
catch(Error)
{
try
{ //test pour IE>5.0
resultat = new ActiveXObject("Msxml2.XMLHTTP");
alert('>5');
}
catch(Error)
{
try
{
//test pour IE<5.0
resultat = new ActiveXObject("Microsoft.XMLHTTP");alert('<5.0');
}
catch(Error)
{
resultat = null;
}
}
}
return resultat;
} |
Cordialement,
GOYER David