News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Custom Form Mod

Started by live627, July 09, 2008, 10:24:44 PM

Previous topic - Next topic

Garou

#320
LHVWB, I am loving this mod. I was using the un-packaged Custom prefills mod I found on Tiny Portal which was usable but this is exactly what I was looking for. Im now using it for an application form on one site and a bug tracker and a hidden support/contact board on another. Its just about perfect. :)

I do have a couple ideas for improvement for ya though.

The first would be being able to set the Message icon for the Forum Output. Sorry I found that that was planned already. Must have missed it the first time.

The other is probably a bit more complicated. The Custom Templates example you listed is excellent but as many people that use SMF are not always that familiar with PHP coding... How much trouble would it be to add a wysiwyg  editor for the templates instead?

I'm picturing something like the input box you have for the forum output editor. That way the admin could set up the form the way they like and would just use the {variable} function to input the form fields.

I'm not sure how much work either would take but its just something that came to mind as I was setting things up on my sites. Either way its still a beautiful mod and I can not thank you enough for making it. You deserve a giant pat on the back.

Dem0n

How do I actually link to a form? I have one created, and it shows in the list of forms, but I only have the option to edit or delete it. When I view "/index.php?action=form" nothing is listed?

Dem0n

nvm got it to work. is there any way to change where a user is redirected to after submitting the form?

Garou

Not yet but LHVWB said it might be in the next version.

Garou

#324
*edit* Ignore this post I fixed it.

I'm working on making an example form for the other staff on one of my sites to look at and learn from so they can make their own forms without having to bug me. I decided that I really should do something with the template so one, they can see how to do it, and also I could put extra instructions in there for them.

That said I'm running into a few problems that I'm sure I could find if I spent some time actually learning to code rather then just cutting and pasting what I know already works. LOL

OK here is the code I have so far...

// This is an example showing how to alter the submit form page.
function form_template_exampleform()
{
global $context, $txt, $settings, $scripturl;

// Starting text for the form and tables. Don't muck with this, unless you need to change the style!!!  ;)
echo '
<form action="', $context['post_url'], '" method="post" accept-charset="', $context['character_set'], '">
<table width="80%" border="0" cellspacing="0" cellpadding="0" class="tborder" align="center">
<tr><td>
<table border="0" cellspacing="0" cellpadding="4" width="100%">
<tr class="titlebg">
<td colspan="3">', $context['settings_title'], '</td>
</tr>
<tr class="windowbg2">
<td colspan="3"><br /></td>
</tr>';

// Here you can add rows to the beginning of the table, if you want to...
/* Like this:
echo '
<tr class="windowbg2">'
New row.
</tr>';
*/

echo '
<tr class="windowbg2">
This will put something above the form.
</tr>';
echo '
<td class="windowbg2">
This will put something at the top of the form.
</td>';

// Documentation for the contents of each $field_data array, or entry into the $context['fields'] array.
/*
$field_name = The name of the field, straight from the value stored by the admin in the admin settings area;
$field_data = array(
'text' => This is the text which needs to be displayed next to the setting.,
'type' => The type of input which the field is ,
'value' => The value of the field, if this is not the first attempt at submitting the form,
'data' => The list of options - only for the selection box type,
'required' => A boolean value telling us wether or not its necessary to have a valid value for this field in order to submit the form,
'failed' => A boolean value which tells us wether or not this field caused the form to fail to submit,
);
*/

// Now actually loop through all the variables.
foreach ($context['fields'] as $field_name => $field_data)
{
// Output the start of the row, as well as a spacer column.
echo '
<tr class="windowbg2">
<td class="windowbg2"></td>';

// Show the display text for this field.
echo '
<td valign="top"><label for="', $field_name, '">', $field_data['text'], '</label></td>
<td class="windowbg2" width="50%">';

// Show a check box?
if ($field_data['type'] == 'checkbox')
echo '
<input type="hidden" name="', $field_name, '" value="0" /><input type="checkbox" name="', $field_name, '" id="', $field_name, '" class="check" ', ($field_data['value']) ? 'checked="checked"' : '', '" />';
// Show a selection box?
elseif ($field_data['type'] == 'selectbox')
{
echo '
<select name="', $field_name, '">';
foreach ($field_data['data'] as $option)
echo '
<option value="', $option, '"', ($option == $field_data['value'] ? ' selected="selected"' : ''), '>', $option, '</option>';
echo '
</select>';
}
// Large Text box?
elseif ($field_data['type'] == 'largetextbox')
{
echo '
<textarea rows="4" cols="30" name="', $field_name, '">', $field_data['value'], '</textarea>';
}
// Int, Float or text box?
else
echo '
<input type="text" name="', $field_name, '" value="', $field_data['value'], '"/>';

// Show the 'required' asterix if necessary.
if(!empty($field_data['required']))
echo '
<div ', !empty($field_data['failed']) ? 'style="color:#FF0000;"' : '' ,'> *</div>';

// End the input column and the entire row.
echo '
</td>
</tr>';
}

// Here you can add rows to the end of the form, if you want to...
/* Like this:
echo '
<tr class="windowbg2">'
New row.
</tr>'
*/

echo '
<tr class="windowbg2">
This was supposed to put something below the form.
</tr>';
echo '
<td class="windowbg2">
This will put something at the bottom of the form.
</td>';

// Show the "Required Fields" text down the bottom, show it in red if there was a failed submit.
echo '
<tr class="windowbg2">
<td colspan="3" style="text-align:center;', !empty($context['failed_form_submit']) ? 'color:#FF0000;' : '', '">
* ', $txt['CustomForm_required'], '
</td>
</tr>';

echo '
<tr>
<td class="windowbg2" colspan="3" align="center" valign="middle"><input type="submit" value="', $txt['CustomForm_submit'], '" /></td>
</tr>
</table>
</td></tr>
</table>
<input type="hidden" name="sc" value="', $context['session_id'], '" />
</form>';
}


