Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: [MOD] Contact form with custom fields (sNews 1.6)  (Read 18316 times)

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 955
[MOD] Contact form with custom fields (sNews 1.6)
« on: May 17, 2007, 05:04:17 PM »

Applying this MOD allows to add custom fields to contact form by describing needed items in single place.

Prerequisites.

1) For this MOD to function following functions should be added (either in snews.php or included from separate php file):
Code: [Select]
function get_contact_fields( $arrFields, &$code, $action ) {
   $strResult = '';
   $code = 0;
   switch ($action) {
      case 'output' :
         foreach ( $arrFields as $fieldDef ) {
            $fieldType    = $fieldDef[0];
            $fieldName    = $fieldDef[1];
            $fieldDefault = '';
            $fieldLabel   = ($fieldDef[3] == 'required') ? '* ' . $fieldDef[2] : $fieldDef[2];
            $fieldCSS     = $fieldType;
            $strResult .= html_input( $fieldType, $fieldName, $fieldName, $fieldDefault, $fieldLabel, $fieldCSS, '', '', '', '', '', '', '', '', '');
         }
         break;
      case 'submit' :
         foreach ( $arrFields as $fieldDef ) {
            $fieldName  = $fieldDef[1];
            $fieldValue = cleanText( $fieldName );
            $fieldLabel = $fieldDef[2];
            $minLength  = $fieldDef[4];
            if (!empty($fieldValue)) {
               if ( $minLength > 0 ) {
                  if ( strlen( $fieldValue ) < $minLength ) {
                     $strResult = '';
                     $code = 2;
                     break;
                  }
               }
               $strResult .= "$fieldLabel: $fieldValue\n\n";
            }
            else if ( $fieldDef[3] == 'required' ) {
               $strResult = '';
               $code = 1;
               break;
            }
         }
         break;
      default :
         break;
   }
   return $strResult;
}


function cleanText( $name ) {
   $str = trim($_POST[$name]);
   $str = clean( cleanXSS( $str ) );
   return $str;
}
MOD itself.

2) All modifications are done inside function contact(), so first comment out original function and then add this version. I included full code here :
Code: [Select]
// CONTACT FORM
function contact() {
   //
   // Custom fields definition array
   $arrFields   = array();
   // Add custom fields using following definition:
   // $arrFields[] = array("<input type>", "<name>", "<label>", '<required/optional>', '<minlength/0>');
   //
   // Examples.
   // To add mandatory field with label Phone No without string length checking:
   //$arrFields[] = array('text', 'phone', 'Phone No', 'required', 0);
   // To add optional field with label City with checking for minimal string length of 4 symbols:
   //$arrFields[] = array('text', 'city', 'City', 'optional', 4);
   //
if (!isset($_POST['contactform'])) {
// Display contact form
echo '<div class="commentsbox"><h2>'.l('contact').'</h2>';
echo '<p>'.l('required').'</p>';
echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website'), '');
echo html_input('text', 'name', 'name', '', '* '.l('name'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'email', 'email', '', '* '.l('email'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'weblink', 'weblink', '', l('url'), 'text', '', '', '', '', '', '', '', '', '');
      //
      // Get output for custom fields
      $strHTML = get_contact_fields( $arrFields, $code, 'output' );
      echo $strHTML;
      //
echo html_input('textarea', 'message', 'message', '', '* '.l('message'), '', '', '', '', '', '5', '5', '', '', '');
      //
echo mathCaptcha();
//
echo '<p>';
echo html_input('hidden', 'ip', 'ip', $_SERVER['REMOTE_ADDR'], '', '', '', '', '', '', '', '', '', '', '');
echo html_input('hidden', 'time', 'time', time(), '', '', '', '', '', '', '', '', '', '', '');
echo html_input('submit', 'contactform', 'contactform', l('submit'), '', 'button', '', '', '', '', '', '', '', '', '');
echo '</p></form></div>';
$_SESSION[db('website').'contact'] = 0;
}
else {
// Fetch and clean input data from default contact form
$to = s('website_email');
$subject = s('contact_subject');
$name = trim($_POST['name']);
$name = strlen($name) > 1 ? clean(cleanXSS($name)) : null;
$mail = trim($_POST['email']);
$mail = trim($_POST['email']);
$mail = (strlen($mail) > 7 && preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $mail)) ? clean(cleanXSS($mail)) : null;
$url = trim($_POST['weblink']);
$url = (strlen($url) > 8 && strpos($url, '?') === false) ? clean(cleanXSS($url)) : null;
$message = trim($_POST['message']);
$message = strlen($message) > 9 ? stripslashes(cleanXSS($message)) : null;
$message = strip_tags($message);
$now = is_numeric($_POST['time']) ? $_POST['time'] : null;
$ip = (strlen($_POST['ip']) < 16) ? clean(cleanXSS($_POST['ip'])) : null;
      //
      // Fetch and clean data from added custom input fields
      $validExtraFields = true;
      $code = 0;
      $strExtraFields = get_contact_fields( $arrFields, $code, 'submit' );
      if ( $code == 1 || $code == 2 ) { // Required fields values missing/invalid
         $validExtraFields = false;
      }
      //
if($_SESSION[db('website').'contact'] == 0) {
         // Added $validExtraFields to if condition
if ($ip == $_SERVER['REMOTE_ADDR'] && (time() - $now) > 4 && $name && $mail && $message && mathCaptcha($_POST['calc'], $_POST['sum']) && $validExtraFields) {
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/plain; charset=".s('charset')."\n";
$header .= "From: $name <$mail>\r\nReply-To: $name <$mail>\r\nReturn-Path: <$mail>\r\n";
$addUrl = isset($url) ? l('url').': '.$url."\n\n" : '';
$body = "Message from: ".$name." <$mail>\n".$addUrl.$strExtraFields.l('message').":\n".$message;
mail($to, $subject, $body, $header);
# notify of success
echo notification(0,l('contact_sent'),'home');
$_SESSION[db('website').'contact'] = 1;
}
else {echo notification(1,l('contact_not_sent'),'contact');}
}}}
Use.

After applying changes as described above just add extra fields needed to the array $arrFields at the very beginning of modified function contact(). Examples are included in code comments.

Same mod for v.1.5.31 is here.

Rights
Modification code (c) 2007 Valdis Ozols aka codetwist
Included 3rd parties code is (c) their respective authors.
Released under Creative Commons Attribution.


Please, who is interested check this out, feedback welcome  :)
« Last Edit: September 29, 2007, 03:54:33 PM by codetwist »
Logged

