mendoz Posted April 1, 2007 Share Posted April 1, 2007 Hey guys. I have a form with 100+ fields (a client, don't ask) I guess I can be doing $_POSTs all day long and then a long long query. Maybe you heard about an easier method... Thanks, Dror Quote Link to comment Share on other sites More sharing options...
tarun Posted April 1, 2007 Share Posted April 1, 2007 Have It As An Array Then Use foreach Quote Link to comment Share on other sites More sharing options...
mendoz Posted April 1, 2007 Author Share Posted April 1, 2007 Thanks man but use what as an array ??? Quote Link to comment Share on other sites More sharing options...
mendoz Posted April 1, 2007 Author Share Posted April 1, 2007 <?php foreach($_POST as $varName => $value) { $dv=$value; print "<b>$varName</b>: $dv<br>"; }; ?> seemed to work Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2007 Share Posted April 1, 2007 Why are you creating another variable in your loop, this will work just as well: <?php foreach($_POST as $varName => $value) echo "<b>$varName</b>: $value<br>"; ?> BTW, you shouldn't have the semi-colon after the closing "}". Ken Quote Link to comment Share on other sites More sharing options...
TheBG Posted April 1, 2007 Share Posted April 1, 2007 Here you go. This will handle any size form, check for required entries AND stop the bad guys... //************************************************************************ // // Form Handler // // $to = email address to send to // $subject = subject statement for email // $expected = ARRAY - list of expected fields // This is a BIG security check mesure which will not allow insertion of // additional fields because only the fields you are looking for will be processed! // $required = ARRAY - a list of required fields // $missing = ARRAY - empty // //************************************************************************ if (array_key_exists('submit', $_POST)) { $to = someone@email.com'; $subject = 'Feedback from Web site'; // Expected fields $expected = array('name', 'email', 'phone', 'comment'); // Required fields $required = array('name', 'email'); //Missing fields $missing = array(); // process the $_POST variables foreach( $_POST as $key => $value) { $temp = is_array($value) $value : trim($value); //if empty AND required add to $missing array if( empty($temp) && in_array($key, $required)) { array_push($missing, $key); } //otherwise assign to a variable with same name as $key // BIG security check! DO NOT EDIT! elseif( in_array($key, $expected)) { ${$key}=$temp; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.