将信息(从表单)发送到数据库和电子邮件

时间:2011-10-03 22:06:53

标签: php database forms email

我有一个表单,允许用户在新产品发布时选择接收通知。目前,用户的信息被验证并发送到数据库。但是,我还需要将其发送到电子邮件地址。

两个脚本分开工作;然而,让他们一起工作证明是困难的。

 <?php
 //CHECK CAPTCHA IMAGE
 session_start();

 if( isset($_POST['submit'])) {
 if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'])) { 
    //          IF CAPTCHA CHECKS OUT, CONTINUE TO VALIDATE DATA.

    if( !isset($_POST['fname']) ||
        !isset($_POST['lname']) ||
        !isset($_POST['email']))
        {
            echo '<script type="text/javascript">';
            echo 'alert("Please go back and fill out the entire form.");';
            echo '</script>';
        }   

        //          CONNECT TO DATABASE

        $dbhost = 'DATABASE NAME';
        $dbuser = 'DATABASE USER';
        $dbpass = 'PASSWORD';
        $dbname = 'DATABASE NAME';
        $dbtable = 'TABLE NAME';
        $conn = mysql_connect($dbhost, $dbuser, $dbpass);

        if(! $conn)
        {
         die('Could not connect: ' . mysql_error() . '<br />');
        } else {
         //echo 'Connected successfully. <br />';
        }

        $selected = mysql_select_db($dbname,$conn);
        if(! $selected)
        {
         die('Could not connect: ' . mysql_error() . '<br />');
        } else {
         //echo 'Connected successfully. <br />';
        }

        $fname = mysql_real_escape_string(stripslashes($_POST['fname']));
        $lname = mysql_real_escape_string(stripslashes($_POST['lname']));
        $email = mysql_real_escape_string(stripslashes($_POST['email']));
        $today = date("Y-m-d H-i-s");

        if (mysql_query("INSERT INTO $dbtable(fname, lname, email, date) VALUES('$fname', '$lname', '$email', '$today')") != true)
        {
            echo ("ERROR: " . mysql_error() . "<br />");    
        } else { 
            //echo 'Thank you, your information has been entered into our database. <br />';

        }

        mysql_close($conn); // CLOSE DATABASE

        include('../thankyou.html');    

        unset($_SESSION['security_code']);  //END SESSION
} else //       IF CAPTCHA DOESN'T CHECK OUT, DISPLAY ERROR MESSAGE.

{
    echo '<script type="text/javascript">';
    echo 'alert("Sorry, you have provided an invalid security code.")';
    echo '</script>';
}
}

?>

我尝试将一个表单到电子邮件的脚本包含为“include”,我尝试将这两个脚本集成到一个脚本中,但到目前为止都没有。

任何想法都会非常感谢..谢谢!

3 个答案:

答案 0 :(得分:1)

对于电子邮件部分,只需使用mail功能,或者更好的是,使用经过良好测试的邮件程序库,例如PHPMailerSwift Mailer

答案 1 :(得分:0)

关闭mysql连接后尝试使用PHP邮件功能。例如......

$my_email =无论你发送的是什么地址。

$subject =电子邮件的主题行。

$message =电子邮件的内容,如果您愿意,可以包含您的变量和HTML格式。类似的东西:

$message = " $time (Central Time) \n
From: $visitor ($visitormail)\n
Message: $notes
";

$headers =标题信息,如:

$headers = "From: $visitormail \r\n" .
    "Reply-To: $visitormail \r\n" .
    'X-Mailer: PHP/' . phpversion();

...并发送电子邮件至...

mail("$my_email", $subject, $message, $headers);

然后您可以重定向到另一个页面:

header( "Location: http://example.com/thankyou.html");

答案 2 :(得分:-1)

<?php

    $dbhost = '';
    $dbuser = '';
    $dbpass = '';
    $dbname = '';
    $dbtable = 'webagents';
    //$conn = mysql_connect($dbhost, $dbuser, $dbpass);
    $conn = mysqli_connect($dbhost, $dbuser, $dbpass);
    //$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
    if(!$conn)
    {
     die('Could not connect: ' . mysqli_error() . '<br />');
    } else {
     //echo 'Connected successfully. <br />';
    }

    $selected = mysqli_select_db($conn,$dbname);
    if(! $selected)
    {
     die('Could not connect: ' . mysqli_error() . '<br />');
    } else {
     //echo 'Connected successfully. <br />';
    }

    $cname = mysqli_real_escape_string($conn, $_POST['compname']);
    $cperson = mysqli_real_escape_string($conn, $_POST['contperson']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $phone = mysqli_real_escape_string($conn, $_POST['phone']);
    $country = mysqli_real_escape_string($conn, $_POST['country']);

    if (mysqli_query($conn,"INSERT INTO $dbtable(compname, contperson, email, phone, country) VALUES('".$cname."','".$cperson."', '".$email."', '".$phone."', '".$country."')") != true)

    {
        echo ("ERROR: " . mysqli_error($conn) . "<br />");
        //die (mysqli_error($myConnection));    
    } else { 
        echo 'Thank you, your information has been entered into our database. <br />';
    }
    mysqli_close($conn); // CLOSE DATABASE

?>