cazlouise cazlouise - 1 year ago 67
HTML Question

how to send a contact form by email when user submits forms

i have created a contact form, i am now trying to send the contact form as an email once the user has clicked in the submit button. but once the user clicked on submit i do not receive any emails. i have included my html form. i am aware that i need php and maybe javascript codes in order to make the email work but i do not know how to start this process.

i could do with some help.

<div id="details">
<form method="post" name="Products" action="">
Please leave your name: <br>
<input type="text" name="firstname"> <br>
Email: <br>
<input type ="email" name ="email">

<div id="detailsSecond">
Please leave us a description of your artwork brief: <br>
<textarea name = "textarea" rows="10" cols="50">Write something here</textarea>


<input type="submit" value="Submit">


Answer Source

Try this it should work perfectly.


        $to="Your email address";


        $errors="";//counting errors


                if(empty($_POST['yourname'])){ //name field empty

                        $nameError="Please enter your name";
                        $errors++; // increament errors

                        $name= UserInput($_POST['yourname']);

                        if(!preg_match("/^[a-zA-Z ]*$/", $name)){

                                $nameError="Only letters and white space accepted";



                        $emailError="Enter email";

                        $email = UserInput($_POST['email']);

                        if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)){

                                $emailError="Invalid Email";


                        $errMsg="Enter message";


                if($errors <=0){//No errors lets setup our email and send it

                         $headers = "MIME-Version: 1.0" . "\r\n";
                         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                         $headers .= 'From: <' . $email . '>' . "\r\n";

                        $text  = "<p>New Message from $name </p>";
                        $text .= "<p>Name : $name</p>";
                        $text .= "<p>Email : $email</p>";
                        $text .= "<p>Message : $message</p>";

                        mail($to, "Website Contact", $text, $headers);
                        $success="Thank your message was submitted";
                        $_POST= array(); //clearing inputs fields after success



 //Filter user input
function UserInput($data){

        $data = trim($data);
        $data = stripcslashes($data);
        $data = htmlspecialchars($data);
        return $data;



        <?php echo $success;?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SEL"]);?>">

        <span style="color:#f00;"><?php echo $nameError;?></span>
        <input type="text" name="yourname" placeholder="enter your name" <?php if(!empty($_POST['yourname'])){echo "value=\"".$_POST['yourname']."\"";}?>><br>

        <span style="color: #f00;"><?php echo $emailError;?></span>
        <input type="email" placeholder="your email" name="email" <?php if(!empty($_POST['email'])){echo "value=\"".$_POST['email']."\"";}?>><br>

        <span style="color: #f00;"><?php echo $errMsg;?></span>
        <textarea name="msg"><?php if(!empty($_POST['msg'])){echo $_POST['msg'];}?></textarea><br>

        <input type="submit" name="send" value="send">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download