form validation

Thread Solved

Join Date: Jun 2005
Posts: 21
Reputation: planethax is an unknown quantity at this point 
Solved Threads: 0
planethax planethax is offline Offline
Newbie Poster

form validation

 
-1
  #1
Sep 21st, 2009
OK, I am making a Form that will have about 65 check boxes, this data will be pulled from database.
I need to validate form to be sure that 7 (no more no less) are checked.
Can some one post a quick code snippet of the code validation for this?

I have searched and have found some greater than examples but to specific number.

Thanx.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,078
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 137
ardav's Avatar
ardav ardav is offline Offline
Veteran Poster

Re: form validation

 
0
  #2
Sep 21st, 2009
I would suggest that you have both client-side and server-side validation. The client-side should prevent users from choosing = 7 options or notify the user that they have chosen = 7 options and refused to submit the form.

A simple version would be:
1) The html should set the state of each checkbox in the array and the initial count set up as a js variable. If total <> 7 , disable the submit button.
2) A click will ascribe a +1 or -1 to the total (depending if the chkbox has been checked or unchecked). Check total, disable or enable submit button.

The server should valso validate, e.g.

  1. $count = count($_POST['chk_option']);

If this is <> 7, refuse the data and send the user back to the form with an error message. Remember to store the info ($_SESSION/$_COOKIE/querystring and $_GET) from the $_POST variables to replenish the form fields.
Last edited by ardav; Sep 21st, 2009 at 12:59 pm.
Happy Humbugging Christmas
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: form validation

 
1
  #3
Sep 21st, 2009
Here's the idea, though i have not tested the code, the logic is correct.

  1. <?php
  2.  
  3. $sql = "SELECT * FROM options LIMIT 65";
  4. $rs = mysql_query($sql);
  5.  
  6. echo "<form name='frmChk' action='mypage.php' method='post'>";
  7. while($row = mysql_fetch_assoc($rs)){
  8. $label = $row['option_name'];
  9. echo "<input type='checkbox' name='chks[]'> $label <br>";
  10. }
  11. echo "<input type='submit' value='Validate & Submit' name='validate' onclick='if(!checkCount()) return false;'></form>";
  12.  
  13. echo
  14. "<script language='javascript'>
  15. function checkCount(){
  16. var count=0;
  17. var inputs=document.frmChk.getElementsByTagName('input');
  18. for(var i=0; i<inputs.length-1; i++){
  19. if(inputs[i].type == 'checkbox' && inputs[i].checked == true)
  20. count++;
  21. }
  22. if(count != 7){
  23. alert('You must select exactly 7 checkboxes');
  24. return false;
  25. }
  26. return true;
  27. }
  28. </script>";
  29. ?>
Last edited by wilch; Sep 21st, 2009 at 1:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 21
Reputation: planethax is an unknown quantity at this point 
Solved Threads: 0
planethax planethax is offline Offline
Newbie Poster

Re: form validation

 
0
  #4
Sep 22nd, 2009
Thanx very much, I will work with this so far.

I am very much in over my head lol, but I could not find what I was lookin for, so will have to make my own haha.

Think after a few pages I will be ok though.

Thanx again, and I am sure I will be back many times yet.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: form validation

 
1
  #5
Sep 22nd, 2009
No problem !
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 21
Reputation: planethax is an unknown quantity at this point 
Solved Threads: 0
planethax planethax is offline Offline
Newbie Poster

Re: form validation

 
0
  #6
Oct 1st, 2009
Well 8 days later lol, finally got the first step done.
What I am finding the mosst difficult is integrating into my current site which uses .php & .tpl

Ok, so now my page goes to db, gets the drivers and validates that user has picked 7 exactly,
Now how do I do next step?
I now need to enter the user (also give user a unique ID) and their seven drivers into the db in a table called userdriver10

