Transfert des données en session entre les pages

Petit nouveau ! | 4 Messages

07 févr. 2012, 14:57

Bonjour à tous,
J'essaye d'afficher les variables de session de mon formulaire précédent sur cette page tout en offrant la possibilité de modifier le cas échéant si c'est nécessaire. Il s'agit d'un formulaire adresse de facturation dont les données s'affichent puisqu'elles ont été rentrées dans une page précédente, identification générale. L'adresse de facturation peut être différente comme sur le site de la fnac par exemple. Mon problème est que je récupère les données du formulaire précédent mais je ne conserve pas celles modifiées... je ne sais pas comment faire, il y a quelque chose qui cloche...
Si vous avez cinq minutes... merci d'avance !

Voici mon code simplifié pour qu'il ne soit pas trop long :
<?php
session_start(); 
 
//valeurs champs premier formulaire = inscription1.php
if (isset($_POST['titre'])) {
    $_SESSION['form0']['titre'] = $_POST['titre'];}
if (isset($_POST['prenom'])) {
    $_SESSION['form0']['prenom'] = $_POST['prenom'];}
if (isset($_POST['nom'])) {
    $_SESSION['form0']['nom'] = $_POST['nom'];}
if (isset($_POST['email'])) {
    $_SESSION['form0']['email'] = $_POST['email'];}
 
//valeurs champs premier formulaire = inscription2.php	 	 
if (isset($_POST['labs'])) {
    $_SESSION['forma']['labs'] = $_POST['labs'];}
 

//On récupère ensuite les valeurs du formulaire de facturation au cas où elles auraient été modifié
$_SESSION['formf2'] = $_SESSION['forma'];

if (isset($_POST['labsf2'])) {
    $_SESSION['formf2']['labs'] = $_POST['labsf2'];}


// on initialise le tableau des erreurs
$erreurs= array(
   "labsf2" =>          array("msg" => ""),
);

$error_exist= false;
$erreur2= "";
 
$labsf2=        (isset($_POST['labsf2']))          ?($_POST['labsf2'])         :("");

if(isset($_POST['inscription'])) {
 

   if(empty($labsf2)  {
      $erreur2 = "All fields are requested !"; 
      $error_exist= true;  
   }
 
   // test sur laboratoire
   $labs_ko= (strlen($labsf2) < 2);
   if($labs_ko) {
       $erreurs["labsf2"]["msg"].="Fill the field labsf !<br/>";
       $error_exist= true;
   } 
   else { 
         $erreur2 = 'One of the fields is empty !'; 
      }   
     } 
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//FR" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

</head>
<body>
            <div id="contour_formulaire">
            <form name="formf2" action="inscriptionFacturation3.php" method="POST">
		<fieldset style="border-color:#73aad2;border-size:2px;" width="625">  
		<legend class="titre_labelaut"><span class="Style4"></span></legend>
        <p class="TitreFormulaire1">Billing adress  #3<br/>              
	      <span class="ClickHere">Does my billing adress different from this one ?</span> <br/>
		</p>

        <table width="740" border="0" cellpadding="0" cellspacing="0" >
          <tr>
            <td width="118"><label for="labsf2" class="TexteLogin">Dept/Labs : </label></td>
            <td width="282"><input name="labsf2" type="text" class="loginRemplissage" SIZE="45" value="<?php if (!empty($_SESSION['formf2']['labs'])) echo htmlentities(trim($_SESSION['formf2']['labs']));
			 ?>"/>
            <span class="TexteCheck">*</span></td>
            <td width="340" colspan="3" class="error2">&#160;<?php echo $erreurs["labsf2"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="118">&nbsp;</td>
            <td width="282">&nbsp;</td>
            <td colspan="3" class="error2">&nbsp;</td>
          </tr>
          <tr>          </tr>
          <tr>
            <td>&nbsp;</td>
            <td colspan="7" class="TexteErreur"><?php if (isset($erreur2)) echo '<br />',$erreur2;  ?></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
			<td colspan="7" class="texteCheckBoxT" valign="top"><strong><a href="inscriptionFacturation4.php" class="TexteFormulaireBleuDessous">>> </a></strong>&nbsp;&nbsp;I have other accounts from wich I can buy. </td>
          </tr>
          
		  <tr>
            <td>&nbsp;</td>
            <td colspan="7" class="TexteCheck">* Must be mandatory filled </td>
          </tr>
        </table>
		</fieldset>     
              </form><br/>
		   </div>
		   	   
        
</body>
</html>


Eléphant du PHP | 250 Messages

07 févr. 2012, 15:30

Bonjour
Sans avoir lu complètement ton code, ne serait il pas plus simple de faire un :
session_destroy();
session_start();
puis de récupérer ton form2?
"L'histoire, à l'image de toutes choses dans l'univers, semble avoir comme unique règle le côté ondulatoire."

Petit nouveau ! | 4 Messages

07 févr. 2012, 15:42

En fait, j'ai besoin de conserver les données remplies dans le formulaire précédent puisqu'elles doivent s'afficher par défaut dans ma page adresse livraison. En fait, j'ai une adresse commerciale et une adresse livraison, la deuxième est la première par défaut mais je dois permettre d'en changer certaines valeurs si elle diffère de l'adresse commerciale. En fait, j'ai un formulaire avec plusieurs pages à remplir, le tout dans la même session active le temps du remplissage.
Merci

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

07 févr. 2012, 16:50

salut,

ton code est assez complexe à suivre.

pourquoi n'essai tu pas bien ranger les infos dans ta sessions (parce que bon 'form0' ce n'est pas parlant).

