/    Sign up×
Community /Pin to ProfileBookmark

Undefined variable: mailSent

Hi-
I’m new to php and am just having a little trouble with a part of my php code, I’ve been working on a form script and it processes the form and emails it to right place with everything intact but I keep getting the error ‘Undefined variable: mailSent’ when I send the form. Because of this error, I’m also not redirecting once the form goes out. Could someone help me identify what I’m missing? Where would I have to define this variable?

I’ve attached the code located in <head> and at the top <body> below.

I appreciate the help,
Ken

<?php
if (array_key_exists(‘send’, $_POST)) {

// MAIL PROCESSING SCRIPT ————————————————

// remove escape characters from POST array

if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ? array_map(‘stripslashes_deep’, $value) : stripslashes($value);
return $value;
}
$_POST = array_map(‘stripslashes_deep’, $_POST);
}

// WHERE THE FORM IS GOING ———————————————–

$to = ‘[email protected]’; // email address that script is sent to
$subject = ‘Request for Information Form’;

// CHECKING FOR REQUIRED FIELDS ——————————————

// list expected form fields
$expected = array(‘nameFirst’, ‘nameLast’, ’email’, ‘phone’, ‘streetAddress’, ‘poBox’, ‘city’, ‘state’, ‘zipCode’, ‘webSite’, ‘programOfInterest’);
// set required form fields
$required = array(‘nameFirst’, ‘nameLast’, ’email’, ‘phone’, ‘streetAddress’, ‘city’, ‘state’, ‘zipCode’, ‘programOfInterest’);
// create empty array for any missing form fields
$missing = array();

// SPAM ——————————————————————

// spam filter, assume that nothing is suspect
$suspect = false;
// spam filter, create a pattern to lOcate suspect phrases
$pattern = ‘/Content-Type:|Bcc:|Cc:/i’;

// function to check for suspect phrases

function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}

//————————————-

if ($suspect) {
$mailSent = false;
unset($missing);
}
else {

// PROCESS THE $_POST VARIABLES ——————————————

foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitesapce 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;
}

}
}

// VALIDATE THE EMAIL ADDRESS ———————————————

if (!empty($email)) {

// regex to identify illegal characters in email address
$checkEmail = ‘/^[^@]+@[^srn'”;,@%]+$/’;
// reject email address if it doesn’t match
if (!preg_match($checkEmail, $email)) {
$suspect = true;
$mailSent = false;
unset($missing);
}
}
// go ahead only if all required fields are OK
if (!$suspect && empty($missing)) {
// set default values for variables that might not exist
$interests = isset($interests) ? $interests : array(‘None Selected’);

// BUILD THE MESSAGE ——————————————————

$message = “First Name: $nameFirstnn”;
$message .= “Last Name: $nameLastnn”;
$message .= “Email: $emailnn”;
$message .= “Phone Number: $phonenn”;
$message .= “Street Address: $streetAddressnn”;
$message .= “PO Box: $poBoxnn”;
$message .= “City: $citynn”;
$message .= “State: $statenn”;
$message .= “Zip Code: $zipCodenn”;
$message .= “Website: $webSitenn”;
$message .= ‘Program of Interest: ‘.implode(‘, ‘,$programOfInterest);

// LIMIT LINE LENGTH TO 70 CHARACTERS ————————————-

$message = wordwrap($message, 70);

// CREATE ADDITIONAL HEADERS ———————————————-

$headers = “From: UNM SAAP Websitern”;
if (!empty($email)) {
$headers .= “rnReply-To: $email”;
}

// SEND IT! —————————————————————

$mailsent = mail($to, $subject, $message, $headers);
if ($mailSent) {
// redirect the page with a fully qualified URL
header(‘Location: http://www.kmarold.com/egdwcs3/requestForInfo_thankYou.php’);
exit;
}

}
}
?>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>UNM SAAP ~ Request for Information</title>
<link rel=”stylesheet” media=”screen” type=”text/css” href=”css/forms.css” />
</head>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>UNM SAAP ~ Request for Information</title>
<link rel=”stylesheet” media=”screen” type=”text/css” href=”css/forms.css” />
</head>

<body>

<h1> Request for Information </h1>

<form action=”<?php echo $_SERVER[‘PHP_SELF’]; ?>” method=”post” name=”requestInfo” id=”requestInfo”>

<!– PERSONAL INFORMATION SECTION –>

<fieldset>
<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class=”warning”>Please complete the required item(s) indicated.</p>
<?php
}
elseif ($_
POST && !$mailsent) {
?>
<p class=”warning”>Sorry, there was a problem sending your message. Please try again later.</p>

<?php }
elseif ($_POST && $mailSent) { // display mail sent acknowledgement
?>
<p><strong>Your message has been sent. Thank you for your interest in UNM SAAP.</strong></p>
<?php } ?>

<legend>Personal Information</legend>
<dl>

<div class=”float box”>
<dt><label for=”nameFirst”>First Name
<?php
if (isset($missing) && in_array(‘nameFirst’, $missing)) { ?>
<span class=”warning”>**</span><?php } ?>
</label>
<input name=”nameFirst” type=”text” class=”textInput” id=”nameFirst” tabindex=”1″
<?php if (isset($missing)) {
echo ‘value=”‘.htmlentities($_POST[‘nameFirst’]).'”‘;
} ?>
/></dt></div>

<div class=”float box”>
<dt><label for=”nameLast”>Last Name
<?php
if (isset($missing) && in_array(‘nameLast’, $missing)) { ?>
<span class=”warning”>**</span><?php } ?>
</label>
<input name=”nameLast” type=”text” class=”textInput” id=”nameLast” tabindex=”2″
<?php if (isset($missing)) {
echo ‘value=”‘.htmlentities($_POST[‘nameLast’]).'”‘;
} ?>
/></dt></div>

<div class=”float-divider”></div>

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @kenmarold spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...