Foros del Web » Programando para Internet » Javascript »

problema con input radio

Estas en el tema de problema con input radio en el foro de Javascript en Foros del Web. hola a todos tengo las sguientes opciones que le permiten al usuario escoger su sexo Código PHP: F < input name = "sexo"  type = "radio"  ...
  #1 (permalink)  
Antiguo 16/06/2009, 09:53
 
Fecha de Ingreso: noviembre-2008
Mensajes: 80
Antigüedad: 15 años, 5 meses
Puntos: 1
problema con input radio

hola a todos tengo las sguientes opciones que le permiten al usuario escoger su sexo
Código PHP:
F<input name="sexo" type="radio" value="Femenino">
        
M<input name="sexo" type="radio" value="Masculino"
mi problema biene que cuando voy a guardar la informacion en la base de datos el campo sexo siempre su valor es Masculino sin importar cual escoja el usuario es decir no me guarda la opcion escogida.
agradeceria muchisimo su respuesta respecto a este tema
... aahhhh antes de guardar la informacion primero valido el formulario con una funcion en javascript no se si depronto tenga algo que ver
  #2 (permalink)  
Antiguo 16/06/2009, 10:04
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: problema con input radio

Muestra el código de la validación y si no es muy largo también el código del formulario completo; ya que de la forma como está debería funcionar.
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #3 (permalink)  
Antiguo 16/06/2009, 10:26
 
Fecha de Ingreso: noviembre-2008
Mensajes: 80
Antigüedad: 15 años, 5 meses
Puntos: 1
Respuesta: problema con input radio

Cita:
Iniciado por David el Grande Ver Mensaje
Muestra el código de la validación y si no es muy largo también el código del formulario completo; ya que de la forma como está debería funcionar.
este es la funcion de validacion
Código PHP:
function valida_ing(form){
      var 
Formulario document.getElementById(form);
      var 
num=Formulario.elements.length;
      var 
nc="";
      for (var 
i=0i<=num-1i++) {
          if (
Formulario.elements[i].value==''){
           
nc+=Formulario.elements[i].name+' Es un valor requerido. \n';
           }
         }
   if (
nc==''){
        
document.forms[0].submit();
    }
    else {
    
alert('Por Favor Corrige los siguintes campos:\n'+nc);
    }
   } 
