Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/05/2013, 07:49
marisanjr
 
Fecha de Ingreso: abril-2013
Mensajes: 53
Antigüedad: 11 años
Puntos: 0
Validar combo llenado con una consulta mysql

Hola que tal buenos días tengo el siguiente código:

Código:
<!DOCTYPE html>
<?php
session_start();
include ("connect_db.php");

?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <script src="jquery.js" type="text/javascript"></script>
        <script src="js/jquery.maskedinput.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#pais').change(function(){
                    var id=$('#pais').val();
                    $('#municipio').load('ajax.php?id='+id);
                });
                $("#form1").submit(function () {
                  if($("#nomcon").val().length < 1) {
			alert("Debes Ingresar Nombre");
			return false;
                  }
                  if($("#apecon").val().length < 1) {
			alert("Debes Ingresar el Apellido");
			return false;
                  }
                  if($("#celcon").val().length < 1) {
			alert("Debes Ingresar un N&uacutemero Celular");
			return false;
                   }
                   if(document.form1.pais.options.selectedIndex==-1){
			alert("Debe Seleccionar un Municipio");
			return false;
		   }
		   if($("#combop option:selected").val() == 0) {
			alert("Debe Seleccionar un Partido Pol&iacutetico");
			return false;
		}


                 });
            });
        </script>
    </head>
<body>
     <FORM name="form1" id="form1" method="POST" action=""/>

    	      <center><table border="1"></center>
                <thead>
                       <tr>
                           <th colspan="5"><H3>INGRESAR CONTACTO</H3></th>
                           <h4>Esta es la creaci&oacuten de Contactos de: <?php echo $_SESSION["nombre"]." ".$_SESSION["apaterno"]." ".$_SESSION["amaterno"]; ?></h4>
                             <font face="arial" size="1"> Los Campos Con (*) Son Requeridos</font>
                       </tr>
                   </thead>
               <tbody>
                   <tr>
                        <td>
                        <label for=nocedula">DOCUMENTO IDENTIDAD</label>
                         <input type="text"   name="nident" id="nident" size="18" value="" />
                            <input type=text   name="coord"  style="display:none" size="2" value="<?php echo $_SESSION["id_usuario"];?>" />
                        </td>
                          <td cospan="2"> <label for="nomcon">*NOMBRE</label>
                            <input type="text"   name="nomcon" id="nomcon" size="35" value="" />
                       </td>
                       <td> <label for="apecon">*APELLIDO</label>
                             <input type="name"   name="apecon" id="apecon" size="30"/>
                       </td>
                       <tr>
                       <td>
                            <label for="dircon">DIRECCION</label>
                          <input type="name"   name="dircon" size="40"/>
                        </td>
                        <td>
                         <label for="telcon">TELEFONO</label>
                         <input type="name"   name="telcon"  size="15"/>
                        </td>
                        <td>
                            <label for="celcon">*CELULAR</label>
                            <input type="name"   name="celcon" id="celcon" size="15"/>
                        </td>
                        <td colspan="2">
                            <label for="emacon"> EMAIL</label>
                            <input type="name"   name="emacon"  size="50"/>
                        </td>
                   </tr>
                   <tr>
                       <td>
                                  <?php
                                     $consulta=mysql_query("select * from departamento order by nompto ASC");
                                    echo "*DEPARTAMENTO<select name='pais' id='pais'><option selected>Seleccione un Departamento</option>";
                                      while ($fila=mysql_fetch_array($consulta)){
                                         echo "<option value='".$fila[0]."'>".utf8_encode($fila[1])."</option>";
                                       }
                                        echo "</select>";
                                      ?>
                       </td>
                       <td>
                            <label for="edo">*MUNICIPIO</label>
                             <div id="municipio">
                 <select name="edo" disabled="disabled">
                    <option value="">Seleccione un Municipio</option>
                 </select>
             </div>
          </select>
                   </tr>
                       <td>
                            <?php
                          require("connect_db.php");
                           $result = mysql_query("SELECT * FROM partidopolitico ORDER BY nompartido ASC");
                        ?>
                        <label for="combop">*MOVIMIENTO POLITICO</label>
                        <select name="combop" id="combop" style="width:150px">
                           <option value="">Seleccione un Partido Pol&iacutetico</option>
                           <?php
                               while($fila = mysql_fetch_array($result)) {
                                     echo "<option value='".$fila[0]."'>".utf8_encode($fila[1])."</option>";
                               }
                             ?>
                       </select>
                        </td>

                       <td>
                       <label for="comboca">*CARGO ACTUAL/EXAUTORIDAD</label>
                      <div id="cargo" style="width:200px; float:left">
                      <select name="comboca" id="comboca" onchange="activarCampo()">
                      <option value="">Seleccione un Cargo</option>
                     <?php
                             require("connect_db.php");
                             $result = mysql_query("SELECT * FROM cargo ORDER BY nomcargo ASC");
                             while($fila = mysql_fetch_array($result)) {
                             echo "<option value='".$fila[0]."'>".utf8_encode($fila[1])."</option>";
                               }
                        ?>
                         </select>
                     </div>
        </tr>
             <tr>
                        <td><center><input type="submit"   name="submit"   value="Guardar"/></center></td>
                      <td><center>  <input type="reset"/></center></td>
                      <td><center><input type="submit"   name="submit1"   value="Menú"/></center></td>
                  </tr>
               </tbody>
             </table>

          </form>
        <?php
        if (isset($_POST['submit'])) {
          require("registrocontacto.php");
        }
       ?>

     </body>
</html>
y lo que deseo es validar:

1.- que si el usuario da clic en el boton guardar le mande un mensaje si esta vacio alguno de los campos requeridos.

la validación de los campos textos funcionan. Pero no he podido con los combos . He utilizado
Código:
                  if(document.form1.pais.options.selectedIndex==-1){
			alert("Debe Seleccionar un Departamento");
			return false;
		   }
También he probado con :
Código:
      
		   if($("#pais option:selected").val() == 0) {
			alert("Debe Seleccionar un Departamento");
			return false;
		}

Pero no lo tiene en cuenta
desde ya muchisimas gracias, es importante mencionar que soy un poco nueva con el php