kenwvs Posted July 14, 2006 Share Posted July 14, 2006 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 = '';}?> Link to comment https://forums.phpfreaks.com/topic/14594-form-validation-should-i-look-at-doing-it-differently/ Share on other sites More sharing options...
Oldiesmann Posted July 14, 2006 Share Posted July 14, 2006 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). Link to comment https://forums.phpfreaks.com/topic/14594-form-validation-should-i-look-at-doing-it-differently/#findComment-57982 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.