donc
- $_SESSION['civilite']
- $_SESSION['adressefacturation']
- $_SESSION['adresselivraison']

Au départ tu peux très bien faire $_SESSION['adressefacturation'] = $_SESSION['adresselivraison'] = $_POST (bon c'est grossier mais le principe est la).
ou laisser $_SESSION['adresselivraison'] vide c'est au choix (par contre faut que l'index existe

après dans le second formulaire tu remplis le formulaire en fonction de $_SESSION['adressefacturation'], à la validation tu remplis $_SESSION['adresselivraison'] en fonction du formulaire.

pour faire bien une petite case à cocher ou un bouton radio "utiliser la même adresse pour livraison / facturation".

au traitement final si $_SESSION['adresselivraison'] n'exiset pas ou est vide tu utilise $_SESSION['adressefacturation'] sinon les deux.

@+
Il en faut peu pour être heureux ......

Petit nouveau ! | 4 Messages

07 févr. 2012, 17:50

merci pour ta réponse mais je ne pense pas avoir tout compris...
J'ai modifié mes intitulés de session pour simplifier la compréhension :
<?php
session_start(); // En premier dans la page 
 
 
//valeurs champs premier formulaire = inscription2.php	 
if (isset($_POST['labs'])) {
    $_SESSION['civilite2']['labs'] = $_POST['labs'];}
if (isset($_POST['company'])) {
    $_SESSION['civilite2']['company'] = $_POST['company'];}
if (isset($_POST['adress'])) {
    $_SESSION['civilite2']['adress'] = $_POST['adress'];}
if (isset($_POST['bat'])) {
    $_SESSION['civilite2']['bat'] = $_POST['bat'];}
if (isset($_POST['code_postal'])) {
    $_SESSION['civilite2']['code_postal'] = $_POST['code_postal'];}
if (isset($_POST['ville'])) {
    $_SESSION['civilite2']['ville'] = $_POST['ville'];}
if (isset($_POST['pays'])) {
    $_SESSION['civilite2']['pays'] = $_POST['pays'];}
if (isset($_POST['tel'])) {
    $_SESSION['civilite2']['tel'] = $_POST['tel'];}
if (isset($_POST['fax'])) {
    $_SESSION['civilite2']['fax'] = $_POST['fax'];}
 

//On récupère ensuite les valeurs du formulaire de facturation au cas où elles auraient été modifié
$_SESSION['facturation'] = $_SESSION['civilite2'];

if (isset($_POST['labs_facturation'])) {
    $_SESSION['facturation']['labs'] = $_POST['labs_facturation'];}
if (isset($_POST['company_facturation'])) {
    $_SESSION['facturation']['company'] = $_POST['company_facturation'];}
if (isset($_POST['adress_facturation'])) {
    $_SESSION['facturation']['adress'] = $_POST['adress_facturation'];}
if (isset($_POST['bat_facturation'])) {
    $_SESSION['facturation']['bat'] = $_POST['bat_facturation'];}
if (isset($_POST['code_postal_facturation'])) {
    $_SESSION['facturation']['code_postal'] = $_POST['code_postal_facturation'];}
if (isset($_POST['ville_facturation'])) {
    $_SESSION['facturation']['ville'] = $_POST['ville_facturation'];}
if (isset($_POST['pays_facturation'])) {
    $_SESSION['facturation']['pays'] = $_POST['pays_facturation'];}
if (isset($_POST['tel_facturation'])) {
    $_SESSION['facturation']['tel'] = $_POST['tel_facturation'];}
if (isset($_POST['fax_facturation'])) {
    $_SESSION['facturation']['fax'] = $_POST['fax_facturation'];}


// on initialise le tableau des erreurs
$erreurs= array(
   "labs_facturation" =>          array("msg" => ""),
   "company_facturation" =>       array("msg" => ""),
   "adress_facturation" =>        array("msg" => ""),
   "bat_facturation" =>           array("msg" => ""),
   "code_postal_facturation" =>   array("msg" => ""),
   "ville_facturation" =>         array("msg" => ""),
   "pays_facturation" =>          array("msg" => ""),
   "tel_facturation" =>           array("msg" => ""),
   "fax_facturation" =>           array("msg" => ""),
);



$error_exist= false;
$erreur2= "";
 
$labs_facturation=  (isset($_POST['labs_facturation'])) ?($_POST['labs_facturation']):("");
$company_facturation= (isset($_POST['company_facturation']))?($_POST['company_facturation']):("");
$adress_facturation= (isset($_POST['adress_facturation']))?($_POST['adress_facturation']):("");
$bat_facturation=  (isset($_POST['bat_facturation']))?($_POST['bat_facturation']):("");
$code_postal_facturation= (isset($_POST['code_postal_facturation']))?($_POST['code_postal_facturation']):("");
$ville_facturation= (isset($_POST['ville_facturation']))?($_POST['ville_facturation']):("");
$pays_facturation= (isset($_POST['pays_facturation']))?($_POST['pays_facturation']) :("");
$tel_facturation= (isset($_POST['tel_facturation']))  ?($_POST['tel_facturation']):("");
$fax_facturation=  (isset($_POST['fax_facturation'])) ?($_POST['fax_facturation']) :("");


 
 // on teste si le visiteur a soumis le formulaire
if(isset($_POST['inscription'])) {
 
 //si tous les champs sont vides(verif avec empty), erreur2 = tous les champs sont requis
   if(empty($labs_facturation) || empty($company_facturation) || empty($adress_facturation) || empty($bat_facturation) || empty($code_postal_facturation) || empty($ville_facturation) || empty($pays_facturation) || empty($tel_facturation) || empty($fax_facturation)) {
      $erreur2 = "All fields are requested !"; 
      $error_exist= true;  
   }
 
   // test sur laboratoire
   $labs_ko= (strlen($labs_facturation) < 2);
   if($labs_ko) {
       $erreurs["labs_facturation"]["msg"].="Fill the field labsf !<br/>";
       $error_exist= true;
   } 
 
   // test sur company
   $company_ko= (strlen($company_facturation) < 2);
   if($company_ko) {
       $erreurs["company_facturation"]["msg"].="Fill the field company !<br/>";
       $error_exist= true;
   } 
 
   // test sur adress
   $adress_ko= (strlen($adress_facturation) < 2);
   if($adress_ko) {
       $erreurs["adress_facturation"]["msg"].="Fill the field adress !<br/>";
       $error_exist= true;
   } 
 
   // test sur bat
   $bat_ko= (strlen($bat_facturation) < 2);
   if($bat_ko) {
       $erreurs["bat_facturation"]["msg"].="Fill the field building !<br/>";
       $error_exist= true;
   } 
   
   // test sur code postal
   $code_postal_ko= (strlen($code_postal_facturation) < 2);
   if($code_postal_ko) {
       $erreurs["code_postal_facturation"]["msg"].="Fill the field postal code !<br/>";
       $error_exist= true;
   } 
   
   // test sur city
   $ville_ko= (strlen($ville_facturation) < 2);
   if($ville_ko) {
       $erreurs["ville_facturation"]["msg"].="Fill the field city !<br/>";
       $error_exist= true;
   } 
   
   // test sur tel
   if(!is_numeric($tel_facturation)) {
      $erreurs["tel_facturation"]["msg"].= "This fiels must contain only figures !<br/>";
   }
   
   // test sur fax
   if(!is_numeric($fax_facturation)) {
      $erreurs["fax_facturation"]["msg"].= "This fiels must contain only figures !<br/>";
   }
 
   if(!$error_exist) {
 
	 	   // et on commence la session qui s'appelle email
         $_SESSION['email'] = $email; 
         header('Location: inscriptionLivraison.php'); 
         exit(); 
   }
   
   else { 
         $erreur2 = 'One of the fields is empty !'; 
      }   
     } 

   // Rajouter condition si case cochée "un autre compte pour acheter"

?>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//FR" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
   
</head>
<body>

     
            <div id="contour_formulaire">
                                           
            <form name="facturation" action="inscriptionFacturation.php" method="POST">
		<fieldset style="border-color:#73aad2;border-size:2px;" width="625">  
		<legend class="titre_labelaut"> Four steps and few clicks...<span class="Style4"></span></legend>


        <p class="TitreFormulaire1">Billing adress  #1<br/>              
	      <span class="ClickHere">Does my billing adress different from this one ?</span> <br/>              
		</p>

        <table width="740" border="0" cellpadding="0" cellspacing="0" >
          <tr>
            <td width="117"><label for="labs_facturation" class="TexteLogin">Dept/Labs : </label></td>
            <td width="320"><input name="labs_facturation" type="text" class="loginRemplissage" SIZE="45" value="<?php if (!empty($_SESSION['facturation']['labs'])) echo htmlentities(trim($_SESSION['facturation']['labs']));
			 ?>"/>
            <span class="TexteCheck">*</span></td>
            <td width="295" colspan="3" class="error2">&#160;<?php echo $erreurs["labs_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="117"><label for="company_facturation" class="TexteLogin">Company : </label></td>
            <td width="320"><input name="company_facturation" type="text" class="loginRemplissage" SIZE="45" value="<?php if (!empty($_SESSION['facturation']['company'])) echo htmlentities(trim($_SESSION['facturation']['company'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["company_facturation"]["msg"]; ?></td>
          </tr>
          <tr>
            <td width="117"><label for="adress_facturation" class="TexteLogin">Adress  : </label></td>
            <td><input name="adress_facturation" type="text" class="loginRemplissage" SIZE="45" value="<?php if (!empty($_SESSION['facturation']['adress'])) echo htmlentities(trim($_SESSION['facturation']['adress'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["adress_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="117"><label for="bat_facturation" class="TexteLogin">Building  : </label></td>
            <td width="320"><input name="bat_facturation" type="text" class="loginRemplissage" SIZE="45" value="<?php if (!empty($_SESSION['facturation']['bat'])) echo htmlentities(trim($_SESSION['facturation']['bat'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["bat_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="117"><label for="code_postal_facturation" class="TexteLogin">Zip code  : </label></td>
            <td width="320"><input name="code_postal_facturation" type="text" class="loginRemplissage" SIZE="20" value="<?php if (!empty($_SESSION['facturation']['code_postal'])) echo htmlentities(trim($_SESSION['facturation']['code_postal'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["code_postal_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="117"><label for="ville_facturation" class="TexteLogin">City  : </label></td>
            <td width="320"><input name="ville_facturation" type="text" class="loginRemplissage" SIZE="45" value="<?php if (!empty($_SESSION['facturation']['ville'])) echo htmlentities(trim($_SESSION['facturation']['ville'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["ville_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="117"><label for="pays_facturation" class="TexteLogin">Country  : </label></td>
            <td width="320">
			<select class="loginRemplissage" style="width:130px;" name="pays_facturation" id="pays_facturation" size="1">

 <option  value="Algeria"<?php if($_SESSION['facturation']['pays_facturation'] == 'Algeria') echo "selected"; ?>>Algeria</option>
<option value="Argentina"<?php if($_SESSION['facturation']['pays_facturation'] == 'Argentina') echo "selected" ; ?>>Argentina</option>
<option value="USA"<?php if($_SESSION['facturation']['pays_facturation'] == 'USA') echo "selected" ;?>>USA</option>
</select>

            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2"> </td>
          </tr>
          <tr>
            <td width="117"><label for="tel_facturation" class="TexteLogin">Tel  : </label></td>
            <td width="320"><input name="tel_facturation" type="text" class="loginRemplissage" SIZE="30" value="<?php if (!empty($_SESSION['facturation']['tel'])) echo htmlentities(trim($_SESSION['facturation']['tel'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["tel_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td width="117"><label for="fax_facturation" class="TexteLogin">Fax : </label></td>
            <td width="320"><input name="fax_facturation" type="text" class="loginRemplissage" SIZE="30" value="<?php if (!empty($_SESSION['facturation']['fax'])) echo htmlentities(trim($_SESSION['facturation']['fax'])); ?>"/>
            <span class="TexteCheck">*</span></td>
            <td colspan="3" class="error2">&#160;<?php echo $erreurs["fax_facturation"]["msg"]; ?> </td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td colspan="7" class="TexteErreur"><?php if (isset($erreur2)) echo '<br />',$erreur2;  ?></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td colspan="7" class="TexteErreur">&nbsp;</td>
          </tr>
          
          <tr>
            <td>&nbsp;</td>
			
			
			<td colspan="7" class="texteCheckBoxT" valign="top"><strong><a href="inscriptionFacturation2.php" class="TexteFormulaireBleuDessous">>> </a></strong>&nbsp;&nbsp;I have other accounts from wich I can buy. </td>
          </tr>
          
		  <tr>
		    <td>&nbsp;</td>
		    <td colspan="7" class="TexteCheck">&nbsp;</td>
	      </tr>
		  <tr>
            <td>&nbsp;</td>
            <td colspan="7" class="TexteCheck">* Must be mandatory filled </td>
          </tr>
		  <tr>
		    <td>&nbsp;</td>
		    <td colspan="7" class="TexteCheck">&nbsp;</td>
	      </tr>
		  <tr>
		    <td colspan="8"><table width="737" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="82" class="loginRemplissage2"><div align="center"><strong><img src="images/Fleche.jpg" alt="" width="16" height="16" align="left" class="AlignementFleche5"/><a href="inscription1.php" class="loginRemplissage2Copie">Account</a></strong></div></td>	
                <td width="110" class="loginRemplissage2"><div align="center"><strong><img src="images/Fleche.jpg" alt="" width="16" height="16" align="left" class="AlignementFleche5"/><a href="InterfineChemicals.php" class="loginRemplissage2Copie">Field of interest </a></strong> </div></td>
                <td width="130" class="loginRemplissage2"><div align="center"><strong><img src="images/Fleche.jpg" alt="" width="16" height="16" align="left" class="AlignementFleche5"/><a href="InterfineChemicals.php" class="loginRemplissage2Copie">First Billing adress </a></strong> </div></td>
                <td width="133">&nbsp;</td>
                <td width="28">&nbsp;</td>
                <td width="77">&nbsp;</td>
                <td width="97">&nbsp;</td>
                <td width="80"><input type="submit" name="inscription" class="AlignementNext" value=""/></td>
              </tr>
            </table></td>
	      </tr>
		  <tr>
		    <td>&nbsp;</td>
		    <td colspan="7" class="TexteCheck">&nbsp;</td>
	      </tr>
		  
		  
		  </td>
	      </tr>
        </table>
		</fieldset>     
              </form><br/>
		   </div>
		   	   
        
</body>
</html>