tuxStyle

  • Newbie
  • *
  • Karma: 0
  • Posts: 15
[MOD] Contact form with custom fields (sNews 1.6)
« Reply #1 on: May 17, 2007, 07:35:35 PM »

Read here how to apply the patch on Widows:
http://www.solucija.com/forum/viewtopic.php?id=4541

Patch:
http://www.activestyle.ca/sNews-patch-mod/mod.CustomContact-s1.6

Yesterday i wanted to request a custom contact form :)
Logged

OtaTop

  • Guest
[MOD] Contact form with custom fields (sNews 1.6)
« Reply #2 on: July 24, 2007, 11:41:19 PM »

Thanks - this has been really helpful for me. One question though: how would I go about adding an (optional) "upload" field to allow users to attach a file and send as normal?
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 955
[MOD] Contact form with custom fields (sNews 1.6)
« Reply #3 on: July 25, 2007, 09:49:57 AM »

Just a quick ideas:
 - look into function files at snews.php
 - probably will be easier to add directly at function contact() without going through get_contact_fields() as adding attachment will not blend nicely with current handling of submit there ;)
Logged

mosh

  • Dev Invitee
  • Hero Member
  • *****
  • Karma: 78
  • Posts: 522
  • Awesome day :)
[MOD] Contact form with custom fields (sNews 1.6)
« Reply #4 on: July 25, 2007, 02:35:25 PM »

:cool:  yup that was needed mod, thank you  :cool:
Logged

Martin2007

  • Newbie
  • *
  • Karma: 0
  • Posts: 22
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #5 on: November 08, 2007, 05:11:00 AM »

Just a quick ideas:
 - look into function files at snews.php
 - probably will be easier to add directly at function contact() without going through get_contact_fields() as adding attachment will not blend nicely with current handling of submit there ;)

Thanks for this MOD. Is there a way to place DROP-DOWN selectable SUBJECT FIELD in the:

$arrFields[]   = array('text', 'subject', 'Subject', 'optional', 0);

Perhaps in the <input type> section of the array. I will appreciate any help. Thanks once again for the great MOD.

Martin
Logged

Nicu Alecu

  • Full Member
  • ***
  • Karma: 12
  • Posts: 236
    • IT consultant
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #6 on: February 27, 2008, 11:50:07 PM »

Does anybody know if this MOD works with Rui's ML? Maybe Rui himself?
Cause it sure ain't working for me! (let me state here the obvious: I'm no programmer!)
And it would be a much appreciated addition for the site I'm working on ...


Later edit: never mind the above! for those who are interested, it's working; all that's needed are some translation variables for ML, but that I can live without.
« Last Edit: February 28, 2008, 12:11:23 AM by Nicu Alecu »
Logged

Keyrocks

  • Doug
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 451
  • Posts: 6288
  • Semantically Challenged
    • snews.ca
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #7 on: February 28, 2008, 09:23:27 PM »

