problème avec touche F5

Eléphant du PHP | 187 Messages

25 sept. 2011, 03:30

bonjour,
là j'ai un grand souci,pourquoi quand je clique sur F5 le code(matricule) que j'ai met dans ma page(qui s'incrémente à chaque enregistrement) il s'incrément sans cliquer sur l le button enregistrer et en plus la valeur passe au base de donnée??!!
c'est un grand problème vraiment car j'ai trouvé ma base toute pleine d'enregistrements fait par mes cliques de f5 sans faire attention !!
je vous montre le code ou y a t-il une solution pour régler ça?? et merci d'avance

Eléphant du PHP | 187 Messages

25 sept. 2011, 03:53

et quand je clique sur F5 voilà le msg donnée par le navigateur :
"
la page que vous tenter de voir contient des données envoyés par POST.si vous renvoyer les données ,toute action entreprise par la page web(telle que une recherche ou achat en ligne)sera répété.pour envoyer à nouveau les donnés cliquer sur OK sinon sur annuler."

Eléphant du PHP | 353 Messages

25 sept. 2011, 09:45

La solution est de faire un redirect (fonction header) après avoir mis à jour la base.

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

25 sept. 2011, 10:51

Comme l'indique le navigateur dans son message, quand tu appuis sur F5 ou que tu cliques sur le bouton actualiser, tu vas recharger la page. Pour ce faire, le navigateur doit renvoyer les données qui ont permis d'arriver sur cette page et du coup ton traitement est exécutée à nouveau...
(à noter que tu auras la même chose si tu joues avec touches précédent/suivant du navigateur..)

Pour éviter cela, comme te le conseil nicolas, il est recommandé juste après ton traitement, de rediriger l'utilisateur vers une nouvelle page à l'aide de la fonction header('Location: ....');

Ainsi, si l'utilisateur tente de recharger la page (avec F5, actualiser, précédent/suivant...), c'est la page vers laquelle il a été redirigée qui s'actualise (et non pas celle qui contient le traitement), donc pas de nouvel enregistrement en base :)
Ce n'est pas en améliorant la bougie que l'on a inventé l'ampoule...

Eléphant du PHP | 187 Messages

25 sept. 2011, 12:36

Merci pour vos réponses. mais pour l'appel de cette header(location: fichier.php');
je vais appeler quoi par exemple comme page parce que normalement là j'en ai besoin d'aucune autre page tout mon code de ce formulaire passe sur la même page :s
merci

ViPHP
xTG
ViPHP | 7331 Messages

25 sept. 2011, 14:01

Bah sur la même page donc. :)

Eléphant du PHP | 187 Messages

25 sept. 2011, 20:45

