Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2010-10-23 04:13:05
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
[howto] How to Save zem_contact_reborn Submissions to a MySQL Database
Not usually one for promoting my own articles on this forum, but I think this might be useful to a lot of people on here: Save zem_contact_reborn Submissions to a Database published on graphicpush.com. This is only part 1. I welcome any feedback on the PHP I used; not being an expert, I am sure it could be optimized.
Kevin
(graphicpush)
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
Copy/paste from the comment I posted there:
Clearing the residual values isn’t necessary, because you only use them upon form submission and in that situation the contents of the $_POST array are used instead.
Instead of htmlspecialchars, it’s better to use the doSlash function that TXP provides.
If you use the ‘submit’ callback event, which normally used by ZCR to call anti-spam plugins, and put the code you wrote in a plugin that is loaded after any anti-spam plugins, you benefit from both the built-in error checking and the anti-spam plugins that are loaded.
MySQL can figure out the current time all by itself. If that time is to be used within TXP later on, you may want to look at how TXP itself stores dates to keep the form submission timestamps consistent with the way TXP handles dates.
Offline
#3 2010-10-23 12:31:36
- kevinpotts
- Member
- From: Ghost Coast
- Registered: 2004-12-07
- Posts: 370
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
Clearing the residual values isn’t necessary, because you only use them upon form submission and in that situation the contents of the $_POST array are used instead.
Yes, I was not sure about that.
Instead of htmlspecialchars, it’s better to use the doSlash function that TXP provides.
How can I call that from this script?
If you use the ‘submit’ callback event, which normally used by ZCR to call anti-spam plugins, and put the code you wrote in a plugin that is loaded after any anti-spam plugins, you benefit from both the built-in error checking and the anti-spam plugins that are loaded.
Yeah. This is where my PHP knowledge falls apart. Like I understand what you’re saying, but I have no idea how to do that.
MySQL can figure out the current time all by itself. If that time is to be used within TXP later on, you may want to look at how TXP itself stores dates to keep the form submission timestamps consistent with the way TXP handles dates.
I will look into it. This is just what has been working for me.
Kevin
(graphicpush)
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
kevinpotts wrote:
How can I call that from this script?
Easiest (and imo best) is to turn the script into a plugin. Then you can use the helpful library TXP has to offer.
Yeah. This is where my PHP knowledge falls apart. Like I understand what you’re saying, but I have no idea how to do that.
By using TXP’s register_callback() function. ZCR provides couple callbacks you can hook to. It allows you to use all the possiblities that come with ZCR, like for example you can use get_zemcontact_evaluator()
to check if the message is spam, and let ZRC to handle the incoming data.
Basic example (not tested):
/**
Registers the callback. xxx_my_saving_function() is
now loaded on 'zemcontact.submit' event. You can find
the callback spot from ZRC's source and what it can offer.
*/
register_callback('xxx_my_saving_function','zemcontact.submit');
/**
The function that does the work on
the submit event
*/
function xxx_my_saving_function() {
/*
get_zemcontact_evaluator(); can
tell us if the comment is spam. If spam,
do not save the comment.
PS. Remember to load the spam plugins
before this plugin; set this
plugin's load order to '6' and the antispams to '5'.
We want to do the spam checks before this point.
*/
$evaluation =& get_zemcontact_evaluator();
/*
It's spam, end here
*/
if($evaluation->get_zemcontact_status() != 0)
return;
/*
Saving the data goes here etc..
$zem_contact_values global etc. can be
used to get the data and so on.
*/
global $zem_contact_values;
foreach($zem_contact_values as $name => $val) {
/* [...] */
}
}
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
You don’t need all those $evaluation
lines, because it’s not a spam plugin using the callback event. I meant something like this (not tested):
register_callback('xxx_my_saving_function', 'zemcontact.submit');
function xxx_my_saving_function()
{
global $zem_contact_values;
$set = 'timestamp=NOW()';
foreach($zem_contact_values as $name => $value)
{
$set .= ', '. $name . "='" . doSlash($value) . "'";
}
safe_insert('enquiries', $set)
}
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
ruud wrote:
You don’t need all those $evaluation lines, because it’s not a spam plugin using the callback event. I meant something like this (not tested):
Yes, the saving plugin isn’t a spam plugin, but then how does the plugin know if a spam plugin already marked the message as spam? Isn’t the saving function loaded on same callback as the spam plugins, not after ZCR has blocked the spam.
If there were third calleback for saving, then I would agree. Tho, it’s matter of if the site has any spam plugins installed.
Last edited by Gocom (2010-10-25 14:26:13)
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
Ah, right. I should’ve read your code more carefully.
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
I really hope somebody could help me out with this!
I have the following snippet as used by Kevin, but after submission the database doesn’t get populated:
<?php
// clear residual values
$formName = '';
$formEmail = '';
if( !empty( $_POST['zem_contact_submit'] ))
{
// scrub field values and assign to variables
$formTimeStamp = date('Y-m-d'); // this gets the current date
$formName = htmlspecialchars( $_POST['name'], ENT_QUOTES );
$formEmail = htmlspecialchars( $_POST['email'], ENT_QUOTES );
// start errors
$errors = array();
if( empty( $formName ) ) { $errors['name'] = '1'; }
if( empty( $formEmail ) ) { $errors['email'] = '1'; }
// start database insertion if there are no errors
if( empty( $errors ) )
{
$query = "INSERT INTO enquiries SET timestamp='$formTimeStamp', name='$formName', email='$formEmail' LIMIT 1";
$result = mysql_query($query);
}
}
?>
My localhost environment (from TXP Diagnostics) is:
- Textpattern version: 4.4.1 (r3575)
- PHP version: 5.2.17
- MySQL: 5.5.25
- Server: Apache
- Server OS: Darwin 11.4.0
I have no PHP skills, so am not able to figure out as to why this wouldn’t work :( … Please help!
Last edited by husainhk (2012-09-02 11:50:18)
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
Try this instead. It should at the very least give you an error message if the insert is tried but fails (not tested).
<?php
if( !empty( $_POST['zem_contact_submit'] ))
{
$formName = doSlash(trim(ps('name')));
$formEmail = doSlash(trim(ps('email')));
if(!empty($formName) and !empty($formEmail))
{
$result = safe_insert('enquiries', "timestamp=NOW(), name='$formName', email='$formEmail'");
if (!$result) echo mysql_errno() . ": " . mysql_error();
}
}
?>
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
Hi Ruud … works like charm!!
I tried adding a <zem_contact_secret> in the form, to save something in the DB too, and modified your code to this:
<?php
if( !empty( $_POST['zem_contact_submit'] ))
{
$formName = doSlash(trim(ps('name')));
$formEmail = doSlash(trim(ps('email')));
$formSecret = doSlash(trim(ps('secret')));
if(!empty($formName) and !empty($formEmail))
{
$result = safe_insert('enquiries', "timestamp=NOW(), name='$formName', email='$formEmail', secret='$formSecret'");
if (!$result) echo mysql_errno() . ": " . mysql_error();
}
}
?>
But it doesn't populate the 'secret' field in the DB :(
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
That’s because <zem_contact_secret>
is not actually sent in the HTML form, otherwise it wouldn’t be secret, so you have to do differently (1 line changed, 1 line added at the beginning). Actually, the other form fields can be done like that as well:
<?php
global $zem_contact_values;
if( !empty( $_POST['zem_contact_submit'] ))
{
$formName = doSlash(trim(ps('name')));
$formEmail = doSlash(trim(ps('email')));
$formSecret = doSlash($zem_contact_values['secret']);
if(!empty($formName) and !empty($formEmail))
{
$result = safe_insert('enquiries', "timestamp=NOW(), name='$formName', email='$formEmail', secret='$formSecret'");
if (!$result) echo mysql_errno() . ": " . mysql_error();
}
}
?>
Offline
Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database
I updated the code, but still get a blank in the secret field :(
EDIT: The previous PHP code works, if I actually place am <input type=“hidden”> in the form, but as you say, it’s not a secret anymore :)
Last edited by husainhk (2012-09-04 08:22:34)
Offline