dshevnock Posted June 8, 2007 Share Posted June 8, 2007 I have a signup form that has 19 small textboxes that will be populated with one number PER textbox from the user. The textboxes on the signup form are respectively called: card1, card2, card3....card18, card19. For my form validation, I know I could do the longhand version of: [pre] if(isset($_POST['card1'] && $_POST['card1'] != "" && is_numeric($_POST['card1']) && strlen($_POST['card1']) == 1) { //proceed } else{ // setup error message(s) } [/pre] ...and reproduce this for the other 18 textboxes relatively easily, but I would rather learn the more efficient and correct way to do. Here is what I came up with: [pre] for ($i = 1; $i<=19;$i++){ $var = 'card'.$i; $temp = $_POST[$var]; if($$var == "" || !is_numeric($$var) || strlen($$var) != 1){ // error message of some sort } } [/pre] Any criticism/suggestions? I don't cry for that long and I don't hold grudges, so please come with it Link to comment https://forums.phpfreaks.com/topic/54763-criticize-this-form-validation-please/ Share on other sites More sharing options...
per1os Posted June 8, 2007 Share Posted June 8, 2007 Why are you using the $$... that is used to create a variable out of the data of $var... <?php for ($i = 1; $i<=19;$i++){ $var = 'card'.$i; if(!isset($_POST[$var]) || $_POST[$var] != "" || !is_numeric($_POST[$var]) || strlen($_POST[$var]) != 1){ // error message of some sort } } ?> Would be better, as incase notices are on the assigning of the $temp variable would throw a notice error if the variable was not an index. Link to comment https://forums.phpfreaks.com/topic/54763-criticize-this-form-validation-please/#findComment-270828 Share on other sites More sharing options...
dshevnock Posted June 8, 2007 Author Share Posted June 8, 2007 Why are you using the $$... that is used to create a variable out of the data of $var... This is the first time I have researched and used the $$ syntax before. I guess I miss understood it's exact use. [pre]<?php for ($i = 1; $i<=19;$i++){ $var = 'card'.$i; if(!isset($_POST[$var]) || $_POST[$var] != "" || !is_numeric($_POST[$var]) || strlen($_POST[$var]) != 1){ // error message of some sort } } ?>[/pre] Would be better, as incase notices are on the assigning of the $temp variable would throw a notice error if the variable was not an index. Thanks for the advice! i didn't even think using a combination of the two possibilities. Link to comment https://forums.phpfreaks.com/topic/54763-criticize-this-form-validation-please/#findComment-270878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.