a few questions

i had to make a form… and i need to print a list of any missing fields but not print the user inputs. i also need it to be sticky? i guess so that if there is an error they do not have to type in the whole form again… i was wondering if someone could check out my code if you have time, and see what i am doing wrong…

[php]<?php

// in general, process the form input before any output the script may send to the browser. This means that the PHP code block should begin right on the first line of the file – before any HTML code and text.
// process the email

if (array_key_exists(‘Register’, $_POST)) {
$to = [email protected]’;
$subject = ‘Feedback from Registration’;

// list expected fields
$expected = array(‘lastName’, ‘email’, ‘instructions’);
// set required fields
$required = array(‘lastName’, ‘instructions’);
// create empty array for any missing fields
$missing = array();

// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$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 of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}

// go ahead only if all required fields OK
if (empty($missing))
{
// build the message
$message = “Name: $lastNamenn”;
$message .= “Email: $emailnn”;
$message .= “Comments: $instructions”;

// limit line length to 70 characters
$message = wordwrap($message, 70);

// send it
$mailSent = mail($to, $subject, $message);
if ($mailSent)
{
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}

}
?>

Belinda's Form Processing Exercise

Form Processing Exercise


Free Newsletter Registration

Please fill out the form and submit to register for free subscription.

<?php if ($_POST && !empty($missing)) { echo $missing; // mail wasn't sent ?> <?php } elseif ($_POST && !$mailSent){?>

Sorry, there was a problem sending your message. Please try later.

<?php } elseif ($_POST && $mailSent){ ?>

Your message was sent. Thank you for the feedback.

<?php } ?>[/php]same file:[code] First Name:

Last Name:

Email:

Topics:

Cooking




Gardening




Coding


Delivery frequency:

Daily


Weekly


Monthly

Language:
English Spanish French

Special instructions:


[/code]

[size=99px]edit Q1712: added [php] and [code][/size]

sry, i havn’t had time before.

ur loop:
it is running through all posted vars, which is user input.
i would reverse that an runn through all keys in $expected.
none of them should be an array.
it’s better coding style to use $GLOBALS than to use variable variables.

[php]// process the $_POST in $expected variables
foreach ($expected as $key) {
// assign to temporary variable and strip whitespace
$temp = isset($_POST[$key]) ? trim($_POST[$key]) : “”;
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
$GLOBALS[$key] = $temp;
}
}[/php]

to keep the values u need a value-atribute in the inputs

<input type="text" name="firstName" value="<?php htmlentities($firstName) ?>">
Sponsor our Newsletter | Privacy Policy | Terms of Service