Hi All,
I have this php code that inserts new record into my sql.
I need some help on:
upon submitting the record send email to my email account.
Here is my insert.php code:
/////////////////////insert.php//////////////////////////
<?php require_once('Connections/psycdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form2")) {
$insertSQL = sprintf("INSERT INTO helpu (id, date_created, sunetid, first_name, last_name, email, phone, Reported_issue) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['id'], "int"),
GetSQLValueString($_POST['date_created'], "date"),
GetSQLValueString($_POST['sunetid'], "text"),
GetSQLValueString($_POST['first_name'], "text"),
GetSQLValueString($_POST['last_name'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['phone'], "text"),
GetSQLValueString($_POST['Reported_issue'], "text"));
mysql_select_db($database_psycdb, $psycdb);
$Result1 = mysql_query($insertSQL, $psycdb) or die(mysql_error());
}
?>
<style type="text/css">
.heading-1 {
text-align: center;
font-size: 18px;
}
</style>
<p> </p>
<p> </p>
<div class="heading-1">Psyc DB</div>
<form name="form1" method="post" action="">
</form>
<form method="post" name="form2" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td nowrap align="right">Sunetid:</td>
<td><input type="text" name="sunetid" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">First_name:</td>
<td><input type="text" name="first_name" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Last_name:</td>
<td><input type="text" name="last_name" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Email:</td>
<td><input type="text" name="email" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Phone:</td>
<td><input type="text" name="phone" value="" size="32"></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Reported_issue:</td>
<td><textarea name="Reported_issue" cols="55" rows="6"></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insert record"></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form2">
</form>
<p> </p>
<p> </p>
/////////////////////////////////////////
Thanks for any help,
Abby
on Submit send email
Moderator: General Moderators
Re: on Submit send email
Have you looked at the mail() function at all? What have you tried? What went wrong?
Also, syntax tags.
Also, syntax tags.
Re: on Submit send email
Yes, I did and it is overwhelming.
I just start learning PHP.
I could not find any area that writes about submitting record \sending email.
Correct me if I’m wrong, At this point I think I have to have two files one is HTML for email function and one for PHP to deal with submitting record to MYSQL.
So far I found this code:
//////////////////////////////
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
////////////////////////////////////
IS this a right one to start with?
I'll be runing this code on MAC OSX Lino Server with Email and PHP Enabled.
Thanks in advnace,
Abby
I just start learning PHP.
I could not find any area that writes about submitting record \sending email.
Correct me if I’m wrong, At this point I think I have to have two files one is HTML for email function and one for PHP to deal with submitting record to MYSQL.
So far I found this code:
//////////////////////////////
<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
////////////////////////////////////
IS this a right one to start with?
I'll be runing this code on MAC OSX Lino Server with Email and PHP Enabled.
Thanks in advnace,
Abby
Re: on Submit send email
The code in your second post looks fine, but I don't see why you feel the need to have a second file for sending mail. You can just place the mail() call in the same block of code that processes the form.
Code: Select all
if (!empty($_POST))
{
// form-processing stuff here
mail($to, $subject, $message, $headers);
etc.
}Re: on Submit send email
Well it works now!!
here is the code:
//////////////////////////
<?php require_once('Connections/psycdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO helpu (sunetid, first_name, last_name, email, phone, Reported_issue, Status) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['sunetid'], "text"),
GetSQLValueString($_POST['first_name'], "text"),
GetSQLValueString($_POST['last_name'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['phone'], "text"),
GetSQLValueString($_POST['Reported_issue'], "text"),
GetSQLValueString($_POST['Status'], "text"));
mysql_select_db($database_psycdb, $psycdb);
$Result1 = mysql_query($insertSQL, $psycdb) or die(mysql_error());
mail('youremail@yahoo.com', 'Subject', '<p>HTML message - <a href="#">Link</a></p>', "From: Notification <test@yahoo.com>\r\nContent-Type: text/html\r\n");
$insertGoTo = "thanks.html";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_psycdb, $psycdb);
$query_Recordset1 = "SELECT * FROM helpu";
$Recordset1 = mysql_query($query_Recordset1, $psycdb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<style type="text/css">
.heading1 {
text-align: center;
color: #FF0;
font-size: 24px;
}
body {
background-image: url(image/background.jpg);
}
.heading1 form table tr td {
color: #FFF;
}
</style><link href="insert_heading.css" rel="stylesheet" type="text/css">
<div class="heading1">
<p>*********************</p>
<p> </p>
</div>
<div class="heading1">
<p>*******************</p>
<p>*********</p>
</div>
<div class="heading1">
<p> </p>
<p class="heading_Insert"> </p>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td width="97" align="right" nowrap>Sunetid:</td>
<td width="435"><input type="text" name="sunetid" value="" size="15"></td>
<td width="435">Status:
<input name="Status" type="text" value="New" size="15" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">First_name:</td>
<td><input type="text" name="first_name" value="" size="15"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Last_name:</td>
<td><input type="text" name="last_name" value="" size="15"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Email:</td>
<td><input type="text" name="email" value="" size="25"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Phone:</td>
<td><input type="text" name="phone" value="" size="25"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Reported_issue:</td>
<td><textarea name="Reported_issue" cols="50" rows="6"></textarea></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td> </td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insert record"></td>
<td> </td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
<p class="heading_Insert"> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p> </p>
<?php
mysql_free_result($Recordset1);
?>
//////////////////
Well, the next challenge for me is how to add the fields name\contents into the email subject?
Can someone assist me with this please?
Thanks in advance,
here is the code:
//////////////////////////
<?php require_once('Connections/psycdb.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO helpu (sunetid, first_name, last_name, email, phone, Reported_issue, Status) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['sunetid'], "text"),
GetSQLValueString($_POST['first_name'], "text"),
GetSQLValueString($_POST['last_name'], "text"),
GetSQLValueString($_POST['email'], "text"),
GetSQLValueString($_POST['phone'], "text"),
GetSQLValueString($_POST['Reported_issue'], "text"),
GetSQLValueString($_POST['Status'], "text"));
mysql_select_db($database_psycdb, $psycdb);
$Result1 = mysql_query($insertSQL, $psycdb) or die(mysql_error());
mail('youremail@yahoo.com', 'Subject', '<p>HTML message - <a href="#">Link</a></p>', "From: Notification <test@yahoo.com>\r\nContent-Type: text/html\r\n");
$insertGoTo = "thanks.html";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
mysql_select_db($database_psycdb, $psycdb);
$query_Recordset1 = "SELECT * FROM helpu";
$Recordset1 = mysql_query($query_Recordset1, $psycdb) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?>
<style type="text/css">
.heading1 {
text-align: center;
color: #FF0;
font-size: 24px;
}
body {
background-image: url(image/background.jpg);
}
.heading1 form table tr td {
color: #FFF;
}
</style><link href="insert_heading.css" rel="stylesheet" type="text/css">
<div class="heading1">
<p>*********************</p>
<p> </p>
</div>
<div class="heading1">
<p>*******************</p>
<p>*********</p>
</div>
<div class="heading1">
<p> </p>
<p class="heading_Insert"> </p>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
<table align="center">
<tr valign="baseline">
<td width="97" align="right" nowrap>Sunetid:</td>
<td width="435"><input type="text" name="sunetid" value="" size="15"></td>
<td width="435">Status:
<input name="Status" type="text" value="New" size="15" /></td>
</tr>
<tr valign="baseline">
<td nowrap align="right">First_name:</td>
<td><input type="text" name="first_name" value="" size="15"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Last_name:</td>
<td><input type="text" name="last_name" value="" size="15"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Email:</td>
<td><input type="text" name="email" value="" size="25"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Phone:</td>
<td><input type="text" name="phone" value="" size="25"></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right">Reported_issue:</td>
<td><textarea name="Reported_issue" cols="50" rows="6"></textarea></td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td> </td>
<td> </td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td><input type="submit" value="Insert record"></td>
<td> </td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
<p class="heading_Insert"> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
<p> </p>
<?php
mysql_free_result($Recordset1);
?>
//////////////////
Well, the next challenge for me is how to add the fields name\contents into the email subject?
Can someone assist me with this please?
Thanks in advance,