473,386 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

form validation

35
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 :
Expand|Select|Wrap|Line Numbers
  1. <head>
  2.   <title></title>
  3.   <script>
  4.   function validation(){
  5.    if (document.form.Q1.checked == false/* &&
  6.         document.form.CHKBOX_2.checked == false &&
  7.         document.form.CHKBOX_3.checked == false*/)
  8.         {
  9.         alert ('You didn\'t choose any of the checkboxes!');
  10.         return false;
  11.         }
  12.     else
  13.         {
  14.         return true;
  15.         }
  16.    }
  17.   </script>
  18.  
  19. </head>
  20.  
  21. <body>
  22. <form method="post" name="form"  action="Q2.php" onsubmit="javascript:validation()">
  23. <b>Quel est votre profession ?</b><br />
  24. <input type="checkbox"  name="Q1[]" value="Artisans, commerçants et chefs d'entreprise"> Artisans, commerçants et chefs d'entreprise<br />
  25. <input type="checkbox" name="Q1[]" value="Cadres et professions intellectuelles supérieures"> Cadres et professions intellectuelles supérieures<br />
  26. <input type="checkbox" name="Q1[]" value="Agriculteur exploitants"> Agriculteur exploitants<br />
  27. <input type="checkbox" name="Q1[]" value="Professions intermédiaires"> Professions intermédiaires<br />
  28. <input type="checkbox" name="Q1[]" value="Employés"> Employés<br />
  29. <input type="checkbox" name="Q1[]" value="Ouvriers"> Ouvriers<br />
  30. <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 />
  31. <input type="submit" name="submit_Q1" value="Allez a la Q2">
  32. </form>
  33.  

Q2.php :

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. session_register('Q1');
  4. $_SESSION['Q1'] = $_POST['Q1'];
  5.  
  6.  $N = count($_POST['Q1']);
  7. if(isset($_POST["submit_Q1"]) and $N ==0){
  8.   header('Location:Q1.php');
  9. }
  10. if(isset($_POST["submit_Q1"]) and $N >1){
  11. header('Location:Q1.php');
  12. }
  13.  
  14. /*echo $_SESSION['Q1'][0];*/
  15.  
  16.  
  17.  
  18. ?>
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  20.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  21. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  22. <head>
  23. <title></title>
  24. </head>
  25. <body>
  26. <?php
  27.  
  28. ?>
  29. <form action="Q3.php" method="post">
  30. <b>Et votre âge s'il vous plait ?</b> <input type="text" name="Q2"><br />
  31. Merci beaucoup. Si vous le permettez ...<br />
  32. <input type="submit" name="submit_Q2" value="Allez a la Q3">
  33. </form>
  34. </body>
  35. </html>
Nov 18 '09 #1
3 1813
Markus
6,050 Expert 4TB
See getElementsByTagName().

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

Mark (moving thread to JS)
Nov 19 '09 #2
arty
35
yes i thought something like getElementsByTagName could make it , but how can i use the checked property with getElementsByTagName ?
Nov 23 '09 #3
acoder
16,027 Expert Mod 8TB
Loop over the elements returned and then check each one:
Expand|Select|Wrap|Line Numbers
  1. if (elems[0].checked) ...
where elems is the collection/array returned.
Nov 30 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now...
4
by: TG | last post by:
I have a validation form that must behave differently based on the results of a PHP validation check. I have a post command at the top of my form that calls itself. I don't leave the form when...
4
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
16
by: Hosh | last post by:
I have a form on a webpage and want to use JavaScript validation for the form fields. I have searched the web for form validation scripts and have come up with scripts that only validate...
9
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
10
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
5
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.