Jump to content


Photo

Form Validation woes


  • Please log in to reply
12 replies to this topic

#1 stormflurry

stormflurry
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 March 2006 - 04:10 AM

Hi All,

I'm building a custom validator and here's the issue that i'm running into. PHP does not collect unset varaibles names for checkboxes or radio buttons into the $_POST array.

so when I do this

(foreach $_POST as $key => $value) {

etc

}

I cannot easily check to see if the varaible is empty

so the line that I'm working along is creating a hidden variable in the form that does this

<input type="hidden" name="checkme" value="checkbox/radiobutton field_name that needs to be check">

my validation code looks something like this


//each varialbe name gets exploded and validated based on the trailing characters
if ($check[1] == 3) {
//set the name of the field that I need to look for
$temp = $value;


//check to see if the variable is set and then if not push that back through the header
if ($$temp != "on") {

$count++;

$error_string .= 'Error_'.$value.'=on&';


}

}

here's my issue this works but I need to change $$temp into something that looks like this $_POST[$$temp] but this doesn't work I need some help to be able to set $_POST['x'] x on the fly to whatever I want...

I've tried a bunch of different things at this point and needs some help.

Thanks

Matt

#2 stormflurry

stormflurry
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 March 2006 - 12:51 PM

Any help anybodY?

#3 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 29 March 2006 - 02:03 PM

What are you looking to do exactly. You can use java to make sure fields are filled in before the form is even submitted. Then there would not be a need to do a check with php after.

If you want to do this I can give you what you need. I have alot of samples to check on form fields.

Ray

#4 stormflurry

stormflurry
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 March 2006 - 02:44 PM

