Jump to content

Recommended Posts

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  ;D

 

Link to comment
https://forums.phpfreaks.com/topic/54763-criticize-this-form-validation-please/
Share on other sites

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.