| | |
form validation
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jun 2005
Posts: 21
Reputation:
Solved Threads: 0
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.
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.
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.
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.
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)
$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
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
Here's the idea, though i have not tested the code, the logic is correct.
PHP Syntax (Toggle Plain Text)
<?php $sql = "SELECT * FROM options LIMIT 65"; $rs = mysql_query($sql); echo "<form name='frmChk' action='mypage.php' method='post'>"; while($row = mysql_fetch_assoc($rs)){ $label = $row['option_name']; echo "<input type='checkbox' name='chks[]'> $label <br>"; } echo "<input type='submit' value='Validate & Submit' name='validate' onclick='if(!checkCount()) return false;'></form>"; echo "<script language='javascript'> function checkCount(){ var count=0; var inputs=document.frmChk.getElementsByTagName('input'); for(var i=0; i<inputs.length-1; i++){ if(inputs[i].type == 'checkbox' && inputs[i].checked == true) count++; } if(count != 7){ alert('You must select exactly 7 checkboxes'); return false; } return true; } </script>"; ?>
Last edited by wilch; Sep 21st, 2009 at 1:10 pm.
•
•
Join Date: Jun 2005
Posts: 21
Reputation:
Solved Threads: 0
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
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)
<?php $tpl = new template; $tpl -> Load("!theme/{$GLOBALS["THEME"]}/templates/fantasy/fselteam.tpl"); $tpl -> GetObjects(); if (isset($_SESSION["id"])) $tpl->Zone("userStatus", "user"); else $tpl->Zone("userStatus", "guest"); if (isset($GLOBALS["LOGIN_FAIL_TYPE"])) { if ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.password") $loginError = $GLOBALS["OBJ"]["loginError.password"]; elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.user") $loginError = $GLOBALS["OBJ"]["loginError.username"]; elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.bruteforce") $loginError = $GLOBALS["OBJ"]["loginError.bruteforce"]; elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.active") $loginError = $GLOBALS["OBJ"]["loginError.active"]; } $tpl -> AssignArray(array( "login.failMessage" => (isset($loginError)?$loginError:NULL) )); // Report all PHP errors (see changelog) // error_reporting(E_ALL); include_once('db_conn.php'); $sql = "SELECT * FROM `nascar_standings` ORDER BY driver ASC LIMIT 65"; $rs = mysql_query($sql); $user = "frank"; $i = 0; $sel_text = ""; $column = "</tr><tr>"; while($row = mysql_fetch_assoc($rs)){ $label = $row['driver']; $sel_text .= "<td><input type='checkbox' name='chks[]'> $label </td>"; if (++$i % 4 == 0) { $sel_text .= $column; } } $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>"; $sel_text .= "<script type='text/javascript'> function checkCount(){ var mincheck = 7; var testform = document.getElementById('frmChk'); var items = testform.getElementsByTagName('input'); var count = 0; for (var i=0; i < items.length-1; i++) { if (items[i].type == 'checkbox' && items[i].checked) { count++; } } if(count != mincheck) { alert('You must select ' + mincheck + ' Drivers'); return false; } return true; } </script>"; $tpl -> AssignArray(array('selteam' => $sel_text)); $tpl -> Flush(); ?>
•
•
Join Date: Jun 2005
Posts: 21
Reputation:
Solved Threads: 0
ooops,
and on the .tpl side I have
I have action='testsel1.php' there just to test, but I imagine that is where I will need to input back into db?
and on the .tpl side I have
PHP Syntax (Toggle Plain Text)
<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?
•
•
Join Date: Aug 2007
Posts: 55
Reputation:
Solved Threads: 9
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
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)
<?php if(! isset($_POST['submit'])) header("another_page.php"); if(isset($_POST['chks']) && count($_POST['chks']) == 7){ $drivers = $_POST['chks']; //generate random id $randID = substr(md5(microtime(), -7)) //generates a random string of 7 characters //note the sql has an ellipsis(...) where you need to fill in the rest $sql = "INSERT INTO user_drivers(user_id, driver1, driver2, ..., driver7) VALUES('$randID', '".$drivers[0]."','".$drivers[1]."','...', '".$drivers[6]."')" mysql_query($sql); } else{ //output invalid number of drivers specified } ?>
Last edited by wilch; Oct 2nd, 2009 at 5:48 pm.
umm.. by the way how do you do it ?
•
•
Join Date: Jun 2005
Posts: 21
Reputation:
Solved Threads: 0
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
DB table structure is
Form page is
The insert page is
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')
PHP Syntax (Toggle Plain Text)
CREATE TABLE `user_drivers` ( `userid` int(11) NOT NULL, `username` varchar(32) character set utf8 collate utf8_unicode_ci NOT NULL, `driver1` text character set latin1 NOT NULL, `driver2` text character set latin1 NOT NULL, `driver3` text character set latin1 NOT NULL, `driver4` text character set latin1 NOT NULL, `driver5` text character set latin1 NOT NULL, `driver6` text character set latin1 NOT NULL, `driver7` text character set latin1 NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Form page is
PHP Syntax (Toggle Plain Text)
<?php $tpl = new template; $tpl -> Load("!theme/{$GLOBALS["THEME"]}/templates/fantasy/fselteam.tpl"); $tpl -> GetObjects(); if (isset($_SESSION["id"])) $tpl->Zone("userStatus", "user"); else $tpl->Zone("userStatus", "guest"); if (isset($GLOBALS["LOGIN_FAIL_TYPE"])) { if ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.password") $loginError = $GLOBALS["OBJ"]["loginError.password"]; elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.user") $loginError = $GLOBALS["OBJ"]["loginError.username"]; elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.bruteforce") $loginError = $GLOBALS["OBJ"]["loginError.bruteforce"]; elseif ($GLOBALS["LOGIN_FAIL_TYPE"] == "e.active") $loginError = $GLOBALS["OBJ"]["loginError.active"]; } $tpl -> AssignArray(array( "login.failMessage" => (isset($loginError)?$loginError:NULL) )); // Report all PHP errors (see changelog) // error_reporting(E_ALL); include_once('db_conn.php'); $sql = "SELECT * FROM `nascar_standings` ORDER BY driver ASC LIMIT 65"; $rs = mysql_query($sql); $username = me('username'); $userid = me('id'); $i = 0; $sel_text = ""; $column = "</tr><tr>"; while($row = mysql_fetch_assoc($rs)){ $label = $row['driver']; $sel_text .= "<td><input type='checkbox' name='chks[]'> $label </td>"; if (++$i % 4 == 0) { $sel_text .= $column; } } $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>"; $sel_text .= "<script type='text/javascript'> function checkCount(){ var mincheck = 7; var testform = document.getElementById('frmChk'); var items = testform.getElementsByTagName('input'); var count = 0; for (var i=0; i < items.length-1; i++) { if (items[i].type == 'checkbox' && items[i].checked) { count++; } } if(count != mincheck) { alert('You must select ' + mincheck + ' Drivers'); return false; } return true; } </script>"; $tpl -> AssignArray(array('selteam' => $sel_text)); $tpl -> Flush(); ?>
The insert page is
PHP Syntax (Toggle Plain Text)
<?php if(! isset($_POST['submit'])) header("fhome.php"); if(isset($_POST['chks']) && count($_POST['chks']) == 7){ $drivers = $_POST['chks']; include_once('db_conn.php'); //note the sql has an ellipsis(...) where you need to fill in the rest $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]."')"; mysql_query($sql); } else{ //output invalid number of drivers specified } echo $sql; ?>
![]() |
Similar Threads
- PHP Form Validation ??? (PHP)
- sample code for form validation using ajax (JavaScript / DHTML / AJAX)
- javascript Form Validation Libraries (JavaScript / DHTML / AJAX)
- PHP Mail Form Validation - Help Please? (PHP)
- coldfusion form validation question (ColdFusion)
- Really Stuck - ASP/Javascript Form Validation (ASP)
- Dreamweaver php form validation and redirect (PHP)
- Help on form validation requested. (PHP)
- uregnt need of form validation (JSP)
Other Threads in the PHP Forum
- Previous Thread: PHP Code.......
- Next Thread: Uploading media files trouble...
| Thread Tools | Search this Thread |
.htaccess ajax apache api array back basic beginner binary broken cakephp checkbox class cms code computing cron curl customizableitems database date delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validation validator variable video web xml youtube






