需要处理联系表格

时间:2016-05-19 04:02:00

标签: javascript php jquery

我购买了一个主题,我需要为我的联系表单制作一个processing.php。将数据发送到服务器进行处理的表单。

这是表格代码:

                <div class="form_error text-center">
                    <div class="name_error hide error">Please Enter your name</div>
                    <div class="email_error hide error">Please Enter your Email</div>
                    <div class="email_val_error hide error">Please Enter a Valid Email Address</div>
                    <div class="message_error hide error">Please Enter Your Message</div>
                </div>
                <div class="Sucess"></div>


                <form role="form">
                    <div class="row">
                        <div class="col-md-4">
                            <input type="text" class="form-control" id="name" placeholder="Name">
                            <input type="email" class="form-control" id="email" placeholder="Email">
                            <input type="text" class="form-control" id="subject" placeholder="Subject">
                        </div>


                        <div class="col-md-8">
                            <textarea class="form-control" id="message" rows="25" cols="10" placeholder="  Message Texts..."></textarea>
                            <button type="button" class="btn btn-default submit-btn form_submit">SEND MESSAGE</button>
                        </div>
                    </div>
                </form>

3 个答案:

答案 0 :(得分:0)

见这个例子

<?php 
if(isset($_POST['submit'])){
    $to = "email@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>

<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html> 

答案 1 :(得分:0)

shubham和adi的答案都容易受到SMTP标头注入攻击,允许您的联系表单用于发送垃圾邮件。它会发生。

传递给additional_headers参数的任何POST或GET数据都需要进行清理,以防止滥用。

请参阅以下内容:

答案 2 :(得分:-1)

<?php

if (isset($_POST['contact_name'])&&isset($_POST['contact_mail'])&&isset($_POST['message'])) {
    $contact_name = $_POST['contact_name'];
    $contact_mail = $_POST['contact_mail'];
    $message = $_POST['message'];
    if (!empty($contact_name) && !empty($contact_mail) && !empty($message)) {
        $to = 'your email@gmail.com'; // this is where you want to send the email
        $subject = 'Contact form Submitted';
        $body = $contact_name."\n".$message;
        $headers = 'From:'.$contact_mail; // email of the sender

            if (@mail($to, $subject, $body, $headers)) {
                echo 'Thanks for contacting us';
            }
            else {
                echo 'Sorry, an error has been occurred';
            }

    }
    else {
        echo 'All fields are required';
    }
}



?>

<form action="index.php" method="POST">
    Name:<br>
    <input type="text" name="contact_name"></input><br><br>
    Email Address:<br>
    <input type="text" name="contact_mail"></input><br><br>
    Message:<br>
    <textarea name="message" cols="30" rows="6"></textarea><br><br>
    <input type="submit" value="Send"></input>


</form>

也许这些代码可以帮助你