Page 1 sur 1

formulaire de commande avec boucle dans tableaux

Posté : 10 avr. 2010, 09:08
par tharan
Alors bonjour a tous , je préviens si vous avez des méthodes plus appropriés par rapport a à ce que je propose je suis tout ouie

Bonjour

Je suis entrain de faire un formulaire de commande qui partira par mail

En clair :



Nom---------------Prix------------- Quantité--------------------------- Sous total
Vin rouge-------- 5€ ----------(défini par l utilisateur)------------- automatique
Vin blanc 3€ ----------(défini par l utilisateur)------------- automatique

total
auto
----------------------------------- Bouton de validation
il valide et sa affiche sa commande
ensuite une fois le recapitulatif en visu il a un 2eme bouton qui cette fois va valider et envoyer les données par mail

Pour mieux suivre, je post le code vous remarquerez que j ai fait le test de quantité différent de 0 que pour vin1 et j aimerais généraliser ce test à tout les vins je suppose que je dois créer 2 tableaux avec un qui stock le nom des produits et l autre qui stock la quantité saisi

avec un truc du genre
$produit = array ('rouge', 'rosé', 'blanc,');
mais comment faire pour l'interroger et faire la liaison entre la sélection que va faire l utilisateur et ça quantité

je met le lien ou vous pourrez visualiser ce que j ai fait http://robertweber.fr/form.html
mercid avance





------------------------------------------------------------------------------------------
formulaire
----------------------------------------------------------------------------------------

Code HTML : <?xml version="1.0"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Achat de Cartons de Vin</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<script type="text/javascript"> 
// <!-- <[CDATA[ 

function calculerTotal(form, span) { 
var cartons_vin1; 
var cartons_vin2; 
var cartons_vin3; 
var total; 
cartons_vin1 = form.cartons_vin1.value * 2; 
cartons_vin2 = form.cartons_vin2.value * 3 ; 
cartons_vin3 = form.cartons_vin3.value * 4.35; 
total = Number(cartons_vin1) + Number(cartons_vin2) + Number(cartons_vin3); 
document.getElementById(span).innerHTML = '= ' + total.toFixed(2) + '€'; 
} 
function calcul(champs, span, facteur) { 
var prix; 
prix = champs * facteur; 
prix = prix.toFixed(2) + '€'; 
document.getElementById(span).innerHTML = prix; 
}



// ]]> --> 
</script> 
</head> 
<body> 
<div id="contenu_principal"> 
<hr /> 
<form method="post" action="traitement.php" id="cartons_vins"> 
<p><label for="nom">Votre Nom : <input type="text" name="nom" id="nom" value="" /></label></p> 
<p><label for="prenom">Votre Prénom : <input type="text" name="prenom" id="prenom" value="" /></label></p> 
<p><label for="email">Votre email : <input type="text" name="email" id="email" value="" /></label></p> 
<p><label for="adresse">Votre adresse : <textarea name="adresse" id="adresse"></textarea></label></p> 
<p><u>Muesli</u></p> 
<p><label for="cartons_vin1">Quantité : 
<input type="text" name="cartons_vin1" id="cartons_vin1" size="2" maxlength="2" onBlur="calcul(this.value, 'affichage_prix-vin1',2);calculerTotal(this.form, 'affichage_total-vins')" value="0" /></label> x 2€ <span id="affichage_prix-vin1"> = €</span></p> 
<p><u>Pralines concasées</u></p> 
<p><label for="cartons_vin2">Quantité : 
<input type="text" name="cartons_vin2" id="cartons_vin2" size="2" maxlength="2" onBlur="calcul(this.value, 'affichage_prix-vin2', 3);calculerTotal(this.form, 'affichage_total-vins')" value="0" /></label> x 3€ <span id="affichage_prix-vin2"> = €</span></p> 
<p><u>pépites de chocolat</u></p> 
<p><label for="cartons_vin3">Quantité : 
<input type="text" name="cartons_vin3" id="cartons_vin3" size="2" maxlength="2" onBlur="calcul(this.value, 'affichage_prix-vin3', 4.35);calculerTotal(this.form, 'affichage_total-vins')" value="0" /></label> x 4,35€ <span id="affichage_prix-vin3"> = €</span></p> 
<p><strong>Total : </strong> 
<span id="affichage_total-vins"> </span></p> 
<p><input type="submit" value="Récapitulatif avant commande" /><input type="reset" value="Annuler" /></p> 
</form> 
</div> 
</body> 
</html>
---------------------------------
traitement du formulaire php
-----------------------------------



