Newbie issue. I’m having some problems with a form that when loaded on an IIS platform running PHP 5.3 throws the following errors:
PHP Notice: Undefined index: validate
PHP Notice: Undefined variable: validated
PHP Notice: Undefined index: name
PHP Notice: Undefined index: email
PHP Notice: Undefined index: sub
PHP Notice: Undefined variable: msg
I’m fairly new to PHP, but it does boggle my mind that the same form works flawlessly in my MAMP development environment with both PHP 5.2 and 5.3.
I’ve posted the code I’m using below, if anyone can provide advice/tips/guidance, I would be very grateful. It’s a simple form with some simple validation.
<?php
if( $_POST['validate'] == 'yes' ) {
if(trim($_POST['name']) == '') { // Make sure there is a name
$hasError = true;
}
else {
$name = stripslashes(htmlentities(trim($_POST['name'])));
}
if(trim($_POST['sub']) == '') { // Make sure there is a subject
$hasError = true;
}
else {
$sub = stripslashes(htmlentities(trim($_POST['sub'])));
}
if(trim($_POST['email']) == '') { // Make sure the email is filled out correctly
$hasError = true;
}
elseif (!preg_match("/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$/i", trim($_POST['email']))) {
$hasError = true;
}
else {
$email = trim($_POST['email']);
}
if(trim($_POST['msg']) == '') { // Make sure there is a msg.
$hasError = true;
}
else {
$msg = stripslashes(htmlentities(trim($_POST['msg'])));
}
if(isset($_POST['fireinthehole'])) {
$thehole = true;
}
// If the fields not filled correctly, display an error.
// Else, send the message.
if (isset($hasError)) {
echo '<p class="error">Please check that all fields contain valid information. Thank you.</p>';
}
elseif (isset($thehole)) {
echo '<p class="error">What are you playing at?</p>';
}
else {
$validated = true; // Validation succeeded. Make sure the form does not display again.
$emailTo = 'Put your own email address here';
$body = "Name: $name \
\
Email: $email \
\
Subject: $sub \
\
Comments:\
$msg";
$headers = 'From: ToyPC Contact Form < '.$emailTo.'>' . "\\r\
" . 'Reply-To: ' . $email;
mail($emailTo, $sub, $body, $headers);
$emailSent = true;
?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you for contacting us. Your message has been sent, and we will be in touch with you shortly.</p>
<?php
} // end error response
} // end if( $_POST['validate'] == 'yes')
?>
<!-- Begin the form -->
<?php if(!$validated) { ?>
<form method="post" action="#form">
<fieldset>
<label for="f_name" class="form_label">Name:</label>
<input type="text" maxlength="255" id="f_name" name="name" value="<?php echo
stripslashes(htmlentities($_POST['name'])); ?>" /><br />
<label for="f_email" class="form_label">Email:</label>
<input type="text" maxlength="255" id="f_email" name="email" value="<?php echo
stripslashes(htmlentities($_POST['email'])); ?>" /><br />
<label for="f_sub" class="form_label">Subject:</label>
<input type="text" maxlength="255" id="f_sub" name="sub" value="<?php echo
stripslashes(htmlentities($_POST['sub'])); ?>" /><br />
<label for="f_msg" class="form_label">Message:</label>
<textarea rows="0" cols="0" id="f_msg" name="msg" ><?php echo
stripslashes(htmlentities($msg)); ?></textarea>
</fieldset>
<fieldset class="hideme">
<label for="fireinthehole">Checkbox for spam reduction</label>
<input type="checkbox" id="fireinthehole" name="fireinthehole" value="1" />
</fieldset>
<fieldset>
<input type="hidden" name="validate" value="yes" />
<input type="submit" value="Submit" class="submit" />
</fieldset>
</form>
<?php } /* end if( !$validated ) */ ?>