y este es el codigo del formulario
Código PHP:
<form name="form1" method="post" action="javascript:enviarFormulario('../dao/php_estudiante/nuevo.php','form1');">
   <table width="700">
    <caption align="top">
      <span class="Estilo4"><img src="../Imagenes/nuevo_est.png" width="500" height="60" border="0"></span>
    </caption>
    <tr><th colspan="2"><hr></th></tr>
    <tr><th>
    <table align="left">
    <tr>
      <th align="right">Identificacion</th>
      <td align="left"><input name="id_est" type="text" value="<?php echo "".$_POST['id_est']; ?>"  onKeyPress="return acceptNum(event)"></td>
    </tr>
    <tr>
      <th align="right">Nombres</th>
      <td align="left"><input name="nom_est" size="40" type="text" value="<?php echo"".$_POST['nom_est'];?>" onKeyUp="this.value=this.value.toUpperCase();" id="nom_adm"></td>
    </tr>
    <tr>
      <th align="right">Direccion</th>
      <td align="left"><input name="dir_est" type="text" value="<?php echo"".$_POST['dir_est'];?>" ></td>
    </tr>
    <tr>
      <th align="right">Telefono</th>
      <td align="left"><input name="tel_est" type="text" value="<?php echo"".$_POST['tel_est'];?>"></td>
    </tr>
    <tr>
      <th align="right">Nacionalidad</th>
      <td align="left"><input name="nac_est" type="text" value="<?php echo"".$_POST['tel_est'];?>"></td>
    </tr>
    <tr>
      <th align="right">Sexo:</th>
      <th align="left">F<input name="sexo" type="radio" value="Femenino">
                       M<input name="sexo" type="radio" value="Masculino">
      </th>
    </tr>
    <tr>
      <th align="right">Tipo de Sangre</th>
      <th align="left"><input name="tps" size="3" type="text" value="<?php echo"".$_POST['tps'];?>"></th>
    </tr>
    <tr>
      <th align="right">Estudiante:</th>
      <th align="left">Trasladado<input name="esta" type="radio" value="Trasladado">
      Continuo<input name="esta" type="radio" value="Continuo">Repitente<input name="esta" type="radio" value="Repitente"></th>
      </tr>
    </table></th>
    <th>
    <table>
    <tr>
      <th align="right">Edad</th>
      <td align="left"><input name="edad_est" size="3" type="text" value="<?php echo"".$_POST['edad_est'];?>" onKeyPress="return acceptNum(event)"></td>
    </tr>
    <tr>
      <th align="right">Fecha de Nacimiento(dd/mm/aaaa)</th>
      <td align="left"><input name="fec_nac" type="text" value="<?php echo"".$_POST['fec_nac'];?>" onblur="valFecha(this)"></td>
    </tr>
    <tr>
      <th align="right"># de Personas en casa</th>
      <td align="left"><input name="per_cas" size="3" type="text" value="<?php echo"".$_POST['per_cas'];?>" onKeyPress="return acceptNum(event)"></td>
    </tr>
    <tr>
      <th align="right">Estrato</th>
      <td align="left"><input name="est" type="text" size="3" value="<?php echo"".$_POST['est'];?>" onKeyPress="return acceptNum(event)"></td>
    </tr>
    <tr>
      <th align="right">Grupo Etnico</th>
      <th align="left"><select name="gru_et" >
     <option  value="<?php echo "".$_POST['gru_et']; ?>" selected="<?php echo "".$_POST['gru_et']; ?>"><?php echo "".$_POST['gru_et']; ?></option>
      <option value="Gitano">Gitano</option>
     <option value="Indigena">Indigena</option>
     <option value="AfroColombiano">AfroColombiano</option>
    </select></th>
    </tr>
    <tr>
      <th align="right">Grado:</th>
      <th align="left"><select name="grado" >
     <option  value="<?php echo "".$_POST['grado']; ?>" selected="<?php echo "".$_POST['grado']; ?>"><?php echo "".$_POST['grado']; ?></option>
      <?php
     $mat
=new ConsultasDAO();
     
$con=$mat->db->consulta("*",'grado',$cond,"");
     
$num=pg_num_rows $con );
     if (
$num>0) {
     while(@
$fila=pg_fetch_array($mat->db->consulta)){
               
?>
      <option value="<?php echo"".$fila["id_grado"];?>"><?php echo"".$fila["nom_grado"];?></option>
  <?php }} ?>
    </select></th>
    </tr>
    <tr>
      <th align="right">Vive con Papa:</th>
      <th align="left">Si<input name="viv_pap" type="radio" value="si">No<input name="viv_pap" type="radio" value="no"></th>
    </tr>
    <tr>
      <th align="right">Vive con Mama:</th>
      <th align="left">Si<input name="viv_mam" type="radio" value="si">No<input name="viv_mam" type="radio" value="no"></th>
    </tr>

  </table></th>
    </tr>
    <tr><th colspan="2"><hr></th></tr>
  </table>
  <BR>
  <table width="700" border="1" align="center">
    <caption align="top" class="Estilo5">
    <span class="Estilo4">INGRESO DE DATOS DE LOS PADRES</span>
    </caption>
    <tr>
      <th class="celda" align="center">DATOS DEL PADRE</th>
      <th class="celda" align="center">DATOS DE LA MADRE</th>
    </tr>
    <tr>
      <th align="center">
      Esta Vivo: <input name="ta_viv_pap" type="radio" value="si" Onclick="vertabla('tablap','');" >Si <input name="ta_viv_pap" type="radio" Onclick="vertabla('tablap','none');" value="no">No
         <table id="tablap" style="display:none">
    <tr>
      <th align="right">Identificacion</th>
      <td align="left"><input name="id_pap" type="text" value="<?php echo "".$_POST['id_pap']; ?>"  onKeyPress="return acceptNum(event)"></td>
    </tr>
    <tr>
      <th align="right">Nombres</th>
      <td align="left"><input name="nom_pap" size="40" type="text" value="<?php echo"".$_POST['nom_pap'];?>" onKeyUp="this.value=this.value.toUpperCase();" id="nom_adm"></td>
    </tr>
    <tr>
      <th align="right">Direccion</th>
      <td align="left"><input name="dir_pap" type="text" value="<?php echo"".$_POST['dir_pap'];?>" ></td>
    </tr>
    <tr>
      <th align="right">Telefono</th>
      <td align="left"><input name="tel_pap" type="text" value="<?php echo"".$_POST['tel_pap'];?>"></td>
    </tr>
    <tr>
      <th align="right">Profesion</th>
      <td align="left"><input name="prof_pap" type="text" value="<?php echo"".$_POST['prof_pap'];?>"></td>
    </tr>
    </tr></table></th>
      <th align="center">
      Esta Viva: <input name="ta_viv_ma" type="radio" value="si" Onclick="vertabla('tablam','');" >Si <input name="ta_viv_ma" type="radio" Onclick="vertabla('tablam','none');" value="no">No
         <table id="tablam" style="display:none">
    <tr>
      <th align="right">Identificacion</th>
      <td align="left"><input name="id_mam" type="text" value="<?php echo "".$_POST['id_mam']; ?>"  onKeyPress="return acceptNum(event)"></td>
    </tr>
    <tr>
      <th align="right">Nombres</th>
      <td align="left"><input name="nom_mam" size="40" type="text" value="<?php echo"".$_POST['nom_mam'];?>" onKeyUp="this.value=this.value.toUpperCase();" id="nom_adm"></td>
    </tr>
    <tr>
      <th align="right">Direccion</th>
      <td align="left"><input name="dir_mam" type="text" value="<?php echo"".$_POST['dir_mam'];?>" ></td>
    </tr>
    <tr>
      <th align="right">Telefono</th>
      <td align="left"><input name="tel_mam" type="text" value="<?php echo"".$_POST['tel_mam'];?>"></td>
    </tr>
    <tr>
      <th align="right">Profesion</th>
      <td align="left"><input name="prof_mam" type="text" value="<?php echo"".$_POST['prof_mam'];?>"></td>
    </tr>
    </table></th>
    </tr>
    </table>
  <table>
  <tr>
      <th colspan="8" scope="row">
      <input type="hidden" name="ing" value="no">
         <a class="enlace" href="#" onClick="valida_est('form1')" ><img src="../Imagenes/botong.png" width="112" height="38" border="0"></a>
        <a class="enlace" href="#" onClick="document.forms.form1.reset();return false"><img src="../Imagenes/cancelar.png" width="112" height="38" border="0"></a>
     </th>
    </tr>
      </table>
  <br>
  </div>
</form>
  #4 (permalink)  
Antiguo 16/06/2009, 10:30
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: problema con input radio

¿Qué tienes en la función enviarFormulario()?
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #5 (permalink)  
Antiguo 16/06/2009, 10:37
Avatar de lobo_php  
Fecha de Ingreso: noviembre-2007
Ubicación: Cali-Colombia
Mensajes: 1.869
Antigüedad: 16 años, 5 meses
Puntos: 75
Respuesta: problema con input radio

ummm tambien tengo un problema con eso aca... siempre me lee el primer campo... es decir
si tengo
1
0
-1

siempre me lee el 1
y si tengo
0
-1
1

siempre me lee el 0

vamos a ver que pasa
__________________
Cabuntu.org - Difundiendo Software Libre en Cali, Colombia
Usuario Linux # 483175
  #6 (permalink)  
Antiguo 16/06/2009, 11:17
 
Fecha de Ingreso: noviembre-2008
Mensajes: 80
Antigüedad: 15 años, 5 meses
Puntos: 1
Respuesta: problema con input radio