re_salut,
beh j'ai ajouté ça : header('location:medecin.php');
en bas de tout mon code mais ça pas marché :( :(

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

25 sept. 2011, 20:50

yop,

fait voir le code sinon c'est des devinette :)

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

Eléphant du PHP | 187 Messages

25 sept. 2011, 21:11

ah oui merci voilà mon code :
c'est pas trop long ha :mrgreen:

Code : Tout sélectionner

<?php include('connexion.php'); $pharam_id = $_POST['ville'].$_POST['secteur'].$_SESSION['pharam_id']; $pharam_id2 = $_SESSION['pharam_id']; $pharam_id1 = $pharam_id2 + 1; $c_ordrepharmacien = $_POST['c_ordrepharmacien']; $c_ordrepharmacien1 = $_SESSION['c_ordrepharmacien']; $c_ordrepharmacien2 = $c_ordrepharmacien1 + 1 ; if(isset($_POST['submit'])){ $profil=$_POST['profil']; if(!empty($_POST['nom']) && !empty($_POST['prenom']) && !empty($_POST['ville']) && !empty($_POST['secteur']) && !empty($_POST['profil'])) { $pharam_id = $_POST['ville'].$_POST['secteur'].$_SESSION['pharam_id']; $city = $ville[$_POST['ville']]; $resp = mysql_query("SELECT secteur FROM secteur where id_sec = '".$_POST['secteur']."'"); $row = mysql_fetch_assoc($resp); $secteur = $row['secteur']; $q="select distinct(ville) from secteur where code_ville='".$_POST['ville']."' "; $r=mysql_query($q); $rq=mysql_fetch_array($r); $q1="select distinct(secteur) from secteur where code_secteur='".$_POST['secteur']."' "; $r1=mysql_query($q1); $rq1=mysql_fetch_array($r1); $profil=$_POST['profil']; if ( $profil == "medecin" ) { mysql_query('insert into partenaire ( pharam_id, nom, prenom, titre, client, activite, exercice, specialite, potentialite, prescripteur, adress_electronique, region, ville, code_postal, secteur, sous_secteur, tel, fixe, fax, commentaire, adress, c_ordremedecin, profil) values( "'.addslashes($pharam_id2).'","'.addslashes($_POST['nom']).'","'.addslashes($_POST['prenom']).'", "'.addslashes($_POST['titre']).'","'.addslashes($_POST['client']).'","'.addslashes($_POST['activite']).'", "'.addslashes($_POST['exercice']).'","'.addslashes($_POST['specialite']).'","'.addslashes($_POST['potentialite']).'", "'.addslashes($_POST['prescripteur']).'","'.addslashes($_POST['adress_electronique']).'","'.addslashes($_POST['region']).'", "'.addslashes($rq[0]).'","'.addslashes($_POST['code_postal']).'","'.addslashes($rq1[0]).'","'.addslashes($_POST['sous_secteur']).'", "'.addslashes($_POST['tel']).'","'.addslashes($_POST['fixe']).'","'.addslashes($_POST['fax']).'", "'.addslashes($_POST['commentaire']).'","'.addslashes($_POST['adress']).'", "'.addslashes($pharam_id).'","'.addslashes($profil).'" )')or die(mysql_error());?> <?php $str = mysql_query("select max(pharam_id) as id from partenaire")or die(mysql_error()); $row = mysql_fetch_assoc($str); $pharam_id1 = $row['id']; $p=$_POST['ville'].$_POST['secteur'].$pharam_id1 ; echo '<style="color:green;font-size:12" ><font color="bleu">Le Medecin Num '.$p.' a ete Bien Enregistree</font></h2>'; echo '<a href="medecin.php"></a>'; } /*ELSE*/ elseif($profil == "pharmacien") { $id= "pharam".$c_ordrepharmacien2 ; $str = mysql_query("select max(id_pharmacien) as id from pharmacien")or die(mysql_error()); $row = mysql_fetch_assoc($str); $c_ordrepharmacien2 = $row['id']; mysql_query ('insert into pharmacien ( nom, prenom, titre, client, activite, exercice, specialite, potentialite, prescripteur, adress_electronique, region, ville, code_postal, secteur, sous_secteur, tel, fixe, fax, commentaire, adress, profil, c_ordrepharmacien ) values( "'.addslashes($_POST['nom']).'","'.addslashes($_POST['prenom']).'", "'.addslashes($_POST['titre']).'","'.addslashes($_POST['client']).'","'.addslashes($_POST['activite']).'", "'.addslashes($_POST['exercice']).'","'.addslashes($_POST['specialite']).'","'.addslashes($_POST['potentialite']).'", "'.addslashes($_POST['prescripteur']).'","'.addslashes($_POST['adress_electronique']).'","'.addslashes($_POST['region']).'", "'.addslashes($rq[0]).'","'.addslashes($_POST['code_postal']).'","'.addslashes($rq1[0]).'","'.addslashes($_POST['sous_secteur']).'", "'.addslashes($_POST['tel']).'","'.addslashes($_POST['fixe']).'","'.addslashes($_POST['fax']).'", "'.addslashes($_POST['commentaire']).'","'.addslashes($_POST['adress']).'", "'.addslashes($profil).'","'.addslashes($c_ordrepharmacien2).'" ) ' ) or die(mysql_error()); ?> <?php $str = mysql_query("select max(id_pharmacien) as id from pharmacien")or die(mysql_error()); $row = mysql_fetch_assoc($str); $c_ordrepharmacien2 = "pharam".$row['id']; $str = mysql_query("select max(id_pharmacien) as id from pharmacien")or die(mysql_error()); $row = mysql_fetch_assoc($str); $c_ordrepharmacien2=0; $c_ordrepharmacien2 = $row['id']; $c_ordrepharmacien2 += 1; echo "pharam".$c_ordrepharmacien2 ; $_POST['c_ordrepharmacien'] = $c_ordrepharmacien2; mysql_query('insert into pharmacien(c_ordrepharmacien) values "'.addslashes($_POST['id_pharmacien']).'" )' ); echo '<style="color:green;font-size:12" ><font color="bleu">Le Pharmacien Num pharam '.$c_ordrepharmacien2.' a ete Bien Enregistree</font></h2>'; } }else{ $pharam_id1 = $pharam_id2; echo '<h3 style="color:red">Les champs * sont obligatoires </h3>'; } //}else{ } ?> <?php session_start(); ?> <form name="form2" id="form2"> <div align="left"> <p align="left" class="style19"> <font color="green">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><span class="style20">GESTION MEDECINS</span> </p> <p align="left" class="style19">&nbsp;</p> </div> </form> <p>&nbsp;</p> <form name="form1" action="" method="post" onSubmit="return controlform();"> <p> <label> <div align="center"> <span class="style14"> <input type="radio" name="profil" value="medecin"> <strong>Medecin </strong></span></label> <div align="center"><br> </div> <label> <div align="center"> <input type="radio" name="profil" value="pharmacien"> <span class="style14"><strong>Pharmacien</strong></span> <label> <table width="200" border="0"> <tr> <td align="center" style="color: black; background: #CCCCCC;"><?php $str = mysql_query("select max(id_pharmacien) as id from pharmacien")or die(mysql_error()); $row = mysql_fetch_assoc($str); $c_ordrepharmacien2=0; $c_ordrepharmacien2 = $row['id']; $c_ordrepharmacien2 += 1; echo "pharam".$c_ordrepharmacien2 ; $_POST['c_ordrepharmacien'] = $c_ordrepharmacien2; mysql_query('insert into pharmacien(c_ordrepharmacien) values "'.addslashes($c_ordrepharmacien2).'" ' ); ?></td> </tr> </table> </label> </div> </label> <div align="center"><br> <span class="style21">Localisation </span></div> <table width="1022" border="0"> <tr> <th width="85" height="28" valign="middle" scope="row"><div align="right" class="Style5"> <div align="left" class="style14"> R&eacute;gion <span style="color: red;">*</span> : </div> </div></th> <td width="132"> <select name="region" id="region" onChange="showMed(this.value)"> <option value="selectionner">selectionner</option> <?php $respo = mysql_query("SELECT DISTINCT(region) FROM secteur "); while($affiche_respo= mysql_fetch_array($respo)){ $id_sec = $affiche_respo['id_sec']; if(strlen($id_sec) != 2){ $id_sec = '0'.$id_sec; } echo '<option value="'.$affiche_respo['region'].'">'.$affiche_respo['region'].' </option>'; } ?> </select>&nbsp;</td> <td width="10"> <th width="98" height="28" valign="middle" scope="col"><div align="right" class="Style5"> <div align="left" class="style14"> <div align="right"> Ville <span style="color: red;">*</span>: </div> </div> </div></th> <th width="136" scope="col"> <div id="med-select-div"> <select name="ville" id="ville"> <option value="selectionner">selectionner</option> <?php $respo = mysql_query("SELECT * FROM secteur ORDER BY code_ville ASC "); while($affiche_respo= mysql_fetch_array($respo)){ $id_sec = $affiche_respo['id_sec']; if(strlen($id_sec) != 2){ $id_sec = '0'.$id_sec; } echo '<option value="'.$affiche_respo['code_ville'].'">'.$affiche_respo['ville'].' '.$affiche_respo['code_ville'].'</option>'; } ?> </select> </div></th> <td width="10"> <th width="98" height="38" valign="middle" scope="row"><div align="right" class="Style5"> <div align="right" class="Style5"> <div align="left" class="style14"> <div align="right"> secteur <span style="color: red;">*</span>: </div> </div> </div> </div></th> <td width="118"><select name="secteur" id="secteur"> <option value="selectionner">selectionner</option> <?php $respo = mysql_query("SELECT * FROM secteur ORDER BY secteur ASC"); while($affiche_respo= mysql_fetch_array($respo)){ $id_sec = $affiche_respo['id_sec']; echo '<option value="'.$affiche_respo['code_secteur'].'">'.$affiche_respo['secteur'].' '.$affiche_respo['code_secteur'].'</option>'; } ?> </select> &nbsp;</td> <th width="123" height="28" valign="middle" scope="col"><div align="right" class="Style5"> <div align="left" class="style14">Sous Secteur :</div> </div></th> <th width="196" scope="col"> <div align="left"> <select name="sous_secteur" id="sous_secteur"> <option value="selectionner">selectionner</option> <?php $respo = mysql_query("SELECT * FROM secteur ORDER BY secteur ASC"); while($affiche_respo= mysql_fetch_array($respo)){ $id_sec = $affiche_respo['id_sec']; echo '<option value="'.$affiche_respo['sous_secteur'].'">'.$affiche_respo['sous_secteur'].' '.$affiche_respo['sous_secteur'].'</option>'; } ?> </select> </div></th> </tr> </table> <table width="935" align="center" cellpadding="3"> <?php $str = mysql_query("select max(pharam_id) as id from partenaire")or die(mysql_error()); $row = mysql_fetch_assoc($str); $pharam_id1 = $row['id']; $pharam_id1 += 1; while(strlen($pharam_id1) != 3){ $pharam_id1 = '0'.$pharam_id1; }?> <tr align="center"> <td width="293" height="329" valign="top"> <div align="left"> <span class="style21">Identification </span> <table width="287" align="right" cellpadding="3"> <tr> <th width="123" height="33" scope="col"><div align="right" class="Style5"> <div align="left" class="style14"> <u>Code Medecin</u> <span style="color: red;">*</span>: </div> </div></th> <th width="144" scope="col"> <div align="left" style="color: black; background: #CCCCCC; text-align: center;"> <p><?php echo $pharam_id1; $_SESSION['pharam_id'] = $pharam_id1; ?></p> </div></th> </tr> <tr> <th height="36" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Titre :</div> </div></th> <td><label></label> <label> <select name="titre"> <option selected="selected" value="Selectionnez">Selectionnez :</option> <option>Docteur</option> <option>Professeur</option> <option>Resident</option> <option>Maitre</option> <option>Interne</option> </select> </label></td> <tr> <th height="46" scope="row"><div align="right" class="Style5"> <div align="left" class="style14"> <p>Adresse:</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>Tel fixe :</p> </div> </div></th> <td><p> <textarea name="adress"></textarea> </p> <p>&nbsp;</p> <p> <label> <input name="fixe" type="text" id="fixe" /> </label> </p></td> </tr> <tr> <th height="43" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Prescripteur :</div> </div></th> <td><select name="prescripteur" id="prescripteur"> <option selected="selected" value="selectionnez">Selectionnez :</option> <option value="fort">Fort: 1</option> <option value="moyen">Moyen: 2</option> <option value="moyen">Faible: 3</option> </select></td> </tr> <tr> <th height="52" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Potentialit&eacute; :</div> </div></th> <td><select name="potentialite" id="potentialite"> <option selected="selected" value="">Selectionnez :</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> </select></td> </tr> </table> </div> <p align="center">&nbsp;</p></td> <td width="622" valign="middle"> <div align="center"> <table width="523" align="center" cellpadding="3"> <tr> <th width="83" height="36" valign="top" scope="row"><div align="right" class="Style5"> <div align="left" class="style14"> Nom <span style="color: red;">*</span>: </div> </div></th> <td width="163" valign="top"><input name="nom" type="text" id="nom" /></td> <th width="79" height="36" valign="top" scope="row"><div align="right" class="Style5"> <div align="left" class="style14"> Pr&eacute;nom <span style="color: red;">*</span>: </div> </div></th> <td width="162" valign="top"><input name="prenom" type="text" id="prenom" /></td> </tr> <tr> <th height="28" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Sp&eacute;cialit&eacute; :</div> </div></th> <td><select name="specialite" id="specialite"> <option selected="selected" value="">Selectionnez :</option> <option value="generaliste">Generaliste</option> <option value="gynecologue">Gynecologue</option> <option value="chirurgien">Chirurgien</option> <option value="kinesitherapeute">Kinesitherapeute</option> <option value="dermatologue">Dermatologue</option> <option value="neurochirurgien">Neurochirurgien</option> <option value="endocrinologue">Endocrinologue</option> <option value="gastro_enterologue ">Gastro-enterologue</option> <option value="urgentiste ">Urgentiste</option> <option value="pediatre">Pediatre</option> <option value="pneumologue ">Pneumologue</option> <option value="podologue">Podologue</option> <option value="rhumatologue ">Rhumatologue</option> <option value="traumatologue ">Traumatologue</option> <option value="Urologue">Urologue</option> <option value="orthopediste">Orthopediste</option> <option value="cardiologue">Cardiologue</option> <option value="Urologue">Ophtalmologue</option> <option value="orl">ORL</option> <option value="psychiatre">Psychiatre</option> <option value="medecindesport">Medecin de sport</option> </select></td> <th width="123" height="43" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Exercice :</div> </div></th> <td width="144"><select name="exercice"> <option selected="selected" value="">Selectionnez :</option> <option>SIAAP</option> <option>Cabinet Prive</option> <option>Hopital</option> <option>Polyclinique</option> </select></td> </tr> <tr> <th height="38" scope="row"><div align="right" class="Style5"> <div align="right" class="Style5"> <div align="left" class="style14">Code postal</div> </div> </div></th> <td><input name="code_postal" type="text" id="code_postal" /> &nbsp;</td> <th height="38" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Email :</div> </div></th> <td><input name="adress_electronique" type="text" id="adress_electronique" /></td> </tr> <tr> <th height="38" scope="row"><div align="right" class="Style5"> <div align="right" class="Style5"> <div align="left" class="style14">Tel fax :</div> </div> </div></th> <td><input name="fax" type="text" id="fax" /> &nbsp;</td> <th height="38" scope="row"><div align="right" class="Style5"> <div align="right" class="Style5"> <div align="left" class="style14">Tel Gsm :</div> </div> </div></th> <td><input name="tel" type="text" id="tel" /> &nbsp;</td> </tr> <tr> <th height="53" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Client :</div> </div></th> <td><label> <input name="client" type="radio" value="actif" /> <span class="style22">Actif</span> </label> <p> <label> <input name="client" type="radio" value="nonactif" /> <span class="style22">Non Actif</span> </label> </p></td> <th height="34" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Activit&eacute; :</div> </div></th> <td><label> <input type="checkbox" name="activite" value="pub" /> <span class="style22">Publique</span> <input type="checkbox" name="activite" value="priv" /> <span class="style22">Priv&eacute;</span> </label></td> </tr> <tr> <th height="46" scope="row"><div align="right" class="Style5"> <div align="left" class="style14">Commentaire:</div> </div></th> <td><label> <textarea name="commentaire"></textarea> </label></td> <tr> </tr> </table> </div></td> </tr> </table> <p>&nbsp;</p> <table width="843" height="52" border="0" align="center"> <tr> <td width="241" align="center" valign="top"><div align="right"></div> </td> <td width="592" valign="top"> <div align="left"> <input name="submit" class="Style1" value="Enregistrer" style="width: 25%" type="submit" /> <input type="reset" id="submit" value="vider" class="Style1" style="width: 25%" /> </div></td> </table> <?php session_destroy(); header('location:medecin.php'); ?> </form> </body> </html>