Question

I'm creating a database system where the user can add details within a form and it adds to the database with php. What I want to do however is to send a confirmation email which will also send some of the details within the form. So, when I add details of a student for instance that an email will be send to me that says something along the lines of "A student called "Bob Dabilda" has been added to your database by "agentname"" - I have no problem doing the agent name part since I can just send the session name through, what I have problems is sending forward the student name. Any help appreciated. The following is a version of the add student php part. The adding student form works and the email works separately but I want the email to be sent without the agent having to click a button that says "send email" or anything like that. The database is mysql.

require "dbc.php";
session_start();

if(isset($_POST['submit']))
{
$sname = mysql_real_escape_string($_POST['shostfamilyid']);

if($sname && $ssurname && $sgender && $saddress && $sdob && $sblood && $spassport && $shobby1 && $shobby2 && $schildren && $sanimals)
{

    $query = mysql_query("INSERT INTO student VALUES ('','$sname','$ssurname','$sgender','$saddress','$semail','$sdob','$stelephone','$sdiet','$sallergy','$smed','$sblood','$spassport','$shobby1','$shobby2','$shobby3','$ssiblings','$schildren','$srequests','$sanimals','$agent','')");
                header("location: successstudent2.php");
}
    else
    { 
       echo "Check All Required Fields Are Completed"; 
    }
}

and this is a copy of the email script that I have working

session_start();

error_reporting(0);

if($_SESSION['username'])
{

$to = "from@from.com";
$from = "receive <to@to.com>";

$name = $_POST['webmaster'];
$email = $_POST['email@email.com'];
$msg = $_POST['msg'];
//generic subject
$subject = "Student Adding";
//header
$header = 'From: '. $from . "\r\n" . 'Content-Type: text/html; charset=ISO-8859-1';
//email
$htmlEmail = "
<html>
<head>
</head>
<body>
<font face='Arial' style='font-size:14px;'></font><br>
<font face='Arial' style='font-size:14px;'>A Student was added by ".$_SESSION['username']."   
</font><br><br>
</body>
</html>";
//php mail function
mail($to,$subject,$htmlEmail,$header);
header('Location:members.php'); //forwards user to members.php }

Any help would be appreciated. Thanking you!

Was it helpful?

Solution

You will need to double check all variable names and data being passed below, as this is just an example and probably not functional as-is.

Create yourself a function in a common file, for example (say this is "functions.php"):

<?php
function send_email ($to, $from, $subject, $body) {
/* you'll want to modify the above variables to match what you need in the function */

$to = "from@from.com";
$from = "receive <to@to.com>";

$name = $_POST['webmaster'];
$email = $_POST['email@email.com'];
$msg = $_POST['msg'];
//generic subject
$subject = "Student Adding";
//header
$header = 'From: '. $from . "\r\n" . 'Content-Type: text/html; charset=ISO-8859-1';
//email
$htmlEmail = "
<html>
<head>
</head>
<body>
<font face='Arial' style='font-size:14px;'></font><br>
<font face='Arial' style='font-size:14px;'>A Student was added by ".$_SESSION['username']."   
</font><br><br>
</body>
</html>";

//php mail function
mail($to, $subject, $htmlEmail, $header);

}
?>

Then change the code that does the insert up a bit:

<?php
session_start();
require "dbc.php";
require "functions.php";

if (isset($_POST['submit'])) {

  $sname = mysql_real_escape_string($_POST['shostfamilyid']);

  if ($sname && $ssurname && $sgender && $saddress && $sdob && $sblood && $spassport && $shobby1 && $shobby2 && $schildren && $sanimals) {

    $query = mysql_query("INSERT INTO student VALUES ('','$sname','$ssurname','$sgender','$saddress','$semail','$sdob','$stelephone','$sdiet','$sallergy','$smed','$sblood','$spassport','$shobby1','$shobby2','$shobby3','$ssiblings','$schildren','$srequests','$sanimals','$agent','')");

    if ($query) {
      send_email ($to, $from, $subject, $body); /* modify these as well */
      header("location: successstudent2.php");
      exit();
    } else { 
       echo "Check All Required Fields Are Completed"; 
    }
  }
}
?>

OTHER TIPS

I say forget the function or separate page, and just include the PHP script to send an email directly below the INSERT statement. You shouldn't need to use the "if($_SESSION['username'])", which may be causing your problem anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top