Email notification when user submits using addForm.php

15 posts by 4 authors in: Forums > CMS Builder
Last Post: June 4, 2012   (RSS)

By JeffC - September 27, 2010

I have set up a form on my site using addForm.php that Dave has provided on this forum. I have got the form working correctly and data is populating the database as it should. I would like to add a function that alerts me whenever someone uses the form. Ideally a duplicate of all of the information to be sent to a email address.

Here is my code
<?php
require_once "cmsAdmin/lib/viewer_functions.php";

// submit form
if (@$_REQUEST['submit']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['title']) { $errorsAndAlerts .= "Please enter your name<br/>\n"; }
if (!@$_REQUEST['email']) { $errorsAndAlerts .= "Please enter your email address<br/>\n"; }


// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}contacts` SET
title = '".mysql_real_escape_string( $_REQUEST['title'] )."',
position = '".mysql_real_escape_string( $_REQUEST['position'] )."',
company_name = '".mysql_real_escape_string( $_REQUEST['company_name'] )."',
type_of_business = '".mysql_real_escape_string( $_REQUEST['type_of_business'] )."',
telephone = '".mysql_real_escape_string( $_REQUEST['telephone'] )."',
email = '".mysql_real_escape_string( $_REQUEST['email'] )."',
address = '".mysql_real_escape_string( $_REQUEST['address'] )."',
postcode = '".mysql_real_escape_string( $_REQUEST['postcode'] )."',
country = '".mysql_real_escape_string( $_REQUEST['country'] )."',
area_of_interest = '".mysql_real_escape_string( $_REQUEST['area_of_interest'] )."',
newsletter_sign_up = '".(@$_REQUEST['newsletter_sign_up'] ? '1' : '0')."',

createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

// display thanks message and clear form
$errorsAndAlerts = "Thanks, we have received your entry";
$_REQUEST = array();
}

}

?>

<form method="post" action="">
<input type="hidden" name="submit" value="1" />


<?php if (@$errorsAndAlerts): ?>
<div style="color: red; font-size: 0.8em; font-weight: normal;"><br/>
<?php echo $errorsAndAlerts; ?><br/><br/>
</div>
<?php endif ?>

<div style="font-weight: normal; font-size: 0.8em; line-height: 2.4em;"><br/>
<table border="0" cellspacing="0" cellpadding="0">
<tr>

<tr>
<td valign="top">Name</td>
<td><input type="text" name="title" value="<?php echo htmlspecialchars(@$_REQUEST['title']) ?>" size="30" /></td>
</tr>

<tr>
<td valign="top">Position</td>
<td><input type="text" name="position" value="<?php echo htmlspecialchars(@$_REQUEST['position']) ?>" size="30" /></td>
</tr>

<tr>
<td width="120" valign="top">Company Name</td>
<td><input type="text" name="company_name" value="<?php echo htmlspecialchars(@$_REQUEST['company_name']) ?>" size="30" /></td>
</tr>

<tr>
<td valign="top">Type of Business</td>
<td><input type="text" name="type_of_business" value="<?php echo htmlspecialchars(@$_REQUEST['type_of_business']) ?>" size="30" /></td>
</tr>

<tr>
<td valign="top">Telephone</td>
<td><input type="text" name="telephone" value="<?php echo htmlspecialchars(@$_REQUEST['telephone']) ?>" size="30" /></td>
</tr>

<tr>
<td width="120" valign="top">Email</td>
<td><input type="text" name="email" value="<?php echo htmlspecialchars(@$_REQUEST['email']) ?>" size="30" /></td>
</tr>


<tr>
<td valign="top">Address</td>
<td><input type="text" name="address" value="<?php echo htmlspecialchars(@$_REQUEST['address']) ?>" size="30" /></td>
</tr>

<tr>
<td valign="top">Postcode</td>
<td><input type="text" name="postcode" value="<?php echo htmlspecialchars(@$_REQUEST['postcode']) ?>" size="30" /></td>
</tr>

<tr>
<td width="120" valign="top">Country</td>
<td><input type="text" name="country" value="<?php echo htmlspecialchars(@$_REQUEST['country']) ?>" size="30" /></td>
</tr>

