validation of Multiple forms in single page

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jul 2009
Posts: 12
Reputation: goldy736 is an unknown quantity at this point 
Solved Threads: 0
goldy736 goldy736 is offline Offline
Newbie Poster

validation of Multiple forms in single page

 
0
  #1
Jul 9th, 2009
hi guys,

i have a php page with java script embed in it , i have 3 forms in a single page ... namely form1, form2, form3 .
form 1 and form2 have a text box , these text boxes get input from users. Now my task is to get the value of these 2 tex boxes in form3 when the user click the submit button , so that i pass the values in those text boxes to next page for calculation.

i tried the following code in form3 but it result in vain :
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type='text/javascript' language='JavaScript'>
  2. var resp_1= document.q1.response1.value;
  3. document.counter.response1_user.value= resp_1;
  4. </script>

q1 = name of form1 , response1 = text box in form1
counter = name of form3 , response1_user = text box in form3.

please help me out ....

Thank you !
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 49
Reputation: sysel is an unknown quantity at this point 
Solved Threads: 3
sysel sysel is offline Offline
Light Poster

Re: validation of Multiple forms in single page

 
0
  #2
Jul 9th, 2009
Please take in account that you can simply submit one form at once. The simple way is to gather all those text inputs into one form and the submit sends all the requested data.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 12
Reputation: goldy736 is an unknown quantity at this point 
Solved Threads: 0
goldy736 goldy736 is offline Offline
Newbie Poster

Re: validation of Multiple forms in single page

 
0
  #3
Jul 10th, 2009
hi,
thanks for your reply , i cant put in single form because am performing a quiz pages where 2 questions were to be displayed in single page , so i used Q1 in form1 and Q2 in form2.. with each having a reset button , if i put them together in single form values may get passed but, click on any 1 reset clear all data in both Questions...so i use 3 forms ...where in 3rd form i get all the values and then i pass it to next page ..

Thank you !
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 49
Reputation: sysel is an unknown quantity at this point 
Solved Threads: 3
sysel sysel is offline Offline
Light Poster

Re: validation of Multiple forms in single page

 
0
  #4
Jul 10th, 2009
Originally Posted by goldy736 View Post
hi,
thanks for your reply , i cant put in single form because am performing a quiz pages where 2 questions were to be displayed in single page , so i used Q1 in form1 and Q2 in form2.. with each having a reset button , if i put them together in single form values may get passed but, click on any 1 reset clear all data in both Questions...so i use 3 forms ...where in 3rd form i get all the values and then i pass it to next page ..

Thank you !
Well, close each separated question in its own <div id="Q1"> Q2 etc. You can reset questions separately and submit together :-)
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 884
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 126
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: validation of Multiple forms in single page

 
0
  #5
Jul 10th, 2009
Goldy736,

The requirement for independent resets is indeed good reason to have separate form1 and form 2.

There is no particular need for a third form as you could compoite reponse_1 and reponse_2 into a field within say form 2 then submit it. However, a third form is also an entirely reasonable approach.

You are nearly there with your code. It just needs a bit more and to be fired in response to form submission rather than while the page loads as will occur unless it is wrapped in a function.

Try this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>Airshow :: Untitled</title>
  6. <script>
  7. function getUserResponses(){
  8. var resp_1 = document.forms['q1'].response1.value;
  9. var resp_2 = document.forms['q2'].response2.value;
  10. document.forms['counter'].response1_user.value= resp_1;
  11. document.forms['counter'].response2_user.value= resp_2;
  12. return false;
  13. }
  14. </script>
  15. </head>
  16.  
  17. <body>
  18.  
  19. <form id="q1" name="q1">
  20. What colour are your eyes?&nbsp;<input name="response1">
  21. <input type="reset" value="Reset">
  22. </form>
  23.  
  24. <form id="q2" name="q2">
  25. What colour are your feet?&nbsp;<input name="response2">
  26. <input type="reset" value="Reset">
  27. </form>
  28.  
  29. <form action="whatever.php" method="get" id="counter" name="counter" onsubmit="return getUserResponses()">
  30. <input Xtype="hidden" name="response1_user">
  31. <input Xtype="hidden" name="response2_user">
  32. <input type="submit" value="Submit">
  33. </form>
  34.  
  35. </body>
  36. </html>
I have left this code in a state where you can see what's going on.

To make it operational:
  • In getUserResponses() , change return false to return true .
  • Change form 3's action to the name of the (php) file that is to receive the user data.
  • Change form 3's method to "post" (optional, depending on what the php file is set up to handle)
  • Change the two attributes Xtype="hidden" to type="hidden" .
Airshow
Last edited by Airshow; Jul 10th, 2009 at 10:13 pm.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: validation of Multiple forms in single page

 
0
  #6
Jul 11th, 2009
Since we have it all here, then here's another demo that allows you to tranfer user input into selected form.

