PHP form not sending data to email

I have coded 2 php pages from a Lynda.com tutorial by David Powers to send form data to an email. It works perfectly on the xampp setup on my machine. When I uploaded it to my server everything works until I click the submit button and then the form reverts back to its initial state with empty fields. It seems to me it should either give me an error message 'Sorry your mail could not be sent or it should bring up the ‘Thank you page’
Can someone please help. I’m a complete newbie to php and don’t know where to go for help


formTest.doc (40 KB)

mail_process.doc (27 KB)

ThankYou.doc (25.5 KB)

EDIT:******************
I looked at your code again. It appears that the mail-process file has nothing to do with the other two.
Unless you were attempting to merge the two. If so, just add that code into the top area of your test
file. Not really sure what you want us to help you with. It appears the test page should work.


Well, first, you should show your code here and only the parts that are needed. Since you are using a
sample from a tutorial, it would appear you are attempting to learn how it is done. So, looking at the files,
the first problem is that the form in your test page does not post to your PHP file. It posts to itself.
This is okay, but, you need the PHP code to be at the beginning of the test file, not a separate file. OR, you
need to point the action of your form to go to the PHP file instead of itself. For this small of a project, most
programmers would just do it all on on page. Note three pages. Unless you were just showing it to us as
separate sections. It appears it is three different files.

How have you debugged this project so far? Have you forced it to show some of the data to make sure it is
retrieving it from the form correctly? Have you placed a display at the beginning of the PHP code to see if it
actually gets there? A lot of the debugging is just running test after test displaying parts of your code to
see if it actually gets thru it all and with the data you think it should have. I do that often with simple data
displays and killing the code at that point to see if it is correct up to that point. You can do this with the DIE
function. So, just when the code is posted, you can display a variable and kill the code to see what was
really inside the variable. Something like:
echo “
” . $somevariable . “
”;
die( “GOT HERE…” );
Your code would display the variable in question and then stop. On the screen, you would see the data that
is inside the variable and nothing further. My guess is that it is just the 's action and it is not being
sent to the PHP code file, or a variable is not containing the correct data.

Test it some and try to see how the code is “flowing” thru the three files you sent us and if you can’t solve
it, post your code here inside the PHP tags for us to look at further… Good luck!

it is 3 separate php pages. In the top section of the formTest page is the statement
…require ‘./mail_process.php’; I was under the impression that was the code that refereed to the mail_ process page. When I run the formTest page in my xampp environment all the code seems to work. If a field is left blank and it is in the required array the error message comes on the form page. If the email is not the proper format the error message appears warning user it is not a valid format.When I hit the submit button the Thank you page opens. I tried testing with the script you suggested…
echo “
” . $somevariable . “
”;
die( “GOT HERE…” ); It came back with error message
Notice: Undefined variable: required in C:\xampp\htdocs\introducingphp\formTest.php on line 25 except when I used the first 2 variables in the formTest, $errors and $missing and then it displayed the same message above with ‘array’ on the next line.
I really don’t know if I running the test script you provided correctly, here is an example

<?php echo"
" . $required .""; die(); ?>

As I said I’m a complete newbie. I’m a Photoshop, Flash, and After Effects animator who is trying to do a web site for a wonderful 93 year old guy. I know some JavaScript, Html and ActionScript but have never worked with PHP. Please forgive me if I ask dumb questions but I don’t know where to go to get help with this

There should not be much code for a send mail script. Post it to the board in code tags.

<?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> <?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; } } ?> <?php echo"
" . $required .""; die(); ?> 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

New page