Later edit: never mind the above! for those who are interested, it's working; all that's needed are some translation variables for ML, but that I can live without.

And they should be easy to add in later as time permits... kind of like housekeeping stuff.  :)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

smandersloot

  • Newbie
  • *
  • Karma: 0
  • Posts: 6
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #8 on: March 29, 2008, 02:15:53 AM »

I've done everything the instructions told me to do, but it isn't working. The custom fields show up on the contactpage, but when I send a test e-mail, the custom fields are not included in the e-mail. What went wrong? This is what I've added:

Code: [Select]
// CONTACT FORM
function contact() {
   //
   // Custom fields definition array
   $arrFields   = array();
   // Add custom fields using following definition:
   // $arrFields[] = array("<input type>", "<name>", "<label>", '<required/optional>', '<minlength/0>');
   //
   // Examples.
   // To add mandatory field with label Phone No without string length checking:
   //$arrFields[] = array('text', 'phone', 'Phone No', 'required', 0);
   // To add optional field with label City with checking for minimal string length of 4 symbols:
   //$arrFields[] = array('text', 'city', 'City', 'optional', 4);
   //
if (!isset($_POST['contactform'])) {
// Display contact form
echo '<div class="commentsbox"><h2>'.l('contact').'</h2>';
echo '<p>'.l('required').'</p>';
echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website'), '');
echo html_input('text', 'name', 'name', '', '* '.l('name'), 'text', '', '', '', '', '', '', '', '', '');
echo html_input('text', 'email', 'email', '', '* '.l('email'), 'text', '', '', '', '', '', '', '', '', '');
$arrFields[] = array('text', 'street', 'Straat + huisnr.', 'required', 0);
$arrFields[] = array('text', 'city', 'Woonplaats', 'required', 0);
$arrFields[] = array('text', 'phone', 'Telefoonnr.', 'required', 0);

Did I forget something?
Thanks in advance! Really like the mod, thanks for that, I just need to get it to work. Please help!
Logged

funlw65

  • Hero Member
  • *****
  • Karma: 96
  • Posts: 800
    • Country Lab
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #9 on: March 29, 2008, 02:44:04 AM »

Put definitions under first line:

   // Custom fields definition array
   $arrFields   = array();
-->Here

Logged

smandersloot

  • Newbie
  • *
  • Karma: 0
  • Posts: 6
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #10 on: March 29, 2008, 02:58:37 AM »

Thanks for your quick reply! It works now, guess I had to read the instructions a bit better.. Getting late I think: it's 3 o'clock in the morning in the Netherlands.
Thanks for your help!
Logged

areyouami

  • Newbie
  • *
  • Karma: 3
  • Posts: 24
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #11 on: April 13, 2008, 08:09:31 PM »

This mod is awesome!

I need the following situation:

The ability to remove the requirement of the message field in the contact form. The blog portion of the script with comments is not being used, so it's no big deal.

Also, the ability to have the custom form work with the mod that remembers the data entered in the form in case there's an error. What happens now, and the default, is that when a person enters everything, but forgets one thing, they click back and then it's all empty. That bites.

Thanks!
Logged

areyouami

  • Newbie
  • *
  • Karma: 3
  • Posts: 24
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #12 on: April 13, 2008, 09:32:06 PM »

Never mind. I figured it out.

You actually don't need this mod to add custom fields, there's instructions:

http://snewscms.com/forum/index.php?topic=3441.msg23754#msg23754


This is what I used to add a company text field, phone text field, and address text area.

Thanks!
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 955
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #13 on: April 13, 2008, 09:40:31 PM »

@ areyouami : Does spamming makes You happy? It's axiomatic that code can be modified in more than 1, 2, bazillion ways, no? Or blinding flash of obvious just hit You heavy and hard. Doh  >:(
Logged

areyouami

  • Newbie
  • *
  • Karma: 3
  • Posts: 24
Re: [MOD] Contact form with custom fields (sNews 1.6)
« Reply #14 on: April 13, 2008, 09:59:30 PM »

I didn't realize I was spamming. I'm not a php person and so many things that are obvious to you are not at all obvious to me. It took me 5 hours to add these field forms and trying, without php knowledge, to combine the mods. Which didn't work for me. Then I saw that other post and it took 1 min., so I added what I found because others might it useful.

Incidentally, if you look at the other post, the initial response by a long time snews user was that it was really difficult to add new fields because it required adding new tables to the database. Which is not the case. And then I read further to see that it's easy to add them. So there are many levels of users here, and I'll bet most are designers and not developers.
« Last Edit: April 13, 2008, 10:05:07 PM by areyouami »
Logged
Pages: [1] 2