Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#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

#2 2010-10-23 06:12:09

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#4 2010-10-23 13:11:27

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

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

#5 2010-10-25 13:40:07

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#6 2010-10-25 14:22:46

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

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

#7 2010-10-25 14:57:59

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: [howto] How to Save zem_contact_reborn Submissions to a MySQL Database

Ah, right. I should’ve read your code more carefully.

Offline

#8 2012-09-02 11:45:53

husainhk
Member
From: Dubai, UAE
Registered: 2007-08-12
Posts: 105
Website

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

#9 2012-09-02 16:09:46

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#10 2012-09-04 05:09:50

husainhk
Member
From: Dubai, UAE
Registered: 2007-08-12
Posts: 105
Website

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

#11 2012-09-04 07:29:32

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#12 2012-09-04 08:00:09

husainhk
Member
From: Dubai, UAE
Registered: 2007-08-12
Posts: 105
Website

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

Board footer

Powered by FluxBB