Ver Mensaje Individual
  #6 (permalink)  
Antiguo 01/03/2013, 11:57
Avatar de mager3
mager3
 
Fecha de Ingreso: junio-2012
Ubicación: Medellin
Mensajes: 318
Antigüedad: 12 años, 9 meses
Puntos: 4
Respuesta: problema al validar dato

mira mi codigo es este completo: con este codigo valido que no hayan campos vacios en el formulario, si estan vacios se retorna y muestra mensaje de debes llenar los datos XXXXXX , si estan llenos los campos permite el INSERT

Código PHP:
Ver original
  1. <?php
  2. include ("conexion.php");
  3. $documento=$_SESSION['documento_user'];
  4. $id_prueba=$_GET['id_prueba'];
  5. $id_convocatoria=$_GET['i'];
  6. $id_cargo=$_GET['c'];
  7. $documento_postulante=$_GET['p'];
  8. $observacion=$_POST['txtobservaciones'];
  9. $puntuacion=$_POST['txtpuntuacion'];
  10.  
  11. function valida_datos() {
  12.       global $form_result;
  13.      
  14.       if(isset($_POST['txtobservacion']) && empty( $_POST['form1'] ) ) {
  15.           return false;
  16.       }
  17.  
  18.       $message = "Favor de completar los siguientes campos: \n";
  19.       $result = true;
  20.       if( empty( $_POST['txtobservaciones'] ) ) {
  21.            $message .= " - Debes diligenciar las observaciones\n";
  22.            echo "<html><head></head>". "<body onload=\"javascript:history.back()\">"."</body></html>";
  23.            $result = false;
  24.       }
  25.        if( empty( $_POST['txtpuntuacion'] ) ) {
  26.            $message .= " - Debes diligenciar la puntuacion obtenida\n";
  27.            echo "<html><head></head>". "<body onload=\"javascript:history.back()\">"."</body></html>";
  28.            $result = false;
  29.            }
  30.         if( !$result ) {
  31.            $form_result = sprintf( "<script>alert( '%s' );</script>", str_replace( "\n", "\\n", $message ) );
  32.       }
  33.      
  34.       return $result;
  35. }      
  36. $form_result = "";
  37. $consulta_prueba=mysql_query("SELECT descrip_prueba FROM pruebas WHERE cod_prueba='$id_prueba'");
  38.  
  39.   while($row = mysql_fetch_array($consulta_prueba))
  40.   {
  41.     $descripcion_prueba=$row[0];
  42.   }
  43. mysql_free_result($consulta_prueba);
  44.  
  45.     if( valida_datos()){   // realizo validaciones que los campos no esten vacios
  46.     mysql_query("INSERT INTO detalle_pruebas(id_convocatoria,id_cargo,id_prueba,observaciones_prueba,puntuacion,documento_postulante,documento_profesional)
  47.     VALUES('$id_convocatoria','$id_cargo','$id_prueba','$observacion','$puntuacion','$documento_postulante','$documento')");
  48.     echo "<META HTTP-EQUIV='Refresh' CONTENT='0;URL=seguimiento.php?documento=$documento'>";
  49. }
  50. echo $form_result;
  51.  
  52. ?>
  53. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  54. <html xmlns="http://www.w3.org/1999/xhtml">
  55. <head>
  56. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  57. <title>Ingresar Novedades</title>
  58. <style type="text/css">
  59. <!--
  60. .Estilo16 {color: #FFFFFF;
  61.     font-weight: bold;
  62. }
  63. body {
  64.     background-image: url(imagenes/fondo.jpg);
  65. }
  66. .rojo {
  67. background-color: #D64938;
  68. color: #ffffff;
  69. }
  70. .verde {
  71. background-color: #009900;
  72. color: #ffffff;
  73. }
  74. -->
  75. </style>
  76. <script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
  77. <link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
  78. <style type="text/css">
  79. <!--
  80. a:link {
  81.     color: #FFFFFF;
  82.     text-decoration: none;
  83. }
  84. a:visited {
  85.     text-decoration: none;
  86.     color: #FFFFFF;
  87. }
  88. a:hover {
  89.     text-decoration: underline;
  90.     color: #FFFFFF;
  91. }
  92. a:active {
  93.     text-decoration: none;
  94.     color: #FFFFFF;
  95. }
  96. .Estilo17 {
  97.     color: #D64938;
  98.     font-weight: bold;
  99. }
  100. .Estilo18 {
  101.     color: #009900;
  102.     font-weight: bold;
  103. }
  104. -->
  105. </style></head>
  106.  
  107. <body>
  108. <table width="1132" border="0" align="center">
  109.   <tr>
  110.     <td align="left" bgcolor="#5AA3D0"><span class="Estilo16"><img src="imagenes/LogotipoCoopsanaCurvasBlanco.jpg" alt="Coopsana" width="247" height="72" longdesc="Coopsana IPS" />Gestión Humana</span></td>
  111.   </tr>
  112.   <tr>
  113.     <td align="left" bgcolor="#5AA3D0"><font color="ffffff"><?php echo "Bienvenido ".$_SESSION['nombre_user']; ?> </font> </td>
  114.   </tr>
  115. </table>
  116. <form id="form1" name="form1" method="post" action="<?php if( !empty( $_POST['txtobservaciones'] ) ) { echo $_POST['txtobservaciones']; } ?>">
  117.   <table width="797" border="0" align="center">
  118.     <tr>
  119.       <td colspan="4" bgcolor="#5AA3D0"><div align="center"><span class="Estilo16">INGRESAR RESULTADO</span></div></td>
  120.     </tr>
  121.     <tr>
  122.       <td colspan="4">&nbsp;</td>
  123.     </tr>
  124.     <tr>
  125.       <td width="139" bgcolor="#5AA3D0"><span class="Estilo16">Tipo de Prueba</span></td>
  126.       <td colspan="3">&nbsp;<b><?php echo $descripcion_prueba; ?></b></td>
  127.     </tr>
  128.     <tr>
  129.       <td bgcolor="#5AA3D0"><span class="Estilo16">Observación</span></td>
  130.       <td colspan="3"><label>
  131.         <textarea name="txtobservaciones" cols="75" rows="8" id="txtobservaciones"></textarea>
  132.       </label></td>
  133.     </tr>
  134.     <tr>
  135.       <td bgcolor="#5AA3D0"><span class="Estilo16">Puntuación</span></td>
  136.       <td width="259">
  137.         <select name="txtpuntuacion" id="txtpuntuacion">
  138.           <option class="rojo"></option>
  139.           <option class="rojo">0</option>
  140.           <option  class="verde">1</option>
  141.         </select>
  142.       </td>
  143.       <td width="177"><span class="Estilo17">Valor 0: No Cumple</span></td>
  144.       <td width="204"> <span class="Estilo18">Valor 1: Si Cumple</span></td>
  145.     </tr>
  146.     <tr>
  147.       <td colspan="4">&nbsp;</td>
  148.     </tr>
  149.     <tr>
  150.       <td colspan="4"><label>
  151.         <div align="center">
  152.           <input type="submit" name="enviar" id="enviar" value="Enviar" />
  153.           <table width="181" border="0" align="center">
  154.             <tr>
  155.               <td width="93" bgcolor="#D64938"><div align="center"><span class="Estilo16"><a href="principal_administrador.php">Ir a Principal</a></span></div></td>
  156.               <td width="10" bgcolor="#FFFFFF">&nbsp;</td>
  157.               <td width="67" bgcolor="#D64938"><div align="center" class="Estilo16"><a href="javascript:history.back(1)">Regresar</a></div></td>
  158.             </tr>
  159.           </table>
  160.         </div>
  161.       </label></td>
  162.     </tr>
  163.   </table>
  164. </form>
  165. <p>&nbsp;</p>
  166. </body>
  167. </html>