[!--quoteo(post=359656:date=Mar 29 2006, 09:03 AM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 29 2006, 09:03 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What are you looking to do exactly. You can use java to make sure fields are filled in before the form is even submitted. Then there would not be a need to do a check with php after.

If you want to do this I can give you what you need. I have alot of samples to check on form fields.

Ray
[/quote]
Ray that would be appreciated I haven't implemented any pre postback validation yet but this side of the script i mainly to protect my DB from junk and make sure that the customer doesn't throw of any SQL errors...

#5 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 29 March 2006 - 04:23 PM

If you post the form part of your script I can write in the java for you. You can go as far as checking patterns or numbers only or letters only, or just make sure there are no blank fields. Let me know what fields you want to check and what to check for and I will do what I can.

Ray

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 04:43 PM

Why don't you create a hidden field for each of the fields that potentionally won't be passed back with a default value. Name this field the same as the real field. This way your script will get a value no matter what the user does and you don't have to jump through hoops to do your validation.

To the poster craygo who keep saying to use Java, I believe you are refering to Javascript, not Java. They are two different languages. Please spell out Javascript when you mean Javascript and don't abbreviate it to Java.

Ken

#7 stormflurry

stormflurry
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 March 2006 - 05:50 PM

[!--quoteo(post=359705:date=Mar 29 2006, 11:43 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 29 2006, 11:43 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Why don't you create a hidden field for each of the fields that potentionally won't be passed back with a default value. Name this field the same as the real field. This way your script will get a value no matter what the user does and you don't have to jump through hoops to do your validation.

To the poster craygo who keep saying to use Java, I believe you are refering to Javascript, not Java. They are two different languages. Please spell out Javascript when you mean Javascript and don't abbreviate it to Java.

Ken
[/quote]

Hey Ken,

I've actually experimented with that and here's the example

let's say this is part of my form
<input type="checkbox" name="field1">
<input type="hidden" name="field1" value="off">

Let's say that I want the checkbox to be checked in my POST array I now have field1=off&field1=on

while I'm looping through my validator for each varaible I'll run into an issue it'll perform two separate actions

foreach($_POST as $key => $value)
{
if ($field1 == "off") {
Send Error }
elsif($field1 == "on") {
no error
}

so my validator will kick an error no matter what I want to have happen...at least I haven't found a way around that little quandy at this point. I keep coming up with new solutions or ideas on how to do this and I get to a point where PHP just won't allow me to do what I need to do. It's frustrating.

Javascript is nice but I need to have a bullet proof postback method that uses neither javascript or ajax...



#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 29 March 2006 - 07:37 PM

Put the hidden field before the real field:
<input type="hidden" name="field1" value="off">
<input type="checkbox" name="field1">

If you look at the $_POST array after receiving this data "field1=off&field1=on", you should only see one "field1", not two.

Ken

#9 gavinandresen

gavinandresen
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 30 March 2006 - 12:24 AM

Do you care if hackers can influence your form validation?

If you do (and you probably should), then you can't put any information about what should be validate or how it should be validated in your forms (because it's really easy to change hidden fields-- e.g. grab the FireFox Web Developer extension and it lets you see and edit the hidden fields before submission).

You should probably turn it around, and define what fields need to get validated in your PHP code:

$needValidation = array('foo', 'bar', ... etc);

foreach ($needValidation AS $field) {
if (!isset($_POST[$field])) or empty($_POST[$field])) ... error....
}

I handle form validation something like this:

$validationData['siteName'] = array('isRequired', 'type' => 'text');
$validationData['isLive'] = array('isRequired', 'type' => 'number');
$validationData['rootDirectory'] = array('isRequired', 'type' => 'text');
$validationData['defaultTemplate'] = array('isRequired', 'type' => 'text');
$validationData['siteFooter'] = array('isRequired', 'type' => 'text');
$validationData['provdist'] = array('isRequired');
if (isset($post['submit'])) {
  $formErrors = validateForm($post, $validationData);
  if (!preg_match('/^\w*$/', $post['rootDirectory'])) {
    $formErrors['rootDirectory'] = "Invalid Location";
  }
  if (count($formErrors) == 0) {
    // Normally there would be code here to process the form
    // and redirect to a thank you page...
  }
}
else {
  $formErrors = array();
}

echo fillInFormValues($html, $post, $formErrors);

(try out the form at [a href=\"http://www.skypaint.com/gavin/code/longExample.php\" target=\"_blank\"]http://www.skypaint.com/gavin/code/longExample.php[/a] )


#10 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 12:28 AM

I'd simply do something like this:
<?php
$error = FALSE;
$required = array( 'checkboxname' , 'inputname' );
foreach ( $required as $require )
{
    if ( ! isset( $_POST[ $require ] ) || ( empty( $_POST[ $require ] ) ) ) {
        $error = TRUE;
        break;
    }
}
if ( $error ) {
//something was left out
}
?>


#11 stormflurry

stormflurry
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 30 March 2006 - 02:17 AM

[!--quoteo(post=359856:date=Mar 29 2006, 07:24 PM:name=gavinandresen)--][div class=\'quotetop\']QUOTE(gavinandresen @ Mar 29 2006, 07:24 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Do you care if hackers can influence your form validation?

If you do (and you probably should), then you can't put any information about what should be validate or how it should be validated in your forms (because it's really easy to change hidden fields-- e.g. grab the FireFox Web Developer extension and it lets you see and edit the hidden fields before submission).

You should probably turn it around, and define what fields need to get validated in your PHP code:

$needValidation = array('foo', 'bar', ... etc);

foreach ($needValidation AS $field) {
if (!isset($_POST[$field])) or empty($_POST[$field])) ... error....
}

I handle form validation something like this:

$validationData['siteName'] = array('isRequired', 'type' => 'text');
$validationData['isLive'] = array('isRequired', 'type' => 'number');
$validationData['rootDirectory'] = array('isRequired', 'type' => 'text');
$validationData['defaultTemplate'] = array('isRequired', 'type' => 'text');
$validationData['siteFooter'] = array('isRequired', 'type' => 'text');
$validationData['provdist'] = array('isRequired');
if (isset($post['submit'])) {
  $formErrors = validateForm($post, $validationData);
  if (!preg_match('/^\w*$/', $post['rootDirectory'])) {
    $formErrors['rootDirectory'] = "Invalid Location";
  }
  if (count($formErrors) == 0) {
    // Normally there would be code here to process the form
    // and redirect to a thank you page...
  }
}
else {
  $formErrors = array();
}

echo fillInFormValues($html, $post, $formErrors);

(try out the form at [a href=\"http://www.skypaint.com/gavin/code/longExample.php\" target=\"_blank\"]http://www.skypaint.com/gavin/code/longExample.php[/a] )
[/quote]

This is a good suggestion and I do do script side validation for my purposes. The main purpose of this class validator i'm building is to allow my users to create forms and database tables on the fly. Basically I want them to have to build the form using the form builder and select which fields need which type of validation before it get's inserted into the database. I guess theoretically I could build custom script side validatorion for each form by creating another include...I don't know it's an option but it seems like there should be something cleaner.

I'm aware of the hacker problem. I do it myself in certain situations. This validator would not be used in any place where data integrity is a 100% priority. That would all be built right in to the script.

#12 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 30 March 2006 - 04:47 AM

JAVASCRIPT, make sure i use correct term, would probably be the simple and easy way to validate the form. No need to submit it checks will be done right away.

Ray

#13 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 30 March 2006 - 05:38 AM

[!--quoteo(post=359906:date=Mar 29 2006, 08:47 PM:name=craygo)--][div class=\'quotetop\']QUOTE(craygo @ Mar 29 2006, 08:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
JAVASCRIPT, make sure i use correct term, would probably be the simple and easy way to validate the form. No need to submit it checks will be done right away.

Ray
[/quote]
JavaScript can be easily disabled on most browsers, and on those it cannot be disabled on, if PHP does no error checking the user can copy and paste the HTML output on the form, copy it to their machine, save the page, remove the JavaScript, run the script and submit it directly to the site. PHP does no checking to see where the form came from or that it's valid...and the user got right in.

BTW: Another common problem...Many people use: <input type="button" ... onclick="checkform()" />
Simple way to get around that validation is to type this in your address bar:
- javascript:document.forms[0].submit();




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users