Form needs to send data to email

Hi,

I have created quite a primitive form using php. It sends a message to the person who has filled it in saying thank you and it also sends a message to 2 different website owners to let them know it has been filled in.
I would also like the form to send the data from the form to one of the mail addresses. Can someone tell me how this can be done?

My code is as below:

<?php require_once("connection.php");
require_once("functions.php");
if(isset($_POST['submit'])){
$errors = array();
$requiredfields = array('first_name'=> 'Please enter your FIRST NAME','surname'=> 'Please enter your SURNAME','email'=> 'Please enter your E Mail ADDRESS','text'=> 'Please enter your current needs');
foreach($requiredfields as $fieldname => $requiredfieldsmessage){
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) || strlen(trim($_POST[$fieldname])) == 0) {
$errors[] = $requiredfieldsmessage;
}
}
 if(count($errors) == 0){
 $_POST = array_map('mysqli_prep', $_POST);
$first_name =  mysqli_prep($_POST['first_name']);
$surname =  mysqli_prep($_POST['surname']);
$email =   mysqli_prep($_POST['email']);
$text =  mysqli_prep($_POST['text']);
$query = "INSERT INTO form (first_name,surname,email,text)Values('{$first_name}','{$surname}','{$email}','{$text}')";
$result = mysqli_query($connection, $query);
$message="A contact form has been completed on solutionsinlaw.co.uk";
$subject="Solutionsinlaw contact form";
$subject2="Solutionsinlaw contact form";
$subject3="Solutionsinlaw contact form";
$message2="Thank you for contacting us at Solutionsinlaw. We will get back you as soon as possible.";
$message3="Thank you for contacting us at Solutionsinlaw. We will get back you as soon as possible.";
$email2=$email;

mail('firstemail@sky.com',$subject,$message);
mail($email2, $subject2, $message2);
mail('secondemail@solutionsinlaw.co.uk', $subject3, $message3);
header('Location: brief_return.php');
exit;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

<head>

<title>Contact Solutions in Law - Immigration Lawyers | USA Immigration Specialists London - UK </title>
<meta name="description" content="Contact London, UK based an experienced immigration law attorney firm - Solutions in Law, Specializing in U.S. immigration, Visas, Green Cards & Citizenship. Call 44 020 3178 5419 today!">
<meta name="keywords" content="USA Immigration, USA Visas, USA Immigration Lawyers,USA Green Cards, Green Cards, USA Citizenship, USA Migration, Live and Work in the USA, Work in the USA, US Immigration, US Visas, USA Immigration Attorneys, USA Residence">

    <meta HTTP-EQUIV="Content-Language" CONTENT="EN">
    <meta name="distribution" CONTENT="Global">
    <meta name="revisit-after" CONTENT="20 days">
    <meta name="robots" CONTENT="index,follow">
    <meta name="Googlebot" CONTENT="index,follow">
    <meta http-equiv='imagetoolbar' content='no'>
    <meta name='Rating' content='General'>
    <meta name='MSSmartTagsPreventParsing' content='true'>
    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
	
	<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-19198483-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>


<!-- CSS -->
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script type="text/javascript">
    function changeClass(id, clas)
    {
        document.getElementById(id).className = clas;
    }
</script>

</head>

<body>
<div id="content">

    <div class="header">
        <div class="floatleftt10"><a href="index.htm"><img src="images/logo.jpg" alt="US green cards, H1B, L1, B1, B2, visas, US Immigration"></a></div>
        <div class="floatrightt50"><p><span class="head_lgrey">TEL:</span> <span class="head_dgrey">(+44) 020 7969 2977&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="head_lgrey">EMAIL:</span> <span class="head_dgrey">info@solutionsinlaw.co.uk</span></p>
        <div class="social"><a href="http://www.facebook.com/pages/Solutions-In-Law-Ltd/332398416821864" target="_blank" title="Like us on Facebook"><img src="images/facebook.jpg" /></a>&nbsp;<a href="https://twitter.com/NicoleEdwards26" target="_blank" title="Follow us on Twitter"><img src="images/twitter.jpg" /></a>&nbsp;<a href="http://uk.linkedin.com/pub/nicole-edwards/37/27a/58" title="Find us on Linked in" target="_blank"><img src="images/linkedin.jpg" /></a>&nbsp;&nbsp;</div></div>

        <div class="clear5"></div>
           <div class="bg_head_menu">
            <div class="home"><a href="index.htm" class="menu_links">Home</a></div>
            <div class="about"><a href="about_solutions_in_law.htm" class="menu_links">About Us</a></div>
			<div class="recruitement"><a href="international_clients.html" class="menu_links">International Clients</a></div>
			<div class="recruitement"><a href="recruitment.htm" class="menu_links">Recruitment</a></div>
            <div class="recruitement"><a href="testimonials.html" class="menu_links">TESTIMONIALS</a></div>
             <div class="recruitement"><a href="http://www.solutionsinlaw.co.uk/usvisa/usimmigrationlawyer/" class="menu_links">Latest News</a></div>
            <div class="contact"><a href="contact.htm" class="menu_links">Contact US</a></div>
			<div class="contact"><a href="http://www.solutionsinlaw.co.uk/german_index.htm" class="menu_links">German Version</a></div>
					

        </div>
    </div>
    <div class="clear20"></div>
    <div class="site_content">
        <div class="col_left">
            <div><img src="images/main_img.jpg" alt="USA Immigration Attorneys, Employment, Business, Family and Education USA Visas" /></div>
            <div class="clear10"></div>
            <div class="floatleft"><h1 align="center">How can we help you?</h1>
            <p class="smalltext" style ="color:green">Our UK lawyers offer initial personal advice free of charge. Call 020 7989 2977<br> If you prefer you may </span> <a href="mailto:myemail" class="maintextlink">email us</a> or fill out the website form below and we will get back to you as soon as possible:</p></div>
            <div class="clear"></div>
            <div class="smalltext">

<div><?php
if(count($errors) != 0){
echo '<h3>Sorry, please address the following errors:-</h3>';
echo '<ul>';
foreach ($errors as $error) {
echo '<li>' . $error . '</li>';}echo '</ul>';
}
?>
<form style="padding:15px;" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >

<p>First Name:<br/> <input type="text" size="30" name="first_name" value="<?php echo htmlspecialchars($_POST['first_name'],ENT_QUOTES); ?>" id="fname" /></p>
<p> Surname:<br/><input type="text" size="30" name="surname" value="<?php echo htmlspecialchars($_POST['surname'],ENT_QUOTES); ?>" id="sname" /></p>
<p>E mail:<br/><input type="text" size="40" name="email" value="<?php echo htmlspecialchars($_POST['email'],ENT_QUOTES); ?>" id="email" /></p>
<p>How Can I help you?<br/>
  <textarea rows="10" cols="40" name="text"> <?php echo htmlspecialchars($_POST['text'],ENT_QUOTES); ?> </textarea></p><br />
  <input type="submit" name="submit" value="Submit " />
</form></div>

Thanks for your help!

so what is working and what is NOT working ?

Its all working. as I say I wan the form to send the information the form-filler puts into form sent in an email to the website owner. How can I do this? at the moment the email only notifies the website owner that a form has been filled in.

Put this in one file and have a play with it.


<?PHP
$subject = 'My subject';

$message = var_export($_POST, 1);

// OR uncomment these lines and use concatenation to build a string
// $message = $_POST['a_test'];
// $message .= $_POST['another_test'];  // <- note the .= (no space between them now)

mail('firstemail@sky.com',$subject,$message);
?>
<form action="" method=POST>
<input type=text name="a_test" value= "this is a test" />
<input type=text name="another_test" value= "this is another test" />
<input type=submit />
</form>

Essentially, its that.

You will want to pick off the POST values you want and filter their content carefully for bad things, and assemble readable output.