Jump to content

Archived

This topic is now archived and is closed to further replies.

kenwvs

Form Validation - Should I look at doing it differently

Recommended Posts

Is the following a bad choice in how I am getting my form validation done?  I got it from a tutorial, but am not having much success in getting it to work, so am wondering if it is more complicated than it needs to be, and I should quit trying to figure out why this won't work, and move on to a different method?

Ken

<?php
/* sample.php */
require_once ('functions.php');

$valid = TRUE;

if (isset ($_POST['Submit'])) {
    foreach($_POST as $key=>$value) {
      $$key = $value;
    }
    $valid = $user = LettersAndDigits($id);
    $title = Variable($item_title);
    $valid = $valid && $title;
    $category = ItemCategory($item_category);
    $valid = $valid && $category;
    $type = Variable($item_type);
    $valid = $valid && $type;
    $quantity = isDigits($quantity_available);
    $valid = $valid && $quantity;
    $price = Dollars($starting_bid);
    $valid = $valid && $price;
    $increment = BidIncrement($bid_increment);
    $valid = $valid && $increment;
    $reserve = ReservePrice($reserve_price);
    $valid = $valid && $reserve;
    $length = isDigits($duration);
    $valid = $valid && $length;
    $time = isDigits($end_time);
    $valid =$valid && $time;
    $relist = isLetters($auto_relist);
    $valid = $valid && $relist;
    $ct = Variable($city);
    $valid = $valid && $ct;
    $prov = Variable($state);
    $valid = $valid && $prov;
    $cntry = Variable($country);
    $valid = $valid && $cntry;
    $desc = Variable($item_description);
    $valid = $valid && $desc;
    $pay = EmailorEmpty($paypal_id);
    $valid = $valid && $pay;
    $hit = Variable($hit_counter);
    $valid = $valid && $hit;
      if ($valid) {
        header ("uploadformconfirmation.html");
        exit;
      }
} else {
    $user = $title = $category = $type = $quantity = $price = $increment =
    $reserve = $length =$time = $relist = $ct = $prov = $cntry = $desc = $pay =
    $hit = TRUE;
    $id = $item_title = $item_category = $item_type = $quantity_available =
    $starting_bid = $bid_increment = $reserve_price = $duration = $end_time =
    $auto_relist = $city = $state = $country = $item_description = $paypal_id =
    $hit_counter = '';
}
?>

Share this post


Link to post
Share on other sites
That's definitly more complicated than it needs to be in my opinion. Using a switch statement should simplify things tremendously:

[code]<?php
/* sample.php */
require_once('functions.php');

$valid = TRUE;

if(isset($_POST['submit']))
{
    foreach(array_keys($_POST) AS $key)
    {
        switch ($key) {
            case "id":
                $valid &= LettersAndDigits($_POST['id']);
                break;
            case "item_title": case "item_type": case "city": case "state": case "country": case "item_description": case "hit_counter":
                $valid &= Variable($_POST[$key]);
                break;
            case "quantity_available": case "duration": case "end_time":
                $valid &= isDigits($_POST[$key]);
                break;
            case "starting_bid":
                $valid &= Dollars($_POST['starting_bid']);
                break;
            case "bid_increment":
                $valid &= BidIncrement($_POST['bid_increment']);
                break;
            case "reserve_price":
                $valid &= ReservePrice($_POST['reserve_price']);
                break;
            case "auto_relist":
                $valid &= isLetters($_POST['auto_relist']);
                break;
            case "paypal_id":
                $valid &= EmailorEmpty($_POST['paypal_id']);
                break;
            case "submit":
                break;
        }
    }
    if($valid)
    {
        header("Location: uploadformconfirmation.html");
        exit;
    }
}
?>[/code]

Still probably not the best way of doing things, but it's a lot simpler (note the minor change to header - you have to tell PHP that this is a location header or it won't work).

Share this post


Link to post
Share on other sites

×

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.