将信息发送到数据库以及邮件

时间:2016-02-12 16:07:55

标签: php mysql forms email post

快一点(希望如此)......

我创建了一个目标网页,其中也有一个联系表单。 我想将联系表单发送到我已成功完成的数据库。但是,我想添加一个"谢谢你"消息说他们已成功提交表格。

我希望能够通过电子邮件发送通知,让他们知道谁和注册人的详细信息。

这是Process.php文件,我相信这就是我所需要的。

    <?php

    define('DB_NAME', 'cl54-the-acorn');
    define('DB_USER', 'cl54-the-acorn');
    define('DB_PASSWORD', '*******');
    define('DB_HOST', 'localhost');


    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    if (!$link) {
      echo "Error: Unable to connect to MySQL." . PHP_EOL;
      echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
      echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
      exit;
    }

    echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

    $query = "INSERT INTO users VALUES ('$_POST[forname]', '$_POST[surname]', '$_POST[email]', '$_POST[mobile]', '$_POST[telephone]', '$_POST[message]', '$_POST[title]')";
    $result = mysqli_query($link, $query);
    if ( false===$result ) {
      printf("There has been an error please contact the server admin: %s\n", mysqli_error($link));
    }
    else {
        header("Location: http://the-acorns.com/");
        die();
    }

    mysqli_close($link);

    ?>

2 个答案:

答案 0 :(得分:0)

        <?php 
    if(isset($_POST['submit'])){
        $to = "phil@adworks.uk.com"; // this is your Email address
        $from = $_POST['email']; // this is the sender's Email address
        $forname = $_POST['forname'];
        $last_name = $_POST['surname'];
        $subject = "Form submission";
        $message = $forname . " " . $surname . " wrote the following:" . "\n\n" . $_POST['message'];
        $message2 = "Here is a copy of your message " . $forname . "\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 " . $forname . ", we will contact you shortly.";
        // You can also use header('Location: thank_you.php'); to redirect to another page.
        // You cannot use header and echo together. It's one or the other.
        }
    ?>

答案 1 :(得分:0)

请在其他部分添加邮件功能。见php:mail function

示例:

$message="your customized message or whatever you want to send";
mail('$_POST[email]', 'Thank you for your feedback', $message);