Jump to content

mail() script to insert and email - please help!


Mr Chris

Recommended Posts

Hi Guys,

I’m trying to put together a script for a form which does two things:

1)Inserts data into a database
2)Emails a confirmation message to the person signing-up to the form

[code=php:0]
<?php

// ** Connect to DB **
include("**************");


// ** If Submit is hit do your stuff **
    if (isset($_POST['submit'])) {

    $your_name = $_POST['your_name'];
    $your_email = $_POST['your_email'];
    $story_type = $_POST['story_type'];
    $text_html = $_POST['text_html'];

// ** Check for Required Fields with IF statements **
      if (empty($your_name)){
        $error = "** You forgot to enter your name! **";
    }  else if (empty($your_email)){
        $error = "** Error: You forgot to enter your email! **";
    }  else if (empty($story_type)){
        $error = "** Error: You forgot to enter the story type you wish to get alerts to! **";
    }  else if (empty($text_html)){
        $error = "** Error: You forgot to specify if you wanted a plain text or html email! **";

// ** If all of the statements are true then **
    } else {
    $query = "INSERT INTO emails(your_name,your_email,story_type,text_html) VALUES
    ('$your_name','$your_email','$story_type','$text_html')";
    $query = mysql_query($query);
    $id=mysql_insert_id();
    header("Location: form_newest.php?id=$id");

// ** And finally run the query to add data to the database **
    $link = mysql_connect;
    mysql_select_db($db);
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    mysql_close();
}


// Send html autoresponder email or whatever
$to = $your_email;
$subject = "Confirmation of Email Alert ";
$message = "
<html>
<head>
<title></title>

</head>
<body>
<font style=\"font-family:tahoma, arial, verdana;font-size:10pt\">
Hi, $your_name,
<p>Thank you for your Registering for $story_type email alerts. Your request has been successful and your first $story_type email alerts will be
arriving to your inbox soon!</p>
<p>Many Thanks,</p>
<p>Me</p>
 
</font>
</body>
</html>
";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .="From: [email protected]\r\nContent-type: text/html";

mail($to, $subject, $message, $headers);
// Done sending autoresponder

}
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<FORM ACTION="".php" method="post" NAME="frmAdd">
<b><?php echo $error; ?></b><br />
Your Name:<input type="text" name="your_name" size="35" value=""><br>
Email:<input type="text" name="your_email" size="35" value=""><br>
Type:<input type="text" name="story_type" size="35" value=""><br>
Text/HTML:<input type="text" name="text_html" size="35" value=""><br>
<input type="submit" name="submit" value="">

</form>
</body>
</html>
[/code]

Now this inserts data into the database, but does [b]NOT[/b] send a confirmation email.  However if I strip out the 'insert into' query from the above, and just have the confirmation bit of the code it [b]DOES[/b] send an email !:

[code=php:0]
<?php

// ** Connect to DB **
include("***********");


// ** If Submit is hit do your stuff **
    if (isset($_POST['submit'])) {

    $your_name = $_POST['your_name'];
    $your_email = $_POST['your_email'];

// Send html autoresponder email or whatever
$to = $your_email;
$subject = "Confirmation of Email Alert ";
$message = "
<html>
<head>
<title></title>

</head>
<body>
<font style=\"font-family:tahoma, arial, verdana;font-size:10pt\">
Hi, $your_name,
<p>Thank you for your Registering for $story_type email alerts. Your request has been successful and your first $story_type email alerts will be
arriving to your inbox soon!</p>
<p>Many Thanks,</p>
<p>Website</p>
 
</font>
</body>
</html>
";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .="From: me@xxyyzz\r\nContent-type: text/html";

mail($to, $subject, $message, $headers);
// Done sending autoresponder

}
?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">
<FORM ACTION="".php" method="post" NAME="frmAdd">
<b><?php echo $error; ?></b><br />
Your Name:<input type="text" name="your_name" size="35" value=""><br>
Email:<input type="text" name="your_email" size="35" value=""><br>
Type:<input type="text" name="story_type" size="35" value=""><br>
Text/HTML:<input type="text" name="text_html" size="35" value=""><br>
<input type="submit" name="submit" value="">

</form>
</body>
</html>
[/code]

So can anyone please advise why the top script will not both insert into the database AND send a email confirmation message?

Many Thanks

Chris
You are redirecting the page after the insert.

[code]
<?php

$query = "INSERT INTO emails(your_name,your_email,story_type,text_html) VALUES
    ('$your_name','$your_email','$story_type','$text_html')";
    $query = mysql_query($query);
    $id=mysql_insert_id();
    header("Location: form_newest.php?id=$id"); //<--- redirect

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.