Here is my page so far

  1. <?php
  2.  
  3. $tpl = new template;
  4. $tpl -> Load("!theme/{$GLOBALS["THEME"]}/templates/fantasy/fselteam.tpl");
  5. $tpl -> GetObjects();
  6.  
  7.  
  8. if (isset($_SESSION["id"])) $tpl->Zone("userStatus", "user");
  9.  
  10. else $tpl->Zone("userStatus", "guest");
  11.  
  12. if (isset($GLOBALS["LOGIN_FAIL_TYPE"])) {
  13. if ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.password") $loginError = $GLOBALS["OBJ"]["loginError.password"];
  14. elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.user") $loginError = $GLOBALS["OBJ"]["loginError.username"];
  15. elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.bruteforce") $loginError = $GLOBALS["OBJ"]["loginError.bruteforce"];
  16. elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.active") $loginError = $GLOBALS["OBJ"]["loginError.active"];
  17. }
  18.  
  19. $tpl -> AssignArray(array(
  20. "login.failMessage" => (isset($loginError)?$loginError:NULL)
  21. ));
  22. // Report all PHP errors (see changelog)
  23. // error_reporting(E_ALL);
  24. include_once('db_conn.php');
  25. $sql = "SELECT * FROM `nascar_standings` ORDER BY driver ASC LIMIT 65";
  26. $rs = mysql_query($sql);
  27. $user = "frank";
  28. $i = 0;
  29. $sel_text = "";
  30. $column = "</tr><tr>";
  31. while($row = mysql_fetch_assoc($rs)){
  32. $label = $row['driver'];
  33. $sel_text .= "<td><input type='checkbox' name='chks[]'> $label </td>";
  34. if (++$i % 4 == 0)
  35. {
  36. $sel_text .= $column;
  37. }
  38. }
  39. $sel_text .= "</tr><tr><td colspan =4 valign = center><input type='submit' value='Validate & Submit' name='validate' onclick='if(!checkCount()) return false;'></td></tr></table></form>";
  40.  
  41. $sel_text .= "<script type='text/javascript'>
  42.  
  43. function checkCount(){
  44. var mincheck = 7;
  45. var testform = document.getElementById('frmChk');
  46. var items = testform.getElementsByTagName('input');
  47. var count = 0;
  48.  
  49. for (var i=0; i < items.length-1; i++)
  50. {
  51. if (items[i].type == 'checkbox' && items[i].checked)
  52. {
  53. count++;
  54. }
  55. }
  56.  
  57. if(count != mincheck)
  58. {
  59. alert('You must select ' + mincheck + ' Drivers');
  60. return false;
  61. }
  62. return true;
  63. }
  64.  
  65. </script>";
  66. $tpl -> AssignArray(array('selteam' => $sel_text));
  67. $tpl -> Flush();
  68.  
  69. ?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 21
Reputation: planethax is an unknown quantity at this point 
Solved Threads: 0
planethax planethax is offline Offline
Newbie Poster

Re: form validation

 
0
  #7
Oct 1st, 2009
ooops,
and on the .tpl side I have
  1. <form id='frmChk' action='testsel1.php' method='post' onsubmit='if (!checkCount()) return false;'>

I have action='testsel1.php' there just to test, but I imagine that is where I will need to input back into db?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 21
Reputation: planethax is an unknown quantity at this point 
Solved Threads: 0
planethax planethax is offline Offline
Newbie Poster

Re: form validation

 
0
  #8
Oct 2nd, 2009
Anyone?

I think once I get this next step figured out, the rest of the pages I need to make should go alot smoother lol.

Thanx.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 55
Reputation: wilch is an unknown quantity at this point 
Solved Threads: 9
wilch wilch is offline Offline
Junior Poster in Training

Re: form validation

 
1
  #9
Oct 2nd, 2009
Hi there !
Ok in the file testsel1.php, you need to do the following:
- revalidate the count of checked drivers, and ensure there are 7 (or less ??)
- generate the random id.
- save both drivers and random id
  1. <?php
  2.  
  3. if(! isset($_POST['submit'])) header("another_page.php");
  4.  
  5. if(isset($_POST['chks']) && count($_POST['chks']) == 7){
  6. $drivers = $_POST['chks'];
  7.  
  8. //generate random id
  9. $randID = substr(md5(microtime(), -7)) //generates a random string of 7 characters
  10.  
  11. //note the sql has an ellipsis(...) where you need to fill in the rest
  12. $sql = "INSERT INTO user_drivers(user_id, driver1, driver2, ..., driver7) VALUES('$randID', '".$drivers[0]."','".$drivers[1]."','...', '".$drivers[6]."')"
  13.  
  14. mysql_query($sql);
  15.  
  16. }
  17. else{
  18. //output invalid number of drivers specified
  19. }
  20.  
  21. ?>
Last edited by wilch; Oct 2nd, 2009 at 5:48 pm.
umm.. by the way how do you do it ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 21
Reputation: planethax is an unknown quantity at this point 
Solved Threads: 0
planethax planethax is offline Offline
Newbie Poster

Re: form validation

 
0
  #10
Oct 4th, 2009
I am trying to put info into the database, have 3 issues (no errors though)
1, The userid username are empty
2, The drivers name are just "on" (need to assign drivers names to checks? )
3, Page echos query, but DB is still empty.

Result page is
INSERT INTO user_drivers(userid, username, driver1, driver2, driver3, driver4, driver5, driver6, driver7) VALUES('','','on','on',''on', 'on', 'on', 'on', 'on')
DB table structure is

  1. CREATE TABLE `user_drivers` (
  2. `userid` int(11) NOT NULL,
  3. `username` varchar(32) character set utf8 collate utf8_unicode_ci NOT NULL,
  4. `driver1` text character set latin1 NOT NULL,
  5. `driver2` text character set latin1 NOT NULL,
  6. `driver3` text character set latin1 NOT NULL,
  7. `driver4` text character set latin1 NOT NULL,
  8. `driver5` text character set latin1 NOT NULL,
  9. `driver6` text character set latin1 NOT NULL,
  10. `driver7` text character set latin1 NOT NULL
  11. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Form page is
  1. <?php
  2.  
  3. $tpl = new template;
  4. $tpl -> Load("!theme/{$GLOBALS["THEME"]}/templates/fantasy/fselteam.tpl");
  5. $tpl -> GetObjects();
  6.  
  7.  
  8. if (isset($_SESSION["id"])) $tpl->Zone("userStatus", "user");
  9.  
  10. else $tpl->Zone("userStatus", "guest");
  11.  
  12. if (isset($GLOBALS["LOGIN_FAIL_TYPE"])) {
  13. if ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.password") $loginError = $GLOBALS["OBJ"]["loginError.password"];
  14. elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.user") $loginError = $GLOBALS["OBJ"]["loginError.username"];
  15. elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.bruteforce") $loginError = $GLOBALS["OBJ"]["loginError.bruteforce"];
  16. elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.active") $loginError = $GLOBALS["OBJ"]["loginError.active"];
  17. }
  18.  
  19. $tpl -> AssignArray(array(
  20. "login.failMessage" => (isset($loginError)?$loginError:NULL)
  21. ));
  22. // Report all PHP errors (see changelog)
  23. // error_reporting(E_ALL);
  24. include_once('db_conn.php');
  25. $sql = "SELECT * FROM `nascar_standings` ORDER BY driver ASC LIMIT 65";
  26. $rs = mysql_query($sql);
  27. $username = me('username');
  28. $userid = me('id');
  29. $i = 0;
  30. $sel_text = "";
  31. $column = "</tr><tr>";
  32. while($row = mysql_fetch_assoc($rs)){
  33. $label = $row['driver'];
  34. $sel_text .= "<td><input type='checkbox' name='chks[]'> $label </td>";
  35. if (++$i % 4 == 0)
  36. {
  37. $sel_text .= $column;
  38. }
  39. }
  40. $sel_text .= "</tr><tr><td colspan =4 valign = center><input type='submit' value='Validate & Submit' name='validate' onclick='if(!checkCount()) return false;'></td></tr></table></form>";
  41.  
  42. $sel_text .= "<script type='text/javascript'>
  43.  
  44. function checkCount(){
  45. var mincheck = 7;
  46. var testform = document.getElementById('frmChk');
  47. var items = testform.getElementsByTagName('input');
  48. var count = 0;
  49.  
  50. for (var i=0; i < items.length-1; i++)
  51. {
  52. if (items[i].type == 'checkbox' && items[i].checked)
  53. {
  54. count++;
  55. }
  56. }
  57.  
  58. if(count != mincheck)
  59. {
  60. alert('You must select ' + mincheck + ' Drivers');
  61. return false;
  62. }
  63. return true;
  64. }
  65.  
  66. </script>";
  67. $tpl -> AssignArray(array('selteam' => $sel_text));
  68. $tpl -> Flush();
  69.  
  70. ?>


The insert page is

  1. <?php
  2.  
  3. if(! isset($_POST['submit'])) header("fhome.php");
  4.  
  5. if(isset($_POST['chks']) && count($_POST['chks']) == 7){
  6. $drivers = $_POST['chks'];
  7.  
  8. include_once('db_conn.php');
  9.  
  10. //note the sql has an ellipsis(...) where you need to fill in the rest
  11. $sql = "INSERT INTO user_drivers(userid, username, driver1, driver2, driver3, driver4, driver5, driver6, driver7) VALUES('".$userid."','".$username."','".$drivers[0]."','".$drivers[1]."',''".$drivers[2]."', '".$drivers[3]."', '".$drivers[4]."', '".$drivers[5]."', '".$drivers[6]."')";
  12.  
  13. mysql_query($sql);
  14.  
  15. }
  16. else{
  17. //output invalid number of drivers specified
  18. }
  19. echo $sql;
  20. ?>
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 PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC