943,808 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1245
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 21st, 2009
-1

form validation

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
planethax is offline Offline
34 posts
since Jun 2005
Sep 21st, 2009
0

Re: form validation

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.

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 947
Sarcastic Poster
ardav is offline Offline
6,680 posts
since Oct 2006
Sep 21st, 2009
1

Re: form validation

Here's the idea, though i have not tested the code, the logic is correct.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007
Sep 22nd, 2009
0

Re: form validation

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
planethax is offline Offline
34 posts
since Jun 2005
Sep 22nd, 2009
1

Re: form validation

No problem !
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007
Oct 1st, 2009
0

Re: form validation

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

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 10
Solved Threads: 0
Light Poster
planethax is offline Offline
34 posts
since Jun 2005
Oct 1st, 2009
0

Re: form validation

ooops,
and on the .tpl side I have
PHP Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
planethax is offline Offline
34 posts
since Jun 2005
Oct 2nd, 2009
0

Re: form validation

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
planethax is offline Offline
34 posts
since Jun 2005
Oct 2nd, 2009
1

Re: form validation

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
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 21
Solved Threads: 15
Junior Poster in Training
wilch is offline Offline
76 posts
since Aug 2007
Oct 4th, 2009
0

Re: form validation

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
Quote ...
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

PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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

PHP Syntax (Toggle Plain Text)
  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. ?>
Reputation Points: 10
Solved Threads: 0
Light Poster
planethax is offline Offline
34 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Code.......
Next Thread in PHP Forum Timeline: Uploading media files trouble...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC