IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Vérification champs formulaire


Sujet :

Langage PHP

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 81
    Points : 33
    Points
    33
    Par défaut Vérification champs formulaire
    Bonjour,

    J'ai créé un forumaire php pour insérer des enregistrements.

    Je souhaiterais vérifier les 2 champs prenom et message et afficher un echo si les champs sont vide.

    Voici le code de ma page :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    <?php require_once('Connections/deejayfloo.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
     
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
     
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
     
    $currentPage = $_SERVER["PHP_SELF"];
     
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
     
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO guestbook (prenom, message) VALUES (%s, %s)",
                           GetSQLValueString($_POST['prenom'], "text"),
                           GetSQLValueString($_POST['message'], "text"));
     
      mysql_select_db($database_deejayfloo, $deejayfloo);
      $Result1 = mysql_query($insertSQL, $deejayfloo) or die(mysql_error());
     
      $insertGoTo = "guestbook.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
     
    $maxRows_Recordset1 = 2;
    $pageNum_Recordset1 = 0;
    if (isset($_GET['pageNum_Recordset1'])) {
      $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
    }
    $startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;
     
    mysql_select_db($database_deejayfloo, $deejayfloo);
    $query_Recordset1 = "SELECT * FROM guestbook";
    $query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
    $Recordset1 = mysql_query($query_limit_Recordset1, $deejayfloo) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
     
    if (isset($_GET['totalRows_Recordset1'])) {
      $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
    } else {
      $all_Recordset1 = mysql_query($query_Recordset1);
      $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
    }
    $totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
     
    $queryString_Recordset1 = "";
    if (!empty($_SERVER['QUERY_STRING'])) {
      $params = explode("&", $_SERVER['QUERY_STRING']);
      $newParams = array();
      foreach ($params as $param) {
        if (stristr($param, "pageNum_Recordset1") == false && 
            stristr($param, "totalRows_Recordset1") == false) {
          array_push($newParams, $param);
        }
      }
      if (count($newParams) != 0) {
        $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
      }
    }
    $queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <!-- saved from url=(0014)about:internet -->
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>DeeJay FloO | GuestBoOk</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    td img {display: block;}body {
    	background-color: #000000;
    }
    .Style1 {
    	color: #FFFFFF;
    	font-size: 12px;
    }
    </style>
    <!--Fireworks CS3 Dreamweaver CS3 target.  Created Mon Jul 06 12:03:15 GMT+0200 2009-->
    <script src="Scripts/swfobject_modified.js" type="text/javascript"></script>
    <script language="JavaScript" type="text/JavaScript">
    <!--
     function addSmiley(codesmiley)
      {
      var msg = document.form1.message; // Remplace formulaire par le nom de ton formulaire
                                              // et zonetext par le nom de la textbox ou textarrea
     
       // Compatible IE
       if(document.selection)
       {
       msg.focus();
       sel = document.selection.createRange();
       sel.text = codesmiley;
       document.step2.focus();
       }
      else if(msg.selectionStart || msg.selectionStart == "0")
       {
       var startPos = msg.selectionStart;
       var endPos = msg.selectionEnd;
       var chaine = msg.value;
     
       msg.value = chaine.substring(0, startPos) + codesmiley + chaine.substring(endPos, chaine.length);
       msg.selectionStart = startPos + codesmiley.length;
       mdg.selectionEnd = endPos + codesmiley.length;
       msg.focus();
       }
      else
       {
       msg.value += codesmiley;
       msg.focus();
       }
      }
    //-->
    </script>
    </head>
    <link rel="stylesheet" href="css.css" charset="ISO-8859-1" type="text/css"/>
    <body>
    <div align="center">
      <table border="0" cellpadding="0" cellspacing="0" width="1200">
        <!-- fwtable fwsrc="Flo_V2.png" fwpage="Page 1" fwbase="index.jpg" fwstyle="Dreamweaver" fwdocid = "118373649" fwnested="0" -->
        <tr>
          <td><img src="images/spacer.gif" width="115" height="1" border="0" alt="" /></td>
          <td><img src="images/spacer.gif" width="680" height="1" border="0" alt="" /></td>
          <td><img src="images/spacer.gif" width="8" height="1" border="0" alt="" /></td>
          <td><img src="images/spacer.gif" width="284" height="1" border="0" alt="" /></td>
          <td><img src="images/spacer.gif" width="113" height="1" border="0" alt="" /></td>
          <td><img src="images/spacer.gif" width="1" height="1" border="0" alt="" /></td>
        </tr>
     
        <tr>
          <td rowspan="6"><img name="index_r1_c1" src="images/index_r1_c1.jpg" width="115" height="800" border="0" id="index_r1_c1" alt="" /></td>
          <td rowspan="3"><img name="index_r1_c2" src="images/index_r1_c2.jpg" width="680" height="342" border="0" id="index_r1_c2" alt="" /></td>
          <td colspan="2"><img name="index_r1_c3" src="images/index_r1_c3.jpg" width="292" height="104" border="0" id="index_r1_c3" alt="" /></td>
          <td rowspan="6"><img name="index_r1_c5" src="images/index_r1_c5.jpg" width="113" height="800" border="0" id="index_r1_c5" alt="" /></td>
          <td><img src="images/spacer.gif" width="1" height="104" border="0" alt="" /></td>
        </tr>
        <tr>
          <td rowspan="5"><img name="index_r2_c3" src="images/index_r2_c3.jpg" width="8" height="696" border="0" id="index_r2_c3" alt="" /></td>
          <td><img name="index_r2_c4" src="images/index_r2_c4.jpg" width="284" height="192" border="0" id="index_r2_c4" alt="" /></td>
          <td><img src="images/spacer.gif" width="1" height="192" border="0" alt="" /></td>
        </tr>
        <tr>
          <td rowspan="2"><img name="index_r3_c4" src="images/index_r3_c4.jpg" width="284" height="55" border="0" id="index_r3_c4" alt="" /></td>
          <td><img src="images/spacer.gif" width="1" height="46" border="0" alt="" /></td>
        </tr>
        <tr>
          <td rowspan="2">&nbsp;</td>
          <td><img src="images/spacer.gif" width="1" height="9" border="0" alt="" /></td>
        </tr>
        <tr>
          <td rowspan="2"><img name="index_r5_c4" src="images/index_r5_c4.jpg" width="284" height="449" border="0" id="index_r5_c4" alt="" /></td>
          <td><img src="images/spacer.gif" width="1" height="69" border="0" alt="" /></td>
        </tr>
        <tr>
          <td background="images/index_r6_c2.jpg" valign="top"><div align="right"><span class="titre_page"><br />
            </span>
              <table width="100%" border="0">
                <tr>
                  <td><table border="0" align="left">
                    <tr>
                      <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
                          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>"><img src="First.gif" border="0" /></a>
                          <?php } // Show if not first page ?>                  </td>
                      <td><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
                          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>"><img src="Previous.gif" border="0" /></a>
                          <?php } // Show if not first page ?>                  </td>
                      <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
                          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>"><img src="Next.gif" border="0" /></a>
                          <?php } // Show if not last page ?>                  </td>
                      <td><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
                          <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>"><img src="Last.gif" border="0" /></a>
                          <?php } // Show if not last page ?>                  </td>
                    </tr>
                  </table></td>
                  <td><div align="right"><span class="titre_page">GuestBook &nbsp;&nbsp;</span></div></td>
                </tr>
              </table>
          </div>
            <hr />
            <div align="center">
    <table width="95%" border="0">
      <?php do { ?>
        <tr>
          <td><div align="left" class="date_news"><span class="Style1">Ecrit par : </span><?php echo $row_Recordset1['prenom']; ?></div></td>
          </tr>
        <tr>
          <td bgcolor="#FFFFFF"><div align="left" class="message_guestbook"><?php $mess = stripslashes($row_Recordset1['message']);
    $mess = preg_replace('#:D#i','<img src="smileys/7.png">', $mess);
    	$mess = preg_replace('#:\)#','<img src="smileys/4.png">', $mess);
    	echo $mess;
    ?>
              <hr color="#FF33CC"/>
          </div></td>
          </tr>
        <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
    </table>
    </div>
            <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
            <table width="65%" align="center">
              <tr valign="middle">
                <td align="right" nowrap="nowrap" class="date_news"><div align="left">Pseudo:</div></td>
                <td><table width="100%" align="left">
                  <tr>
                    <td width="54%"><div align="left">
                      <input type="text" name="prenom" value="" size="20" />
                    </div></td>
                    <td width="46%"><div align="left">
                      <table width="200" align="right" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
                          <tr>
                            <td><a href="#" onclick="addSmiley(' :)'); return(false)"><img src="smileys/1.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' :(('); return(false)"><img src="smileys/2.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' :('); return(false)"><img src="smileys/3.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' :@'); return(false)"><img src="smileys/4.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' :/'); return(false)"><img src="smileys/5.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' :d'); return(false)"><img src="smileys/6.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' :o'); return(false)"><img src="smileys/7.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' (h)'); return(false)"><img src="smileys/8.png" width="15" height="15" border="0" /></a></td>
                            <td><a href="#" onclick="addSmiley(' ;)'); return(false)"><img src="smileys/9.png" width="15" height="15" border="0" /></a></td>
                            <td><div align="center"><a href="#" onclick="addSmiley(' (l)'); return(false)"><img src="smileys/10.png" width="15" height="15" border="0" /></a></div></td>
                          </tr>
                                      </table>
                    </div></td>
                  </tr>
                </table></td>
                <td valign="middle">&nbsp;</td>
              </tr>
              <tr valign="top">
                <td width="63" align="right" nowrap="nowrap" class="date_news"><div align="left">Message:</div></td>
                <td width="344"><div align="left">
                  <table width="100%" border="0">
                     <tr>
                       <td><textarea name="message" cols="45" rows="4"></textarea></td>
                     </tr>
                   </table>
                </div></td>
                <td width="129" valign="middle"><input type="submit" class="bouton_guestbook" value="Postez le Message !" /></td>
              </tr>
            </table>
            <input type="hidden" name="MM_insert" value="form1" />
            </form>      </td>
          <td><img src="images/spacer.gif" width="1" height="380" border="0" alt="" /></td>
        </tr>
      </table>
    </div>
    <script type="text/javascript">
    <!--
    swfobject.registerObject("FlashID");
    //-->
    </script>
    </body>
    </html>
    <?php
    mysql_free_result($Recordset1);
    ?>
    Merci d'avance

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mai 2007
    Messages
    81
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 81
    Points : 33
    Points
    33
    Par défaut
    J'essaye par moi même, voilà mon code mais ca ne fonctionne pas :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    $msg_erreur = "Erreur. Les champs suivants doivent être obligatoirement remplis :<br/><br/>";
    $msg_ok = "Votre demande a bien été prise en compte.";
    $message = $msg_erreur;
     
    // vérification des champs
     
    if (empty($_POST['prenom']))
      $message .= "Votre prenom<br/>";
    if (empty($_POST['message']))
      $message .= "Votre message<br/>";
     
     
    // si un champ est vide, on affiche le message d'erreur
     
    if (strlen($message) > strlen($msg_erreur)) {
     
      echo $message;
     
     
    // sinon c'est ok
     
    } else { ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO guestbook (prenom, message) VALUES (%s, %s)",
                           GetSQLValueString($_POST['prenom'], "text"),
                           GetSQLValueString($_POST['message'], "text"));
     
      mysql_select_db($database_deejayfloo, $deejayfloo);
      $Result1 = mysql_query($insertSQL, $deejayfloo) or die(mysql_error());
     
      $insertGoTo = "guestbook.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
     
      if ($Result1) {
        echo $msg_ok;
      } else {
        echo mysql_error();
      }
    Voyez vous l'erreur ?

  3. #3
    Membre régulier Avatar de sub-0
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2008
    Messages
    197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2008
    Messages : 197
    Points : 76
    Points
    76
    Par défaut
    Je crois qu'il te manque un if ici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    else { >if<((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {...
    puis vous devez fermer l'accolade du else .
    bon courage

Discussions similaires

  1. Vérification champ formulaire Paypal
    Par poupou19 dans le forum Langage
    Réponses: 2
    Dernier message: 19/11/2012, 15h39
  2. [MySQL] vérification champs formulaire
    Par mouna87 dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 13/06/2010, 10h19
  3. Réponses: 8
    Dernier message: 26/05/2009, 12h18
  4. Vérification champs formulaire
    Par Invité dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 29/04/2008, 09h12
  5. Réponses: 5
    Dernier message: 12/07/2005, 17h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo