#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    223
    Rep Power
    0

    Smile Check if radio buttons are checked


    Hello. I am making this brief survey with radio buttons using php, and mysql. Its allready working but now i have the issue that if i click the submit button and did not checked all radio buttons the results are sent anyway. Which i dont want to happen. I want the results to be sent to the mysql database ONLY if every single radio button is checked.

    http://capricciosas.espritwebdesign..../encuesta1.php

    So I guees i need some javascript code to check if all the radio buttons are selected. for example if there is one button on the survey wich is not selected by the user, and they click submit then i want a display that states: please answer all questions. I know its easy but i dont know any javascript. I dont want to specify which radio button has been left un-answered i just want javascritp to block the submit post action and tell the user that one radio button has been left blank..

    This is my code for the form:

    Code:
     
    
     <form method="post">
    <table border="0" bgcolor="#ffffff" width="450px;">
    
    <td></td>
    <td align="center">H</td>
    <td align="center">M</td>
    
    
    </tr>
    <tr><td>Sexo</td>
      <td align="center"><input type="radio" name="sexo"  value="Hombre"/></td>
      <td align="center"><input type="radio" name="sexo" value="Mujer"/></td>
    </tr>
    <td></td>
      <td align="center">15-24</td>
      <td align="center">25-34</td>
      <td align="center">35-54</td>
      <td align="center">54+</td>
      </tr>
    
    
    <tr><td>Edad</td>
      <td align="center"><input type="radio" name="edad" value="15-24"/></td>
      <td align="center"><input type="radio" name="edad" value="25-34"/></td>
      <td align="center"><input type="radio" name="edad" value="35-54"/></td>
      <td align="center"><input type="radio" name="edad" value="54+"/></td>
    </tr>
    
    <tr>
      <td colspan="5"><h3>¿Cómo evalúas nuestros platillos?</h3></td>
    </tr>
    
    <tr>
    <td></td>
    <td align="center">Muy Bueno</td>
    <td align="center">Bueno</td>
    <td align="center">Regular</td>
    <td align="center">Malo</td>
    </tr>
    
    
    <tr align="top"><td>Sabor</td>
      <td align="center"><input type="radio" name="sabor" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="sabor" value="Bueno"/></td>
      <td align="center"><input type="radio" name="sabor" value="Regular"/></td>
      <td align="center"><input type="radio" name="sabor" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Calidad</td>
      <td align="center"><input type="radio" name="calidad" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="calidad" value="Bueno"/></td>
      <td align="center"><input type="radio" name="calidad" value="Regular"/></td>
      <td align="center"><input type="radio" name="calidad" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Ingredientes</td>
      <td align="center"><input type="radio" name="ingredientes" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="ingredientes" value="Bueno"/></td>
      <td align="center"><input type="radio" name="ingredientes" value="Regular"/></td>
      <td align="center"><input type="radio" name="ingredientes" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Apariencia</td>
      <td align="center"><input type="radio" name="apariencia" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="apariencia" value="Bueno"/></td>
      <td align="center"><input type="radio" name="apariencia" value="Regular"/></td>
      <td align="center"><input type="radio" name="apariencia" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Temperatura</td>
      <td align="center"><input type="radio" name="temperatura" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="temperatura" value="Bueno"/></td>
      <td align="center"><input type="radio" name="temperatura" value="Regular"/></td>
      <td align="center"><input type="radio" name="temperatura" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Variedad</td>
      <td align="center"><input type="radio" name="variedad" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="variedad" value="Bueno"/></td>
      <td align="center"><input type="radio" name="variedad" value="Regular"/></td>
      <td align="center"><input type="radio" name="variedad" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Precio</td>
      <td align="center"><input type="radio" name="precio" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="precio" value="Bueno"/></td>
      <td align="center"><input type="radio" name="precio" value="Regular"/></td>
      <td align="center"><input type="radio" name="precio" value="Malo"/></td>
    </tr>
    
    
    <tr><td><input type="submit" name="submit" value="Submit"/></td></tr>
    </table>
    </form>
    Thanks in advanced
    Last edited by Escolta; October 15th, 2012 at 10:56 PM.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    Usted voluntad tener traducir con espanol; porque Mi espanol es no muy bueno. Tienes que amar el Traductor de Google ... lol!

    Code:
    <script>
    
    var error1, error2, error3, error4, error5, error6, error7, error8, error9;
    
    function radioValidate()
    {
    var radio1 = document.getElementsByName("sexo")[0];
    var radio2 = document.getElementsByName("sexo")[1];
    var radio3 = document.getElementsByName("edad")[0];
    var radio4 = document.getElementsByName("edad")[1];
    var radio5 = document.getElementsByName("edad")[2];
    var radio6 = document.getElementsByName("edad")[3];
    var radio7 = document.getElementsByName("sabor")[0];
    var radio8 = document.getElementsByName("sabor")[1];
    var radio9 = document.getElementsByName("sabor")[2];
    var radio10 = document.getElementsByName("sabor")[3];
    var radio11 = document.getElementsByName("calidad")[0];
    var radio12 = document.getElementsByName("calidad")[1];
    var radio13 = document.getElementsByName("calidad")[2];
    var radio14 = document.getElementsByName("calidad")[3];
    var radio15 = document.getElementsByName("ingredientes")[0];
    var radio16 = document.getElementsByName("ingredientes")[1];
    var radio17 = document.getElementsByName("ingredientes")[2];
    var radio18 = document.getElementsByName("ingredientes")[3];
    var radio19 = document.getElementsByName("apariencia")[0];
    var radio20 = document.getElementsByName("apariencia")[1];
    var radio21 = document.getElementsByName("apariencia")[2];
    var radio22 = document.getElementsByName("apariencia")[3];
    var radio23 = document.getElementsByName("temperatura")[0];
    var radio24 = document.getElementsByName("temperatura")[1];
    var radio25 = document.getElementsByName("temperatura")[2];
    var radio26 = document.getElementsByName("temperatura")[3];
    var radio27 = document.getElementsByName("variedad")[0];
    var radio28 = document.getElementsByName("variedad")[1];
    var radio29 = document.getElementsByName("variedad")[2];
    var radio30 = document.getElementsByName("variedad")[3];
    var radio31 = document.getElementsByName("precio")[0];
    var radio32 = document.getElementsByName("precio")[1];
    var radio33 = document.getElementsByName("precio")[2];
    var radio34 = document.getElementsByName("precio")[3];
    
    var error="no";
    
    // Validate Sex
    
    if (radio1.checked != true && radio2.checked != true) {
    error1 = "Please Select Your Sex\n";
    error="yes";
    }
    else if (radio1.checked == true || radio2.checked == true) {
    error1 = "";
    }
    
    // Validate Age
    
    if (radio3.checked != true && radio4.checked != true && radio5.checked != true && radio6.checked != true) {
    error2 = "Please Select Your Age Range\n";
    error="yes";
    }
    else if (radio3.checked == true || radio4.checked == true || radio5.checked == true || radio6.checked == true) {
    error2 = "";
    }
    
    // Validate Flavor
    
    if (radio7.checked != true && radio8.checked != true && radio9.checked != true && radio10.checked != true) {
    error3 = "Please Rate The Flavor of Your Dish\n";
    error="yes";
    }
    else if (radio7.checked == true || radio8.checked == true || radio9.checked == true || radio10.checked == true) {
    error3 = "";
    }
    
    // Validate Quality
    
    if (radio11.checked != true && radio12.checked != true && radio13.checked != true && radio14.checked != true) {
    error4 = "Please Rate The Quality of Your Dish\n";
    error="yes";
    }
    
    else if (radio11.checked == true || radio12.checked == true || radio13.checked == true || radio14.checked == true) {
    error4 = "";
    }
    
    // Validate Ingredients
    
    if (radio15.checked != true && radio16.checked != true && radio17.checked != true && radio18.checked != true) {
    error5 = "Please Rate The Ingredients of Your Dish\n";
    error="yes";
    }
    else if (radio15.checked == true || radio16.checked == true || radio17.checked == true || radio18.checked == true) {
    error5 = "";
    }
    
    // Validate Appearance
    
    if (radio19.checked != true && radio20.checked != true && radio21.checked != true && radio22.checked != true) {
    error6 = "Please Rate The Appearance of Your Dish\n";
    error="yes";
    }
    else if (radio19.checked == true || radio20.checked == true || radio21.checked == true || radio22.checked == true) {
    error6 = "";
    }
    
    // Validate Temperature
    
    if (radio23.checked != true && radio24.checked != true && radio25.checked != true && radio26.checked != true) {
    error7 = "Please Rate The Temperature of Your Dish\n";
    error="yes";
    }
    else if (radio23.checked == true || radio24.checked == true || radio25.checked == true || radio26.checked == true) {
    error7 = "";
    }
    
    // Validate Variety
    
    if (radio27.checked != true && radio28.checked != true && radio29.checked != true && radio30.checked != true) {
    error8 = "Please Rate The Variety of Your Dish\n";
    error="yes";
    }
    else if (radio27.checked == true || radio28.checked == true || radio29.checked == true || radio30.checked == true) {
    error8 = "";
    }
    
    // Validate Price
    
    if (radio31.checked != true && radio32.checked != true && radio33.checked != true && radio34.checked != true) {
    error9 = "Please Rate The Price of Your Dish\n";
    error="yes";
    }
    else if (radio31.checked == true || radio32.checked == true || radio33.checked == true || radio34.checked == true) {
    error9 = "";
    }
    
    // Prepare To Send Form Data
    
    if (error == "yes") {
    alert(error1 + error2 + error3 + error4 + error5 + error6 + error7 + error8 + error9);
    }
    else {
    document.getElementById("survey").onsubmit = function() {
    return true;
    }
    }
    
    }
    </script>
    
    <form id="survey" method="post" onsubmit="return false">
    <table border="0" bgcolor="#ffffff" width="450px;">
    
    <td></td>
    <td align="center">H</td>
    <td align="center">M</td>
    
    
    </tr>
    <tr><td>Sexo</td>
      <td align="center"><input type="radio" name="sexo"  value="Hombre"/></td>
      <td align="center"><input type="radio" name="sexo" value="Mujer"/></td>
    </tr>
    <td></td>
      <td align="center">15-24</td>
      <td align="center">25-34</td>
      <td align="center">35-54</td>
      <td align="center">54+</td>
      </tr>
    
    
    <tr><td>Edad</td>
      <td align="center"><input type="radio" name="edad" value="15-24"/></td>
      <td align="center"><input type="radio" name="edad" value="25-34"/></td>
      <td align="center"><input type="radio" name="edad" value="35-54"/></td>
      <td align="center"><input type="radio" name="edad" value="54+"/></td>
    </tr>
    
    <tr>
      <td colspan="5"><h3>¿Cómo evalúas nuestros platillos?</h3></td>
    </tr>
    
    <tr>
    <td></td>
    <td align="center">Muy Bueno</td>
    <td align="center">Bueno</td>
    <td align="center">Regular</td>
    <td align="center">Malo</td>
    </tr>
    
    
    <tr align="top"><td>Sabor</td>
      <td align="center"><input type="radio" name="sabor" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="sabor" value="Bueno"/></td>
      <td align="center"><input type="radio" name="sabor" value="Regular"/></td>
      <td align="center"><input type="radio" name="sabor" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Calidad</td>
      <td align="center"><input type="radio" name="calidad" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="calidad" value="Bueno"/></td>
      <td align="center"><input type="radio" name="calidad" value="Regular"/></td>
      <td align="center"><input type="radio" name="calidad" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Ingredientes</td>
      <td align="center"><input type="radio" name="ingredientes" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="ingredientes" value="Bueno"/></td>
      <td align="center"><input type="radio" name="ingredientes" value="Regular"/></td>
      <td align="center"><input type="radio" name="ingredientes" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Apariencia</td>
      <td align="center"><input type="radio" name="apariencia" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="apariencia" value="Bueno"/></td>
      <td align="center"><input type="radio" name="apariencia" value="Regular"/></td>
      <td align="center"><input type="radio" name="apariencia" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Temperatura</td>
      <td align="center"><input type="radio" name="temperatura" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="temperatura" value="Bueno"/></td>
      <td align="center"><input type="radio" name="temperatura" value="Regular"/></td>
      <td align="center"><input type="radio" name="temperatura" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Variedad</td>
      <td align="center"><input type="radio" name="variedad" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="variedad" value="Bueno"/></td>
      <td align="center"><input type="radio" name="variedad" value="Regular"/></td>
      <td align="center"><input type="radio" name="variedad" value="Malo"/></td>
    </tr>
    </tr>
    
    
    <tr align="top"><td>Precio</td>
      <td align="center"><input type="radio" name="precio" value="Muy Bueno"/></td>
      <td align="center"><input type="radio" name="precio" value="Bueno"/></td>
      <td align="center"><input type="radio" name="precio" value="Regular"/></td>
      <td align="center"><input type="radio" name="precio" value="Malo"/></td>
    </tr>
    
    
    <tr>
    <td>
    <input type="submit" name="submit" value="Submit" onclick="radioValidate()"/>
    <input type="reset"/>
    </td>
    </tr>
    </table>
    
    </form>

IMN logo majestic logo threadwatch logo seochat tools logo