KeaauBoy, seems that you never read our blog rules. Code should be placed INSIDE of the PHP tags.
This makes it much much easier for us to copy your code to our editors for testing to find a solution for you.
Since you did not do this, I did it for you here. Some notes on your code follows it near the bottom…
formTest
[php]
<?php
$errors = array();
$missing = array();
if (isset($_POST['send'])) {
$to = '
[email protected]';
$subject = 'Feedback from contact form';
$expected = array('name', 'address', 'state', 'zip', 'email', 'phone');
$required = array( 'name', 'address', 'state', 'zip', 'email' );
$headers = "From:
[email protected]\r\n";
$headers .= "Content-type: test/plain; charset=utf-8";
$authenticate = '
[email protected]';
require './mail_process.php';
if($mailSent) {
header('Location: thanks.php');
exit;
}
}
?>
Untitled Document
To receive your book please enter your mailing information
<?php if (($_POST && $suspect) || ($_POST && isset($errors['mailfail']))) { ?>
Sorry your mail could not be sent.
<?php } elseif ($errors || $missing) { ?>
Please fix the item(s) indicated.
<?php }?>
<div id="apDiv1">
Name:
<?php if ($missing && in_array('name', $missing)) { ?>
Please enter your name
<?php } ?>
>
Address:
<?php if ($missing && in_array('address', $missing)) { ?>
Please enter your address
<?php } ?>
>
State:
<?php if ($missing && in_array('state', $missing)) { ?>
Please select your state
<?php } ?>
selected>Select
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($state, ENT_COMPAT, 'utf-8') . '"';
}
?>
</select>
</p>
<p>
<label for="zip">Zip Code:
<?php if ($missing && in_array('zip', $missing)) { ?>
<span class="warning"> Please enter your Zip Code</span>
<?php } ?>
</label>
<input name="zip" type="text" id="zip" tabindex="20"
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($zip, ENT_COMPAT, 'utf-8') . '"';
}
?>
>
</p>
<p>
<label for="email">Email:
<?php if ($missing && in_array('email', $missing)) { ?>
<span class="warning"> Please enter your email address</span>
<?php } elseif (isset($errors['email'])) { ?>
<span class="warning">Invalid email address</span>
<?php } ?>
</label>
<input type="text" name="email" id="email" tabindex="40"
<?php
if ($errors || $missing) {
echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"';
}
?>
>
</p>
<p>
<label for="phone">Phone Number:
<?php if ($missing && in_array('phone', $missing)) { ?>
<span class="warning"> Please enter your phone Number</span>
<?php } ?>
</label>
<input type="text" name="phone" id="phone" tabindex="50" />
</p>
<p>
<input type="submit" name="send" id="send" value="Submit ">
</p>
</form>
</div>
Currently we do not have the capability of accepting Paypal or Credit Cards
[/php]
mail-process
[php]
if (!$suspect) {
foreach ($_POST as $key => $value) {
$temp = is_array($value) ? $value : trim($value);
if (empty($temp) && in_array($key, $required)) {
$missing[] = $key;
$$key = ‘’;
} elseif(in_array($key, $expected)) {
$$key = $temp;
}
}
}
if (!$suspect && !empty($email)) {
$validemail = filter_input(INPUT_POST, ‘email’, FILTER_VALIDATE_EMAIL);
if ($validemail) {
$headers .= “\r\nReply-to: $validemail”;
} else{
$errors[‘email’] = true;
}
}
if (!$suspect && !$missing && !$errors) {
$message = ‘’;
foreach ($expected as $item) {
if (isset($$item) && !empty($$item)) {
$val = $$item;
}else {
$val = ‘not selected’;
}
if (is_array($val)) {
$val = implode(’, ‘, $val);
}
$item = str_replace(array(’_’, ‘-’), ’ ', $item);
$message .= ucfirst($item) . “: $val\r\n\r\n”;
}
$message = wordwrap($message, 70);
$mailSent = mail($to, $subject, $message, $headers, $authenticate);
if(!mailSent) {
$errors['mailfail'] = true;
}
}
[/php]
thank-you
[php]
Thank You for Your Comments
Thank You
Your mailing info has been sent. We'll send your book as soon as we receive payment.
[/php]
Well, over the last few years, I have started to dislike Lynda.com as they seem to post a lot of code that
is not well-formed. You have so much code inside this mash-up where it checks validation in the middle of
live code. We have covered this subject many many times here. To recap, you normally grab all of your
inputs from your posted form. Then, you validate all of the data to insure it is not missing, valid formats and
safe for your database. Then, you display any messages and if none, handle sending the email and posting
any data to the database as needed. Your code intertwines it all and this makes it hard to follow and a bit
more work for the server.
So, the formtest page has PHP code to set up a validation call to routines that handle if an item posted is
missing. In the middle of it, you have the mail-processing file included which does basically the same with
ALL of the posted data. (note that this includes the submit button which does not need validating.) Since
the included file (mail-processing) is just 45 lines after removing some blank lines and does not include any
functions, the code in this file should just be placed in the main file. No need for the code to go out and get
it. Waste of the server’s time. In your form, you post to: action="<?php echo $_SERVER['PHP_SELF'];?>
This is not safe. It has been proved to be able to be hacked with ease by capturing the posted data. This
should be changed to just action="" ! This tells the server to post to itself and is the now preferred way to
handle it.
Here is a simple explanation of how to do it in a bit better manner. It does use JS for validation which I do
not use. (I do it all on the server.) But, you might see how more streamlined it is. (It does not match your
posted data, but, serves for a good example!)
http://www.html-form-guide.com/contact-form/php-email-contact-form.html
I feel that you would do much better with this type of layout instead of the current one.
Lastly, your current code, again from Lynda.com uses a lot of wasted code in JS to manipulate images for
hover-over changes. This is now done in just simple CSS. This tells me that the code you are using is very
out-dated…
Not sure if these comments really help you or not. Hopefully they do. There are so many ways to do code
to solve a project. Each must find the best way for them. If you decide to stay with your version, let us all
know and we can try to find your errors.