<?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> <?php $suspect = false; $pattern = '/Content-Type:|Bcc:|Cc:/i'; function isSuspect($val, $pattern, &$suspect) { if (is_array($val)) { foreach ($val as $item) { isSuspect($item, $pattern, $suspect); } }else { if (preg_match($pattern, $val)) { $suspect = true; } } } isSuspect($_POST, $pattern, $suspect); 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) { //echo '
' . print_r(error_get_last(), true) . '
'; // exit; //$errors['mailfail'] = true; $errors['mailfail'] = true; } }

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.

This doesn’t actually have to do with your code directly, but why aren’t you using something like a PayPal button for the payment? PayPal will send the info you need, the button is literally copy and paste.

I apologize to the entire forum for my dumb mistake. I can not find the blog rules anywhere and I really don’t know how to post my code to the board inside php tags.

I have taken your suggestion and trashed the Lynda.com code. I am presently working with one of the forms from HTML Form Guides.com but can’t get it to send the email.
If you will tell me how to post to the board. I will upload my code for help. I feel pretty useless that I can’t get this simple form to send the mail, but I will persevere until I get it right. Its for a great cause

There is a button labeled php. Click that and it gives code tags. Everything inside of the tags is parsed as code, syntax coloring, indentation, and all.

Also, KeaauBoy, hang in with us, we can help. No need to feel useless. The code you started with was, well,
a bit messy. There are hundreds of sites that will walk you thru this. The one I posted seemed simple and
explained each part. Please post it as Astonecipher mentioned between the PHP tags (php) button just
above where you type. We can help…

ErnieAlex,
You guys are great and I plan to stick with this forum until I get it right. I am posting the new code from
HTML Form Guides.com with the hope that there is a fairly simple way to get it to actually send the email. I read the comments on the download for the HTML Form Guides and it seems it worked immediately for some people.

[php]<?PHP
require_once("./include/fgcontactform.php");

$formproc = new FGContactForm();

//1. Add your email address here.
//You can add more than one receipients.
$formproc->AddRecipient(‘@dm*.com’); //<<—Put your email address here

//2. For better security. Get a random tring from this link: http://tinyurl.com/randstr
// and put it here
$formproc->SetFormRandomKey(‘CnRrspl1FyEylUj’);

if(isset($_POST[‘submitted’]))
{
if($formproc->ProcessForm())
{
$formproc->RedirectToURL(“thank-you.php”);
}
}

?>

Contact us Contact us
* required fields
<?php echo $formproc->GetErrorMessage(); ?>
Your Full Name*:
' maxlength="50" />
Email Address*:
' maxlength="50" />
Message:
<?php echo $formproc->SafeDisplay('message') ?>
[/php]

Well, I have not looked at the code inside the library, but, what you posted looks good so far. I will download
the library and test your code later tonight. Did it work for you as-is? If not, what error(s) are you getting so
far?

Another possible way would be with your own code for emails. I do not want to mix you up too much at this
point since you appear to have a working page. Well, let us know if it worked for you.

Oh, and you really shouldn’t place your email address in your posts. Normally, you should block out part of it
so robots can’t grab it. I will edit your post and remove the email addresses…

Thanks ErnieAlex

This code works exactly as it should in xampp, and also after I upload it to the server. When I hit the submit button it even brings up the thank you page but no body gets the email. I tested it with my clients email, my wife’s email and my email. Nothing has been received on any of the 3 emails. Do you mean no email when posting to this site or on my php page. I already get 120 emails a day as I have my own domain.

Do a simple

If (! mail(to,subject,message) )
echo"problem"

Hmmmm, Well, yes, keep it simple. Are you sure you are sending the correct data to the library? And, I do not
see where the code actually sends the email, it does process the form. Is the mail() function inside the library?

I have unexpected plans for many hours tonight and will look at it in more detail when I get back. I might
suggest doing it yourself and I will post sample code for that. But, the library appears to be more “robust”
and might help you in the future if you wish to expand on the code. Not sure until I get back and delve into
the library code to see what is missing… More when I return…

My computer crashed last Sunday 10_4 and I have not had email until today. Has anyone responded to my last post. EarnieAlex are you out there somewhere, you were such a great help.

Yes, KeaauBoy, we did post some things…

Also, I did get time yesterday to download that file and look at it’s code. It is basically all of the code on
the site that I posted for you already in place. To test this system, you just upload the one folder to your
server. You have to change one line which is in the contact-form-handler.php file. It is where you place
your email address. Then, go to your site and that folder and open the contact-form.html file in your
browser and see how it works. I tested and it worked just fine as-is. (Well, with my email address in it.)

Try it and let us know if it worked well for you. If it does, you can alter your current code to fit in with it’s
code. Merge the two and you should be all set. Let us know…

Its been so long since I posted to this forum, I forgot how to post to the board. I started a new contactForm and have 3 pages to post. Please remind me how to post to the board in the proper manner so I don’t break any more rules

Sorry, KeaauBoy, it is not so much as “rules” as just not being able to copy your stuff to our editors quickly to
save us time and speed up an answer to you…

Anyways, to post, you do as you have been. But, for code, place it inside the PHP tags. You press the PHP
button just above the typing area and it will put the tags in your form. THen, copy your code between them.
If you post more than one file, use a separate set of PHP tags to show each file separately. Also, it is very
handy if you type the name of the page just above the tagged area like this:

index.html
[php]
[/php]
contact-form-handler.php file:
[php]
[/php]
In this way, we can quickly copy and paste your code into our editors for testing for you… You can press
the PREVIEW button to see how your post will look before you commit to it.
Also, glad you got your computer back up and running!

Sponsor our Newsletter | Privacy Policy | Terms of Service