Here's the document sample :

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head profile="http://www.w3.org/2005/10/profile">
  6. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  7. <meta http-equiv="Window-Target" content="_top" />
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. <meta http-equiv="Content-Style-Type" content="text/css" />
  10. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  11. <title>Free Live Help!</title>
  12. <style type="text/css">
  13. /* <![CDATA[ */
  14. .hide { display : none; }
  15. .show { display : block; }
  16. /* ]]> */
  17. </style>
  18. <script type="text/javascript" src="./form.js"></script>
  19. <script type="text/javascript">
  20. // <![CDATA[
  21. var fLen;
  22. var validate = function() {
  23. fLen = 3; // Where 3 is the number of forms in the page.
  24. $((( fLen ) - 1 )).onsubmit = function() { // Initiating onsubmit handler with form3.
  25. for ( var i = 0; i < 3; i++ ) {
  26. /* Looping thru the entire form field( 1-3 ), to check whether if the user provided sufficient data on this fields. */
  27. if ( $( i, 0 ).value ) { // Simulating the user input into form3>>field#2>>field#3
  28. $( 2, i ).value = $((( i ) - 1 ), 0 ).value;
  29. continue
  30. }
  31. } if ( !$( 2, 0 ).value || !$( 2, 1 ).value || !$( 2, 2 ).value ) { // If any of the fields inside form3 is left empty, then exit function. Otherwise return true;
  32. return false;
  33. } alert( "All fields has been verified.", "Thank you!" );
  34. };
  35. }; window.onload = validate;
  36. // ]]>
  37. </script>
  38. </head>
  39. <body>
  40. <div id="main">
  41. <form id="form0" action="#" method="post" onsubmit="return false;">
  42. <div><label for="txt0">Form1 Field #1 : <input type="text" id="txt0" name="txt0" value="" size="30" /></label></div>
  43. </form>
  44. <form id="form1" action="#" method="post" onsubmit="return false;">
  45. <div><label for="txt1">Form2 Field #2 : <input type="text" id="txt1" name="txt1" value="" size="30" /></label></div>
  46. </form>
  47. <form id="form2" action="#" method="post">
  48. <div><label for="txt4">Form3 Field #3 : <input type="text" id="txt4" name="txt4" value="" size="30" /></label></div>
  49. <div style="width : 35%; background-color : #eee; color : #405060; padding : 1em; border : 1px solid #ccc; margin : .500em 0 .500em 0;">
  50. <div><label for="txt2">Get Field #1 : <input type="text" id="txt2" name="txt2" value="" size="30" /></label></div>
  51.  
  52. <div><label for="txt3">Get Field #2 : <input type="text" id="txt3" name="txt3" value="" size="30" /></label></div>
  53. <div style="margin-top: .500em;"><h3>Values from:</h3>
  54. <ol><li><b>form1»field#1</b></li>
  55. <li><b>form2»field#2</b></li></ol> will be passed onto the fields above.</div>
  56. </div>
  57. <div><input type="submit" value="- submit form -" /></div>
  58. </form>
  59. </div>
  60. </body>
  61. </html>

And here's the form.js , that offers easy access, to forms including its elements, using a few syntax of : $(FormId/RefNumber, ElementId/RefNumber).properties ( REUSABLE FUNCTION ).

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. /*******************************
  2.  - Simplified Form Access Script v7.9
  3. ********************************
  4. - This notice must remain intact for use :
  5. ********************************
  6. ~ Developed By DaniWeb USER : essential
  7.  
  8. ~ http://www.daniweb.com/forums/member383844.html
  9. *******************************/
  10. var $form;
  11. var $element;
  12. var modern = Boolean( document.getElementById );
  13. var ie = Boolean( document.all && !modern );
  14. var node = Boolean( document.getElementsByTagName );
  15. var $ = function( FORMID, FORMELEMENT ) {
  16. if ( typeof FORMID !== "undefined" ) {
  17. var classes = typeof( FORMID );
  18. try {
  19. switch( classes ) {
  20. case "object" :
  21. try {
  22. $form = FORMID;
  23. $element = eval( "$form." + FORMELEMENT );
  24. } catch( e0 ) {
  25. $form = document.forms[ FORMID.id ];
  26. $element = $form.elements[ FORMELEMENT ];
  27. } break;
  28. case "string" :
  29. if ( document.forms ) {
  30. $form = document.forms[ FORMID ];
  31. $element = $form.elements[ FORMELEMENT ];
  32. } else {
  33. $form = (( modern ) ? document.getElementById( FORMID ) : (( ie ) ? document.all[ FORMID ] : (( document.layers ) ? document.layers[ FORMID ] : undefined )));
  34. $element = $form.children[ FORMELEMENT ];
  35. } break;
  36. case "number" :
  37. $form = (( node ) ? document.getElementsByTagName("form")[ FORMID ] : (( ie ) ? document.all.tags("form").item( FORMID ) : undefined ));
  38. $element = (( node ) ? $form.getElementsByTagName("input")[ FORMELEMENT ] : (( ie ) ? $form.all.tags("input").item( FORMELEMENT ) : undefined ));
  39. break;
  40. default : $form = undefined;
  41. break;
  42. }
  43. } catch( error ) {
  44. $form = undefined;
  45. } if ( typeof FORMELEMENT === "undefined" ) {
  46. return $form;
  47. } return $element;
  48. }
  49. };
Hope we're all getting your needs...

essential
Last edited by essential; Jul 11th, 2009 at 2:57 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC