Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

August 19, 2022, 09:30:23 PM

Login with username, password and session length
Members
  • Total Members: 3843
  • Latest: Tricky
Stats
  • Total Posts: 193479
  • Total Topics: 21088
  • Online today: 60
  • Online ever: 1035
  • (November 26, 2020, 04:45:12 AM)
Users Online
Users: 0
Guests: 40
Total: 40

Author Topic: Generic Application Form  (Read 97548 times)

0 Members and 1 Guest are viewing this topic.

JPDeni

  • Guest
Generic Application Form
« on: May 23, 2009, 04:04:28 PM »
This is designed for those who wish to make an application form for their site.

type:php article
author:JPDeni
name:Application Form
version:.1
date:16 Apr 2009
discussion topic:http://www.tinyportal.net/index.php/topic,9840.0.html (sort of)
description:
  • Creates a form to be filled out by prospective members
  • Form fields are defined within the text, to include text, textarea, select, radio and checkbox fields
  • Results can be emailed to an admin, posted to a specified board or both

Code: [Select]
global $sourcedir, $ID_MEMBER, $context, $scripturl, $user_info, $db_prefix;

// Generic Application Form
// URL: http://www.tinyportal.net/index.php?topic=29670.msg236779#msg236779
// Date: 30 November 2009

// Guests can't fill out the form
if ($ID_MEMBER == 0)
    echo 'You must be logged in before you can apply.';
else {

   require_once($sourcedir . '/Subs.php');
   require_once($sourcedir .'/Subs-Post.php');


// CONFIGURATION SECTION

   $intro_form = "Put whatever introduction you want to appear at the top of the form here. You can add html. Be careful with quotation marks.";
   $thanks_text = "This is what will appear after the form is submitted. You can use html. Be careful with quotation marks.";

// Define your fields. All of these values need to be defined, even if they are empty.
// The fields will be displayed in the order in which they are listed in the array.
// $fielddef =
//   array(
//     array(
//       'caption' =>      "", // caption to be displayed on the form. Can include symbols and spaces.
//       'name' =>         "", // a unique name for the field. No symbols or spaces
//       'type' =>         "", // text, radio, select, checkbox, textarea, heading
//       'options' =>      "", // for radio and select fields. List in order you wish them to appear, separated by commas; for checkboxes, it's the value to be saved and displayed next to the box
//       'defaultvalue' => "", // the default value for the field. Can be a variable or text. Be sure to enclose text in quotation marks
//       'required' =>     0   // 0 or 1 -- use 1 if the field must be filled out. use 0 if it's optional; never set a checkbox to be required
//     ),
//   );

   $fielddef =
      array(
         array(
            'caption' =>      "Heading",
            'name' =>         "heading",
            'type' =>         "heading",
            'options' =>      "",
            'defaultvalue' => "",
            'required' =>     0
         ),
         array(
            'caption' =>      "Name",
            'name' =>         "realname",
            'type' =>         "text",
            'options' =>      "",
            'defaultvalue' => "",
            'required' =>     1 
         ),
         array(
            'caption' =>      "Age",
            'name' =>         "age",
            'type' =>         "select",
            'options' =>      "13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40+",
            'defaultvalue' => "18",
            'required' =>     1
         ),     
         array(
            'caption' =>      "Sex",
            'name' =>         "sex",
            'type' =>         "radio",
            'options' =>      "M,F",
            'defaultvalue' => "",
            'required' =>     1
         ),     
         array(
            'caption' =>      "Do you like me?",
            'name' =>         "like",
            'type' =>         "checkbox",
            'options' =>      "Yes",
            'defaultvalue' => "Yes",
            'required' =>     0
         ),     
         array(
            'caption' =>      "Comments",
            'name' =>       "comments",
            'type' =>         "textarea",
            'options' =>      "",
            'defaultvalue' => "",
            'required' =>     1
         ),
      );



//send the application by email?
   $enable_email=false;

// email address of recruitment staff member
   $email_address='';

//post the application on forum?
   $enable_post=true;

//board id to which the application should be posted
   $board_id=2;

// Poll options
$add_poll = 1; // 0 = No poll; 1 = add poll
$poll_question = "Accept this member?"; // This is the question for the poll
$expireTime = 0; // Change this to the number of days you want the poll to run
$hideResults = 0; // 0 = Results are always visible; 1 = Can see results only after voting; 2 = Can see results only after poll expires
$changeVote = 0; // 0 = Voters can not change their votes; 1 = Voters can change their votes
$poll_choices = array('Yes','No'); // The options listed for the poll

//END OF CONFIGURATION SECTION
////////////////////////////////////////////////////////////////////////////////////////////


   $show_form= 'true';
   if (isset($_REQUEST['submitted'])) {  // Handle the form
// Check required fields
      $errors = array(); //Initialize error array   

      foreach ($fielddef as $field)
         if (empty($_REQUEST[$field['name']]) && ($field['required'] == 1)){ $errors[] = $field['name']; }
   
// There's at least one field missing
      if (isset($errors[0])) {
         foreach ($_REQUEST as $key => $value)
         $fieldvalue[$key] = $value;
      }
      else { // all is well
 
         $show_form='false';

         if ($enable_email)   {  // email an application
            $subject = 'Application';
            $body = '';
            foreach ($fielddef as $field) {
               if ($field['type'] == 'heading')
                  $body .= $field['caption'] . '
               ';
               else
                  $body .= $field['caption'] . ': ' . $_REQUEST[$field['name']] . '
               ';
            }
            mail($email_address, $subject, $body,"From: " . $user_info['email']);
         }

         if ($enable_post) {  //create new forum post with application
if ($add_poll === 1) {
$posterName = $context['user']['name'];
if ($expireTime > 0)
$expireTime = time() + $expireTime * 3600 * 24;
db_query("
INSERT INTO {$db_prefix}polls
(question, hideResults, maxVotes, expireTime, ID_MEMBER, posterName, changeVote)
VALUES ('$question', '$hideResults', '1', '$expireTime', '$ID_MEMBER', '$posterName', '$changeVote')", __FILE__, __LINE__);
$ID_POLL = db_insert_id();

// Create each answer choice.
$i = 0;
$setString = '';
foreach ($poll_choices as $option)
{
$setString .= "
('$ID_POLL', '$i', SUBSTRING('$option', 1, 255)),";
$i++;
}

db_query("
INSERT INTO {$db_prefix}poll_choices
(ID_POLL, ID_CHOICE, label)
VALUES" . substr($setString, 0, -1), __FILE__, __LINE__);

}
else
$ID_POLL = null;

            $postbody = 'Recruitment application has been made by ' . $context['user']['name'] .'<br /><br/>';
            foreach ($fielddef as $field) {
               if ($field['type'] == 'heading')
                  $postbody .= $field['caption'] . '<br />';
               else
                  $postbody .=  $field['caption'] . ': ' . $_REQUEST[$field['name']] . '<br />';
            }

            $msgOptions = array(
               'id' =>  0 ,
               'subject' => '[Pending] Application of ' . addslashes($_REQUEST['realname']),
               'body' => addslashes($postbody) ,
               'icon' => 'xx',
               'smileys_enabled' => true,
               'attachments' =>  array(),
            );
            $topicOptions = array(
               'id' => 0 ,
               'board' => $board_id,
               'poll' =>  $ID_POLL,
               'lock_mode' =>  null,
               'sticky_mode' =>  null,
               'mark_as_read' => true,
            );
            $posterOptions = array(
               'id' => $context['user']['id'],
               'name' => $context['user']['name'],
               'email' => $user_info['email'],
               'update_post_count' => true,
            );
            createPost($msgOptions, $topicOptions, $posterOptions);
         }
// Text for thank you page
         echo $thanks_text;
      }
   }
   else {
      foreach ($fielddef as $field) {
         $fieldvalue[$field['name']] = $field['defaultvalue'];
      }
   }

// Looks like you want the form,
   if ($show_form == 'true') {
      echo $intro_form . "<br>";
      if (isset($errors[0])) { echo '<div style="color: red;">Please fill in all fields with a *.</div>';  }

      echo '
   <form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
      <INPUT name="submitted" type="hidden" value="TRUE" />
        <table style="margin-left:auto; margin-right:auto;">';

      $bg = 'windowbg2';

      foreach ($fielddef as $field) {
// Headings have their own type of display
         if ($field['type'] == 'heading') {
            echo '
         <TR>
            <TD colspan="2" style="text-align: center; text-decoration: underline;">' .
               $field['caption'] . '
            </TD>
         </TR>';
         }

         else {

// How each field is displayed in the table
            echo '
         <TR class ="' . $bg . '">
            <TD align="right">';
              if ($field['required'] == 1) { echo '* '; }
              echo $field['caption'] . ':
            </TD>
            <TD align="left">';

// Go through each field type
            if ($field['type'] == 'text') {
               echo '<INPUT name="' . $field['name'] . '" type="text" value ="' . $fieldvalue[$field['name']] . '" />';
            }
            elseif ($field['type'] == 'radio') {
               $options = explode(',',$field['options']);
               foreach ($options as $option) {
                  echo '<INPUT TYPE="RADIO" NAME="' . $field['name'] . '" VALUE="'. $option . '"';
                  if ((isset($fieldvalue[$field['name']])) && ($option == $fieldvalue[$field['name']])) { echo ' CHECKED'; }
                  echo '>' . $option . ' ';
               }
            }
            elseif ($field['type'] == 'checkbox') {
               echo '<INPUT TYPE="CHECKBOX" NAME="' . $field['name'] . '" VALUE="'. $field['options'] . '"';
               if (isset($fieldvalue[$field['name']]) && ($fieldvalue[$field['name']]==$field['options'])) { echo ' CHECKED'; }
               echo '>' . $field['options']; 
            }
            elseif ($field['type'] == 'select') {
               echo '<SELECT name="' . $field['name'] . '" />';
               $options = explode(',',$field['options']);
               foreach ($options as $option) {
                  echo '<OPTION value="' . $option . '"';
                  if ((isset($fieldvalue[$field['name']])) && ($option == $fieldvalue[$field['name']])) { echo ' selected'; }
                  echo '>' . $option . '</OPTION>';
               }
               echo '</SELECT>';
            }   
            elseif ($field['type'] == 'textarea') {
               echo '<TEXTAREA name="' . $field['name'] . '" rows="4" cols="40">';
               echo $fieldvalue[$field['name']];
               echo '</' . 'TEXTAREA>';
            }

// Finish off the row
            echo '
            </TD>
         </TR>';
         }

// Set up the alternating colors for the next row
         ($bg == 'windowbg2') ? $bg = 'windowbg' : $bg = 'windowbg2';
      }

      echo '
         <TR class ="' . $bg . '">
            <TD colspan="2" align="center">
               <INPUT type="submit" value="Submit">
               <INPUT type="reset" value="Reset">
            </TD>
         </TR>
      </TABLE>
   </form>';
   }
}

23 Sep 2009 -- Minor alteration to make the script work in blocks as well as articles.
30 Nov 2009 -- Added function to add a poll
« Last Edit: November 30, 2009, 12:06:34 PM by JPDeni »

Offline Renegd98

  • Hero Member
  • *****
  • Posts: 733
Re: Generic Application Form
« Reply #1 on: May 23, 2009, 04:22:03 PM »
Thanks for sharing JP

JPDeni

  • Guest
Re: Generic Application Form
« Reply #2 on: May 23, 2009, 04:37:06 PM »
This is the same thing that I posted in the "Join us form" topic. I just created my own topic for it so people could figure out what code to use if they want to use mine.

Offline ZarPrime

  • Friends
  • *
  • Posts: 4653
Re: Generic Application Form
« Reply #3 on: May 23, 2009, 05:10:34 PM »
Jp Deni,

Thanks for sorting this out for me.  I'm going to try it now.

Should we put this in the Block Code Snippets Board as a [Block]?

ZarPrime

Offline Skhilled

  • Beta Tester
  • *
  • Posts: 1179
  • Once you stop learning, you stop living!
    • Doc Skillz
Re: Generic Application Form
« Reply #4 on: May 23, 2009, 05:31:30 PM »
Thanks, JPDeni! It is pretty easy to follow and commented well. Great job! I am going to try this.

Reading can seriously damage your ignorance!

Offline Ken.

  • Support Team
  • *
  • Posts: 4362
    • Our FamilyForum
Re: Generic Application Form
« Reply #5 on: May 23, 2009, 06:44:45 PM »
Already tried it and it works great.
Now I've just gotta find a use for it.  :coolsmiley:

http://www.ourfamilyforum.org/FamilyForum/index.php?page=197
" If everything seems under control, you’re not going fast enough." - Mario Andretti
Yesterday When I was Young.

Offline Skhilled

  • Beta Tester
  • *
  • Posts: 1179
  • Once you stop learning, you stop living!
    • Doc Skillz
Re: Generic Application Form
« Reply #6 on: May 23, 2009, 08:26:09 PM »
I'm surprised you didn't put "yes" or "no" as options for "sex"! LMAO

Reading can seriously damage your ignorance!

JPDeni

  • Guest
Re: Generic Application Form
« Reply #7 on: May 23, 2009, 08:30:46 PM »
Quote
I'm surprised you didn't put "yes" or "no" as options for "sex"! LMAO

:2funny:

Quote
Should we put this in the Block Code Snippets Board as a [Block]?

That's where I thought I had it.  :uglystupid2:

Offline FUBAR

  • Jr. Member
  • **
  • Posts: 98
Re: Generic Application Form
« Reply #8 on: May 25, 2009, 10:00:17 PM »
Thanks for posting the code here JPDeni.  :)

I had one question for you though, I noticed when submitting the application the thread title reads.."[Pending] Application of " but doesn't include the name of the user submitting the form.  Is it supposed to work this way? 

It would be confusing having the same title over and over in a board.   ;)

JPDeni

  • Guest
Re: Generic Application Form
« Reply #9 on: May 25, 2009, 10:27:12 PM »
I had taken that code from where someone had altered it for themselves. I guess I didn't look at it really closely. The person had set the post title to correspond with his own field names. You can alter the line to match your own field names. Just do a search for "[Pending]" and then put the field name in the brackets. Like:

Code: [Select]
'subject' => '[Pending] Application of ' . $_REQUEST['realname'],

I just changed it to match one of the sample fields that I have in my code. Put your own field name instead of realname.