Email form after validation?

Hello Everyone,
I have been trying now for days to work this out and am just no closer! All I am trying to do is to email a form to myself once it has been validated. The validation is working fine but once validated I have no idea how to then email it to myself? I was looking for a line of PHP that would be at the end of the validation code and say if validation has passed then send the data in an email? Well the easiest way to be honest I just want to be able to send the validated form! Code is below and any help is really appreciated:

[code]<?php include 'contact-engine.php';?>

<style type="text/css">
	.asterix{color: #bc2021;font-size: 20px;}
	.border:focus{border: 2px solid #bc2021;}
	.error {color: #FF0000;}
	#marketing{border: 1px solid black; width: 150px; height: 22px;}
	#submit{width: 100px; height: 25px; font-size: 15px;}
	#submit:active{background-color: #bc2021;}
<p><span class="error">* required field.</span></p><br />

<!--Name--><div class="contact-font" style=" margin-top: 20px;">
    <span class="asterix">* </span>Name:<br />
    <input type="text" name="name" class="border" size="25" value="<?php if(isset($_POST['name'])) {echo $_POST['name']; } ?>">
    <span class="error"><?php echo $nameErr;?></span>

<!--Email--><div class="contact-font" style=" margin-top: 20px;">
    <span class="asterix">* </span>Email: (please double check entry)<br />
	<input type="text" name="email" class="border" size="25" value="<?php if(isset($_POST['email'])) {echo $_POST['email']; } ?>"><span class="error">
	<?php echo $emailErr;?></span>

<!--Subject--><div class="contact-font" style=" margin-top: 20px;">
    Subject:<br />
    <input type="text" name="subject" class="border" size="25" value="<?php if(isset($_POST['subject'])) {echo $_POST['subject']; } ?>">

<!--Message--><div class="contact-font" style=" margin-top: 20px;">
    <span class="asterix">* </span>Message:<br />
    <textarea cols="40" rows="10" name="message" class="border"><?php if(isset($_POST['message'])) {echo $_POST['message']; } ?></textarea>
    <span class="error"><?php echo $messageErr;?></span> 
</div><br />

    <select name="Question">
        <option <?php if($menuVar=="----------") echo 'selected="selected"'; ?> value="----------">----------</option>
        <option <?php if($menuVar=="WebSearch") echo 'selected="selected"'; ?> value="WebSearch">Web Search</option>
        <option <?php if($menuVar=="SocialMedia") echo 'selected="selected"'; ?> value="SocialMedia">Social Media</option>
        <option <?php if($menuVar=="Wordofmouth") echo 'selected="selected"'; ?> value="Wordofmouth">Word of mouth</option>
        <option <?php if($menuVar=="Other") echo 'selected="selected"'; ?> value="Other">Other</option>

	<input type="submit" value="Send" id="submit">

This is the included page (contact-engine.php):

// define variables and set to empty values
$nameErr = $emailErr = $subjectErr = $messageErr = $questionErr = “”;
$name = $email = $subject = $message = $WebSearch = $SocialMedia = $WordOfMouth = $Other ="";

if (empty($_POST[“name”]))
{$nameErr = “Name is required”;}
$name = test_input($_POST[“name”]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
$nameErr = “Only letters and white space allowed”;

if (empty($_POST[“email”]))
{$emailErr = “Email is required”;}
$email = test_input($_POST[“email”]);
if (!preg_match("/([\w-]+@[\w-]+.[\w-]+)/",$email))
$emailErr = “Invalid email format”;

if (empty($_POST[“subject”]))
{$comment = “”;}
{$comment = test_input($_POST[“subject”]);}

if (empty($_POST[“message”]))
{$messageErr = “A message is required”;}
{$comment = test_input($_POST[“message”]);}

if (isset($_POST[‘Question’]))
$menuVar = $_POST[‘Question’];
} else {
$menuVar = “----------”;}
function test_input($data)
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;

a form will automagically post to themselves so you don’t need the action.

you have the builtin mail function

but I would suggest using phpmailer instead, the built in mail function for one does not give you any errors.

Hello JimL,
Firstly, thank you very much for your help. I really am struggling here! I did php courses for about 4 months but realize I just need to do php courses for about 4 years as it appears I did not learn a lot at all :frowning:

Is the code I have not easily modified to email this form to myself? I have checked out the links but am a bit unsure of how the GitHub one works. If my code is not easily fixed what would you suggest is my best option?

your php code for now is only a field validation script. You were right in that you need to add some email function call after the code you have now. the mail function may look easier but setting it up to use phpmailer is really much better.

just look at the simple phpmailer examples. it should be pretty easy to do.

download the phpmailer class, include that file in your file, and use it as in the example.

Hi JimL,
Ok sounds good. I will work this one out as it seems a better solution to do it this way over my way (which has taken days and got me no-where! :D) I will leave you feedback of how I got on and also hopefully this will help other that are stuck on the same problem. Thank you very much for pointing me down the right path, I was considering removing the validation and just emailing the data without validating it until I understood what was wrong with the form. It will probably take about 4 days for me to get this working but I will do it ;D

Thank you for your time and help

Hello JimL,
I have tried to get the phpMailer to work but I just do not understand things enough to get this working. I have spent days already trying to figure this out and and am just totally lost! I have tried all over the internet for tutorial files and even youtube videos that can help me but I just do not understand.

Is it possible that I could pay you to do this for me? Perhaps if you have a paypal account I could pay you that way? Or if not maybe you know someone who can help me if I pay them? I basically cannot fix this problem :’(

Thank you for your help.

Mmm, the good old mail() function…
I spent a couple of hours yesterday setting up an email system for someone on here - code worked on one server but not another, ended up scrapping it and using phpmailer…
see here


All done :slight_smile:

Hi Redscouse,
Thank you so much for sorting this form out for me. PHPMailer is all set up, my mails are being directed to my personal inbox, all code is commented, the captcha is working, domain forwarding is working and everything else you fixed along the way for me. I will look you up as soon as I am in need of more coding.
Margate ;D

You’re welcome, happy to have helped :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service