<tr>
<td width="120" valign="top">Area of Interest</td>
<td><input type="text" name="area_of_interest" value="<?php echo htmlspecialchars(@$_REQUEST['area_of_interest']) ?>" size="30" /></td>
</tr>
</table>
</div>

<div style="font-weight: normal; font-size: 0.8em; line-height: 1.6em;"><br/>
<table>
<tr>
<td valign="top">
Please tick if you wish to receive our monthly E-zine <input name="newsletter_sign_up" type="checkbox" value="1" <?php checkedIf(@$_REQUEST['newsletter_sign_up'], '1') ?>" />
</td>
</tr>
</table>
</div>

<div style="font-weight: normal; font-size: 0.8em; line-height: 1.6em;"><br/>
<input type="submit" name="add" value="Submit &gt;" />
</div>
</form>

Jeff

Re: [Jeffncou] Email notification when user submits using addForm.php

By Jason - September 27, 2010

Hi,

Try using this code below where you add information to the database:

$to="your_email@domain.com";
$subject="New Contact Added";
if(@$_REQUEST['newsletter_sign_up']){$_REQUEST['newsletter_sign_up']=1;}else{$_REQUEST['newsletter_sign_up']=0;}
$message=<<<__TEXT__
A new contact was added:

title:{$_REQUEST['title']}
position:{$_REQUEST['position']}
company_name:{$_REQUEST['company_name']}
type_of_business:{$_REQUEST['type_of_business']}
telephone:{$_REQUEST['telephone']}
email:{$_REQUEST['email'] }
address:{$_REQUEST['address']}
postcode:{$_REQUEST['postcode']}
country:{$_REQUEST['country'] }
area_of_interest:{$_REQUEST['area_of_interest'] }
newsletter_sign_up: {$_REQUEST['newsletter_sign_up']}
__TEXT__;

mail($to,$subject,$message);


You can change the value of $to to be whatever email address you want the message sent to.
It's important to note that the line __TEXT__; that's at the end of the message must be flush against the left hand side of your page. If there are any spaces, it will cause an error.

Give that a try.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Email notification when user submits using addForm.php

By JeffC - September 27, 2010

Hi Jason

Thanks for the response. Can you explain what you mean by 'after I add code to the database'. I have tried putting it directly after:

// submit form
if (@$_REQUEST['submit']) {


but it returns a syntax errors
Jeff

Re: [Jeffncou] Email notification when user submits using addForm.php

By Jason - September 27, 2010

Hi,

Try putting this code after this line:
$recordNum = mysql_insert_id();

What are the syntax errors you're receiving?

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Email notification when user submits using addForm.php

By JeffC - September 27, 2010

HI Jason

I have put the code where you suggest and get the following error

Parse error: syntax error, unexpected T_SL in /home/jeffncou/public_html/thegrandtour.eu/addForm.php on line 41
Jeff

Re: [Jeffncou] Email notification when user submits using addForm.php

By Jason - September 27, 2010

Hi,

Try replacing the $message variable with this?

$message=<<<__TEXT__
A new contact was added:

title:{$_REQUEST['title']}
position:{$_REQUEST['position']}
company_name:{$_REQUEST['company_name']}
type_of_business:{$_REQUEST['type_of_business']}
telephone:{$_REQUEST['telephone']}
email:{$_REQUEST['email'] }
address:{$_REQUEST['address']}
postcode:{$_REQUEST['postcode']}
country:{$_REQUEST['country'] }
area_of_interest:{$_REQUEST['area_of_interest'] }
newsletter_sign_up: {$_REQUEST['newsletter_sign_up']}

__TEXT__;


Again, make sure the __TEXT__; is right at the far left.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Email notification when user submits using addForm.php

By JeffC - September 27, 2010

Still no joy i'm afraid.
Jeff

Re: [Jeffncou] Email notification when user submits using addForm.php

By Jason - September 27, 2010

Hi,

If you could email your CMS login and FTP details to jason@interactivetools.com, I'll take a closer look.

Only email this information don't post it to the forum.

Thanks.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jeffncou] Email notification when user submits using addForm.php

By Jason - September 27, 2010

Hi Jeff,

I've got it fixed. What was happening is there was a blank space after "$message=<<<__TEXT__" that was causing the error, which is pretty hard to spot. Give it a try, it should be working for you now.


Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/