Jump to content

Any ideas on handling a big big form?


mendoz

Recommended Posts

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

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 = [email protected]';

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

}

}

Archived

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

×
×
  • 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.