Code PHP :Code : PHP
<?php 
// Formulaire envoye ? Champs Vides ? 
foreach($_POST as $champs => $valeur) { 
if($champs != 'cartons_vin1' AND $champs != 'cartons_vin2' AND $champs != 'cartons_vin3') { 
if(!isset($valeur) OR empty($valeur)) { 
echo '<script type="text/javascript">// <![CDATA[',"\n",'alert("Veuillez remplir tous les champs.");',"\n",'window.location="form.html";',"\n",'// ]]>',"\n",'</script>'; 
} 
} 
} 
// Declaration des variables 
$nom = trim(strip_tags($_POST['nom'])); 
$prenom = trim(strip_tags($_POST['prenom'])); 
$email = trim(strip_tags($_POST['email'])); 
$Adresse = trim(strip_tags($_POST['Adresse'])); 
$k[0] = trim(strip_tags($_POST['cartons_vin1'])); 
$NbreCartons['vin2'] = trim(strip_tags($_POST['cartons_vin2'])); 
$NbreCartons['vin3'] = trim(strip_tags($_POST['cartons_vin3'])); 
$SousTotal['vin1'] = $NbreCartons['vin1'] * 2; 
$SousTotal['vin2'] = $NbreCartons['vin2'] * 3; 
$SousTotal['vin3'] = $NbreCartons['vin3'] * 4.35; 
$Total = $SousTotal['vin1'] + $SousTotal['vin2'] + $SousTotal['vin3']; 





?> 

<?php echo '<','?xml version="1.0" encoding="ISO-8859-1" ?','>',"\n"; ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr-FR" dir="ltr"> 
<head xml:lang="fr-FR" dir="ltr"> 
<title> 
Achat de Cartons de Vin - R&eacute;capitulatif de votre Commande 
</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
</head> 

<body> 
<div id="contenu_principal"> 
<h3>Récapitulatif de vos Informations :</h3> 
<p>Votre Nom : <strong><?php echo $nom; ?></strong></p> 
<p>Votre Prénom : <strong><?php echo $prenom; ?></strong></p> 
<p>Votre Adresse Email : <strong><?php echo $email; ?></strong></p> 
<p>Votre Adresse Postale : <strong><?php echo $adresse; ?></strong></p> 
<h3>Récapitulatif de votre Commande</h3> 

<?php


if ($NbreCartons['vin1'] > 0){
   echo ($NbreCartons['vin1'] == 1) ? '<strong>'. $NbreCartons['vin1'] .'</strong> unité ' : $NbreCartons['vin1'] .' unités';
   echo ' de vin, soit un total de <strong>'. $SousTotal['vin1'] .'</strong> €.';
}

?>

<p style="font-weight: bold; color: #c00000;">Le total de votre commande s'élève donc à <strong><?php echo $Total; ?></strong> €.</p> 
<p><a href="form.html">Modifier vos Informations</a> | <a href="javascript:print()">Imprimer 

</div>


<div id="commande"> 
<form action="commande.php" method="post" name="contact" id="contact">
<p><input type="submit" value="Commandez" />
 </div>
</body>

Re: formulaire de commande avec boucle dans tableaux

Posté : 11 avr. 2010, 19:27
par Aureusms
Le plus simple est que tu fasses le traitement de la commande dans la page PHP. Ainsi tu peux vérifier si tous champs sont bien remplis et correctement remplis (genre est ce que l'email ressemble à un email, est ce que un nombre est un nombre,...). Ensuite si il y a un problème de donnée, utilise la fonction header pour revenir vers le formulaire. Pour le traitement Javascript, je suis Ok mais ton visiteur désactive Javascript te voilà coincé.