This is what it looks like...


Why the <tr> command prints is up above the entire form doesn't make sense to me when the <td> command will print within the form. I would think that both should be printing in the same place, depending on where you actually put them within the function.

Following the previous template examples I don't understand why the bottom <tr> line posts at the top instead of the bottom. Using the <td> line in the same place prints at the bottom of the fields so shouldn't a <tr> in that section print below the entire form?

The alignment thing I'm sure I can figure out but right now its bugging me. It looks like its using the colspan from the lines above. I'm also assuming that's why the white space is showing to the right of the text.

That said I would think as long as the colspan is not listed in the current as <td> it shouldn't take place. The one above should have closed it out with the </td>, right?

I haven't tried it yet as it just came to mind but should I just put those sections in a table command? Would that actually fix it or am I just grasping at straws?

Any help in understanding this would be greatly appreciated.

Just in case anyone wants what Ive got so far for their own site or has suggestions of other examples I should put in, this is my a dump of my sql file for the form...
--
-- Table structure for table `smf_cf_fields`
--

CREATE TABLE IF NOT EXISTS `smf_cf_fields` (
  `ID_FIELD` smallint(5) NOT NULL auto_increment,
  `ID_FORM` smallint(5) default NULL,
  `title` tinytext,
  `text` tinytext,
  `type` tinytext,
  `type_vars` text,
  PRIMARY KEY  (`ID_FIELD`),
  KEY `ID_FORM` (`ID_FORM`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `smf_cf_fields`
--

INSERT INTO `smf_cf_fields` (`ID_FIELD`, `ID_FORM`, `title`, `text`, `type`, `type_vars`) VALUES
(1, 1, 'text_box', 'There should be a small input allowing you to type anything here.', 'textbox', ''),
(2, 1, 'large_text_box', 'There should be a Large Box that you can type anything in here.', 'largetextbox', ''),
(3, 1, 'check_box', 'This will tell you whether the boxed was checked or not when it posts in the forum.', 'checkbox', 'Box was checked., Box was not checked.'),
(4, 1, 'selection_box', 'The Selection Box will allow members to chose from various items.', 'selectbox', 'Item 1, Item 2, Item 3'),
(7, 1, 'float_box', 'This box allows for <a href="http://en.wikipedia.org/wiki/Floating_point">Floating Point Values</a>. <br><br> Mostly it will be used for allowing users to input decimal numbers.', 'float', ''),
(6, 1, 'integer_box', 'The Integer Box will only accept whole numbers. <br><br> You can not type text, decimals, or fractions in this box.', 'int', '');

-- --------------------------------------------------------

--
-- Table structure for table `smf_cf_forms`
--

CREATE TABLE IF NOT EXISTS `smf_cf_forms` (
  `ID_FORM` smallint(5) NOT NULL auto_increment,
  `ID_BOARD` smallint(5) default NULL,
  `title` tinytext,
  `template_function` tinytext,
  `subject` tinytext,
  `output` text,
  PRIMARY KEY  (`ID_FORM`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `smf_cf_forms`
--

INSERT INTO `smf_cf_forms` (`ID_FORM`, `ID_BOARD`, `title`, `template_function`, `subject`, `output`) VALUES
(1, 123, 'Example', 'exampleform', 'Test templates.', '[center][u][b]*This post it the result of someone using the Example Form*[/b][/u][/center]<br /><br /><br />If this post was in the wrong board then the wrong [b]Board ID[/b] was entered in the &quot;Example&quot; Form.<br /><br />index.php?action=featuresettings;sa=customform;<br /><br />[b]Text Box[/b]<br />If something was entered in the Text Box field It will show below.<br /><br />{text_box}<br /><br />[b]Large Text Box[/b]<br />If something was entered in the Large Text Box field It will show below.<br /><br />{large_text_box}<br /><br />[b]Check Box[/b]<br />Was the box checked?<br /><br />{check_box}<br /><br />[b]Selection Box[/b]<br />What item was picked in the Selection Box?<br /><br />{selection_box}<br /><br />');

the.basement

#325
Quotethe.basement, um its good if you find a fix for something then post what you changed say like

Find some code and
replace with some other code.

Repackaging someones mod like that really isn't a good practice to get into.

ok sorry go and find what i changed......

pooya

Okay this just makes me a new topic in the board without anything in it .. wtf ?

Garou

pooya, try this link http://www.simplemachines.org/community/index.php?topic=248871.msg1633514#msg1633514

Its a good example of how to fill out everything.

the.basement, um its good if you find a fix for something then post what you changed say like

Find some code and
replace with some other code.

Repackaging someones mod like that really isn't a good practice to get into.

Garou

#328
OK I fixed my template problem. I'm still concerned about some of the issues I mentioned before but none the less I have a working template.



Paste this code before the ?> in the CustomForm.template.php
  /* This is an example showing how to alter the template for the submit form page.
     To use this template in a form in the Custom Form Mod Settings we would just enter
exampleform not form_template_exampleform in the Custom Template Function field */

function form_template_exampleform()
{
global $context, $txt, $settings, $scripturl;

// Starting text for the form and tables. Don't muck with this, unless you need to change the style!!!  ;)
echo '
<form action="', $context['post_url'], '" method="post" accept-charset="', $context['character_set'], '">
<table width="80%" border="0" cellspacing="0" cellpadding="0" class="tborder" align="center">
<tr><td>
<table border="0" cellspacing="0" cellpadding="4" width="100%">
<tr class="titlebg">
<td colspan="3">', $context['settings_title'], '</td>
</tr>
<tr class="windowbg2">
<td colspan="3"><br /></td>
</tr>';

// Here you can add information at the top of yor form, if you want to...
echo '
        <tr
<td colspan="3"  class="windowbg2" style="text-align:center">
<div>
<h1>Custom Form Mod Example</h1>
<br>
<p>Using templating will allow you to customize the look of your form. It will even allow you to add images like...</p>
<img border="1" src="http://www.google.com/intl/en_ALL/images/logo.gif">
<p style="text-align:left">In fact you can use most any HTML in your SMF forums /Themes/default/CustomForm.template.php as long as all the actual functions for the form remain intact. There should be basic instructions throughout explaining where and how you can modify the template.</p>
<p style="text-align:left">To use a template like this you first have modify the <b>CustomForm.template.php</b>. Then you have to set up your actuall form in your fourms under Admin / Features and Options / Custom Forms.<br><br>Click on <b>Add New Form</b>. If you named your function form_template_exampleform then you would enter exampleform in the <b>Custom Template Function</b> field.<br><br>After that you may customize your form questions as you wish.
<br><hr />
<div><i>Below is an example of some of the functions you can preform with the Custom Form Mod for SMF</i></div>
<br><hr />
</td></tr>';
// End of information in the top section.

// Documentation for the contents of each $field_data array, or entry into the $context['fields'] array.
/*
$field_name = The name of the field, straight from the value stored by the admin in the admin settings area;
$field_data = array(
'text' => This is the text which needs to be displayed next to the setting.,
'type' => The type of input which the field is ,
'value' => The value of the field, if this is not the first attempt at submitting the form,
'data' => The list of options - only for the selection box type,
'required' => A boolean value telling us wether or not its necessary to have a valid value for this field in order to submit the form,
'failed' => A boolean value which tells us wether or not this field caused the form to fail to submit,
);
*/

// Now actually loop through all the variables.
foreach ($context['fields'] as $field_name => $field_data)
{
// Output the start of the row, as well as a spacer column.
echo '
<tr class="windowbg2">
<td class="windowbg2"></td>';

// Show the display text for this field.
echo '
<td valign="top"><label for="', $field_name, '">', $field_data['text'], '</label></td>
<td class="windowbg2" width="50%">';

// Show a check box?
if ($field_data['type'] == 'checkbox')
echo '
<input type="hidden" name="', $field_name, '" value="0" /><input type="checkbox" name="', $field_name, '" id="', $field_name, '" class="check" ', ($field_data['value']) ? 'checked="checked"' : '', '" />';
// Show a selection box?
elseif ($field_data['type'] == 'selectbox')
{
echo '
<select name="', $field_name, '">';
foreach ($field_data['data'] as $option)
echo '
<option value="', $option, '"', ($option == $field_data['value'] ? ' selected="selected"' : ''), '>', $option, '</option>';
echo '
</select>';
}
// Large Text box?
elseif ($field_data['type'] == 'largetextbox')
{
echo '
<textarea rows="4" cols="30" name="', $field_name, '">', $field_data['value'], '</textarea>';
}
// Int, Float or text box?
else
echo '
<input type="text" name="', $field_name, '" value="', $field_data['value'], '"/>';

// Show the 'required' asterix if necessary.
if(!empty($field_data['required']))
echo '
<div ', !empty($field_data['failed']) ? 'style="color:#FF0000;"' : '' ,'> *</div>';

// End the input column and the entire row.
echo '
</td>
</tr>';
}

// Show the "Required Fields" text down the bottom, show it in red if there was a failed submit.
echo '
<tr class="windowbg2">
<td colspan="3" style="text-align:center;', !empty($context['failed_form_submit']) ? 'color:#FF0000;' : '', '">
* ', $txt['CustomForm_required'], '
</td>
</tr>';

//  Here you can add information just before the submit button.
echo '
<tr class="windowbg2">
<td colspan="3"  style="text-align:center">
<hr />
<div>
<p style="text-align:left">Once the user hits the <b>Submit Form</b> button the form will print all the information entered in the fields above in a preformated post to the designated board. Depending on the users access to the board they will be re-directed to it and they may modify the post.</p>
<hr />
</div>
</td></tr>';

  //  Show the Form Submit button
echo '
<tr>
<td class="windowbg2" colspan="3" align="center" valign="middle"><input type="submit" value="', $txt['CustomForm_submit'], '" /><hr /></td>
</tr>';

  //   Here you can add information below the submit button.
echo '
<tr class="windowbg2">
<td colspan="3"  style="text-align:center">
<div>Visit the <a href="http://custom.simplemachines.org/mods/index.php?mod=1279">Custom Form Mod</a> page for more information about how to use this Mod for <a href="http://www.simplemachines.org/">SMF</a>.</div>
</td></tr>

</table>
</td></tr>
</table>
<input type="hidden" name="sc" value="', $context['session_id'], '" />
</form>';
}


In your Custom Form Mod Settings enter "exampleform" (minus the ") in the Custom Template Function field.

Just in case anyone wants what Ive got  for their own site, this is my a dump of my sql file for the form...

--
-- Table structure for table `smf_cf_fields`
--

DROP TABLE IF EXISTS `smf_cf_fields`;
CREATE TABLE IF NOT EXISTS `smf_cf_fields` (
  `ID_FIELD` smallint(5) NOT NULL auto_increment,
  `ID_FORM` smallint(5) default NULL,
  `title` tinytext,
  `text` tinytext,
  `type` tinytext,
  `type_vars` text,
  PRIMARY KEY  (`ID_FIELD`),
  KEY `ID_FORM` (`ID_FORM`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `smf_cf_fields`
--

INSERT INTO `smf_cf_fields` (`ID_FIELD`, `ID_FORM`, `title`, `text`, `type`, `type_vars`) VALUES
(1, 1, 'text_box', '<b>Text box (String)</b> adds a small input allowing the user to type anything.', 'textbox', ''),
(2, 1, 'large_text_box', '<b>Large (Multiline) Text Box</b> adds a large input allowing the user to type anything.', 'largetextbox', ''),
(3, 1, 'check_box', '<b>Check box</b> will post whether the boxed was checked or not once the form is submitted to the forum. By default it will post yes if checked and no if not checked. You can chose what is actually posted by using the <b>Extra Type Parameters</b> field.<b', 'checkbox', 'The box was checked., The box was not checked.'),
(4, 1, 'selection_box', '<b>Selection Box</b> will allow the user to chose from various items. Enter the list of items you want seperated by commas in the <b>Extra Type Parameters</b> field.', 'selectbox', '-- Select One -- , Item 1, Item 2, Item 3'),
(7, 1, 'float_box', '<b>Text Box (Float)</b> allows a user to enter <a href="http://en.wikipedia.org/wiki/Floating_point">Floating Point Values</a>. <br><br> Mostly it will be used for allowing users to input decimal numbers.', 'float', ''),
(6, 1, 'integer_box', '<b>Text Box (Integer)</b> Will only allow the user to type whole numbers.<br><br>The user can not type text, decimals, or fractions in this box or the form will fail when submitted.', 'int', ''),
(8, 1, 'require', 'By entering "require" in the <b>Extra Type Parameters</b> field, you can require that the user makes an entry in that field. If the user does not make an entry in the field, the form will not post to the forum and asks that the user complete the form.', 'textbox', 'required');

-- --------------------------------------------------------

--
-- Table structure for table `smf_cf_forms`
--

DROP TABLE IF EXISTS `smf_cf_forms`;
CREATE TABLE IF NOT EXISTS `smf_cf_forms` (
  `ID_FORM` smallint(5) NOT NULL auto_increment,
  `ID_BOARD` smallint(5) default NULL,
  `title` tinytext,
  `template_function` tinytext,
  `subject` tinytext,
  `output` text,
  PRIMARY KEY  (`ID_FORM`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `smf_cf_forms`
--

INSERT INTO `smf_cf_forms` (`ID_FORM`, `ID_BOARD`, `title`, `template_function`, `subject`, `output`) VALUES
(1, 123, 'Example', 'exampleform', 'Test templates.', '[center][u][b]*This post it the result of someone using the Example Form*[/b][/u][/center]<br /><br /><br />If this post was in the wrong board then the wrong [b]Board ID[/b] was entered in the &quot;Example&quot; Form.<br /><br />index.php?action=featuresettings;sa=customform;<br /><br />[b]Text Box[/b]<br />If something was entered in the Text Box field It will show below.<br /><br />{text_box}<br /><br />[b]Large Text Box[/b]<br />If something was entered in the Large Text Box field It will show below.<br /><br />{large_text_box}<br /><br />[b]Check Box[/b]<br />Was the box checked?<br /><br />{check_box}<br /><br />[b]Selection Box[/b]<br />What item was picked in the Selection Box?<br /><br />{selection_box}<br /><br />[Required Text Box]<br />Note there are some problems with this that need to be worked out.<br /><br />{require}<br /><br />');


If anyone has any other ideas for field examples that should be included in this let me know.

pooya

Okay my custom form output isnt being saved, i click save brings me back to the same page with Form Output being blank.

Also how ot make this show in a single topic rather then making different topics ?

Garou

It should be saved. The only time it didn't save for me is if I clicked add new button before I clicked saved under the input box.

The singe topic feature should be in the next update when he gets around to it. For now we just have to use a board.

pooya

Oooooooooo this will populate my board like crazy, better wait then... :(

happyfeet

LHVWB -

First off, thank you for your hard work in developing this mod!

I understand the time constraints involved with updating this, or any mod. As outlined in previous posts, the 'required' option will function for Text Box (Integer) and will not work for Text Box (String). To be honest, the 'required' option is the only bug I would appreciate being addressed.

Is there any way you could tell me what I would need to change in my PHP files so that this one bug is resolved? I am using SMF 1.1.7

Thanks for your efforts  ;)

Garou

Quote from: the.basement on November 18, 2008, 03:43:28 PM
ok sorry go and find what i changed......

the.basement,
I looked at theCustomFormModcustom_v1.1_SMF1.1.5.zip you posted. It looks like in your version of the mod you only changed two files CustomForm.php and CustomForm.template.php.

Using WinMerg I only found one difference in the CustomForm.php nothing at all changed CustomForm.template.php.

In the CustomForm.php

Find...
// Do we have an invalid value? Is this field required?
if(($required
&& (($value == '') || ($value == 0))
&& ($field['type'] != 'checkbox'))

Replace...
// Do we have an invalid value? Is this field required?
if(($required
&& (($value == '') || ($value == '0'))
&& ($field['type'] != 'checkbox'))


This does fix the required fields problem. Good work on finding the fix.  :)

Nvb

Don't know if this is possible, but is there a way to create a form where you can choose from a selection box the correct board id/board description?

Or in other words, i want to create 1 form to use in all my different boards.
But now i have to make always the same form , with a different board id.
If you know i have about 100 boards and sub-boards...  :-\

TY for looking into this



Garou

Out of the box? No. The board ID is set in the admin section atm.

Its most likely possible to create something like that but I'm not sure where to begin. I'm guessing it would take a complete rework of the mod.

Nvb

#336
Hmm, where are the Board-ID settings stored?
Maybe i can manually copy/paste the php with notepad, and then change all the id's manually.
I guess i would work quicker than create 100 times the same form for the different boards.
8)

I did find this code in the CustomForm.Template.php

// If we can then show the list of forms.

if(!empty($context['custom_forms_list']))

{

// Show the list of forms.

foreach($context['custom_forms_list'] as $form)

{

echo '

<tr class="windowbg">

<td style="padding:4px;" >

<a href="' , $scripturl , '?action=form;id=' , $form['id'] ,'">' , $form['title'] , '</a>

</td>

<td style="padding:4px;" >

<a href="' , $scripturl , '?board=' , $form['id_board'] ,'">' , $form['board'] , '</a>

</td>

<td style="padding:4px;text-align:center;" >

<a href="' , $scripturl , '?action=form;id=' , $form['id'] ,'">' , $txt[305] , '</a>

</td>

</tr>';

}

}


Where is that 'foreach($context['custom_forms_list'] as $form)' referring to?

Garou

I think that's how you get the /index.php?action=form

Nvb

owkey, but where is that data stored then???

Garou

All information from this mod is stored in the sql database under...
smf_cf_fields
smf_cf_forms

Advertisement: