form validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arty
    New Member
    • Nov 2008
    • 35

    form validation

    So basically i have a form with a action attribute set to another form, it is a survey spread among several pages and using php sessions, now im trying to validate the form with Javascript but it looks imposible because the check boxes all have the same name Q1[](and the array symbol is conflicting with js), the same name is needed for processing the answers with php in the database, i am able to make a form validation of the form below using PHP(if nothing is selected the form wont go to form Q2.php, if more than 1 checkboxe is selected same thing will happen) but can't do it using echo ,like echoing "you must select at least one checkbox" so i try with JS but neither with succes.

    Q1.php :
    Code:
    <head>
      <title></title>
      <script>
      function validation(){
       if (document.form.Q1.checked == false/* &&
    	    document.form.CHKBOX_2.checked == false &&
    	    document.form.CHKBOX_3.checked == false*/)
    		{
    		alert ('You didn\'t choose any of the checkboxes!');
    		return false;
    		}
    	else
    		{
    		return true;
    		}
       }
      </script>
    
    </head>
    
    <body>
    <form method="post" name="form"  action="Q2.php" onsubmit="javascript:validation()">
    <b>Quel est votre profession ?</b><br />
    <input type="checkbox"  name="Q1[]" value="Artisans, commerçants et chefs d'entreprise"> Artisans, commerçants et chefs d'entreprise<br />
    <input type="checkbox" name="Q1[]" value="Cadres et professions intellectuelles supérieures"> Cadres et professions intellectuelles supérieures<br />
    <input type="checkbox" name="Q1[]" value="Agriculteur exploitants"> Agriculteur exploitants<br />
    <input type="checkbox" name="Q1[]" value="Professions intermédiaires"> Professions intermédiaires<br />
    <input type="checkbox" name="Q1[]" value="Employés"> Employés<br />
    <input type="checkbox" name="Q1[]" value="Ouvriers"> Ouvriers<br />
    <input type="checkbox" name="Q1[]" value="Autres personnes sans activités professionnelle et retraitée"> Autres personnes sans activités professionnelle et retraitée<br />
    <input type="submit" name="submit_Q1" value="Allez a la Q2">
    </form>

    Q2.php :

    Code:
    <?php
    session_start();
    session_register('Q1');
    $_SESSION['Q1'] = $_POST['Q1'];
    
     $N = count($_POST['Q1']);
    if(isset($_POST["submit_Q1"]) and $N ==0){
      header('Location:Q1.php');
    }
    if(isset($_POST["submit_Q1"]) and $N >1){
    header('Location:Q1.php');
    }
    
    /*echo $_SESSION['Q1'][0];*/
    
    
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title></title>
    </head>
    <body>
    <?php
    
    ?>
    <form action="Q3.php" method="post">
    <b>Et votre âge s'il vous plait ?</b> <input type="text" name="Q2"><br />
    Merci beaucoup. Si vous le permettez ...<br />
    <input type="submit" name="submit_Q2" value="Allez a la Q3">
    </form>
    </body>
    </html>
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    See getElementsByTa gName().

    Also, please in future make the distinction between a PHP problem and a Javascript one.

    Mark (moving thread to JS)

    Comment

    • arty
      New Member
      • Nov 2008
      • 35

      #3
      yes i thought something like getElementsByTa gName could make it , but how can i use the checked property with getElementsByTa gName ?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Loop over the elements returned and then check each one:
        Code:
        if (elems[0].checked) ...
        where elems is the collection/array returned.

        Comment

        Working...