Cita:
Iniciado por David el Grande Ver Mensaje
¿Qué tienes en la función enviarFormulario()?
esta es la funcion enviarFormulario que se utiliza debido al a utilizacion de div
Código PHP:
function enviarFormulario(urlformid){
    var  
peticion false
if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
 
peticion = new XMLHttpRequest()
} else if (
window.ActiveXObject){ // pero si es IE
try {
 
peticion = new ActiveXObject("Msxml2.XMLHTTP")

catch (
e){ // en caso que sea una versión antigua
try{
 
peticion = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (
e){}
}
}
         var 
Formulario document.getElementById(formid);
         var 
cadenaFormulario "";
         var 
sepCampos;
         
sepCampos "";
         for (var 
i=0<= Formulario.elements.length-1;i++) {
         
cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
         
sepCampos="&";
}
  
peticion.open("POST"urltrue);
  
peticion.setRequestHeader('Content-Type''application/x-www-form-urlencoded; charset=ISO-8859-1');
  
peticion.onreadystatechange = function () {
  if (
peticion.readyState == && (peticion.status == 200 || window.location.href.indexOf ("http") == - 1)){
                
document.getElementById('contenedor').innerHTML peticion.responseText;
          } 
}
peticion.send(cadenaFormulario);

  #7 (permalink)  
Antiguo 16/06/2009, 11:33
Avatar de David
Moderador
 
Fecha de Ingreso: abril-2005
Ubicación: In this planet
Mensajes: 15.720
Antigüedad: 19 años
Puntos: 839
Respuesta: problema con input radio

Es que esa función no está contemplando la posibilidad de haber radios. Está enviando todos los datos de los elementos del formulario sin discriminar.

Puedes agregar una condición así antes de incluir el valor a los datos a enviar:
Código javascript:
Ver original
  1. if (elemento.type == "radio") {
  2.     if (elemento.checked) {
  3.         /* Enviar */
  4.     }
  5. }
__________________
Por favor, antes de preguntar, revisa la Guía para realizar preguntas.
  #8 (permalink)  
Antiguo 16/06/2009, 11:58
 
Fecha de Ingreso: noviembre-2008
Mensajes: 80
Antigüedad: 15 años, 5 meses
Puntos: 1
Respuesta: problema con input radio

Cita:
Iniciado por David el Grande Ver Mensaje
Es que esa función no está contemplando la posibilidad de haber radios. Está enviando todos los datos de los elementos del formulario sin discriminar.

Puedes agregar una condición así antes de incluir el valor a los datos a enviar:
Código javascript:
Ver original
  1. if (elemento.type == "radio") {
  2.     if (elemento.checked) {
  3.         /* Enviar */
  4.     }
  5. }
funciono de maravilla muchas gracias por tu ayuda
la funcion quedo asi
Código PHP:
function enviarFormulario(urlformid){
    var  
peticion false
if (window.XMLHttpRequest) {// Si es Mozilla, Safari etc
 
peticion = new XMLHttpRequest()
} else if (
window.ActiveXObject){ // pero si es IE
try {
 
peticion = new ActiveXObject("Msxml2.XMLHTTP")

catch (
e){ // en caso que sea una versión antigua
try{
 
peticion = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (
e){}
}
}
         var 
Formulario document.getElementById(formid);
         var 
cadenaFormulario "";
         var 
sepCampos;
         
sepCampos "";
         for (var 
i=0<= Formulario.elements.length-1;i++) {
         if(
Formulario.elements[i].type=='radio'){
          if(
Formulario.elements[i].checked){
         
cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
         
sepCampos="&";}
         }
         else{
          
cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
         
sepCampos="&";
         }
}
  
peticion.open("POST"urltrue);
  
peticion.setRequestHeader('Content-Type''application/x-www-form-urlencoded; charset=ISO-8859-1');
  
peticion.onreadystatechange = function () {
  if (
peticion.readyState == && (peticion.status == 200 || window.location.href.indexOf ("http") == - 1)){
                
document.getElementById('contenedor').innerHTML peticion.responseText;
          } 
}
peticion.send(cadenaFormulario);

por si al alguien le sirve
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:31.