开发者

Send information (from a form) to a database and email

开发者 https://www.devze.com 2023-04-10 03:13 出处:网络
I have a form that allows a user to opt in to receive a notification if a new product comes out. Currently, a users\' information is validated and sent to a database. However, I also need it to be sen

I have a form that allows a user to opt in to receive a notification if a new product comes out. Currently, a users' information is validated and sent to a database. However, I also need it to be sent to an email address.

Both of the scripts work separately; getting them to work together is proving difficult, though.

 <?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'开发者_StackOverflow, '$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>';
}
}

?>

I've tried including a form-to-email script as an "include" and I've tried integrating the two scripts into one, but neither has worked so far.

Any thoughts would be greatly appreciated.. Thank you!


For the email portion, simply use the mail function or, better yet, use one of the well-tested mailer libraries such as PHPMailer and Swift Mailer.


Try using the PHP mail function after you close the mysql connection. For example...

$my_email = Whatever address you'd like this sent to.

$subject = Subject line of email.

$message = The content of the email, this can contain your variables and html formatting if you wish. Something like:

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

$headers = The header info, something like:

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

...and send the email with...

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

and then you can redirect to another page with:

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


<?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

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号