05
Days
23
Hours
59
Minutes
59
Seconds


Attaching more files based on user's selection

In today's digital world, the ability to upload and share multiple files easily is essential for many online forms, whether for job applications, school assignments, or other purposes.

RSForm!Pro allows only a file to be attached to the User Email through the Attach File option. However, there's a way to enhance this functionality to support multiple file attachments based on user selection. By adding a custom PHP script to the 'Script called before the User Email is sent' section, you can control which files that are stored on your server, are attached to your emails.

 

What to do


Case 1.

Let's consider a scenario where you have a form with a checkbox field (or a dropdown field) named 'forms'. This field allows users to select the specific files they wish to attach. The setup for this field would look something like this:

    form1|select this item to receive the 'form1' PDF file
    form2|select this item to receive the 'form2' PDF file
    form3|select this item to receive the 'form3' PDF file

Next, navigate to Form Properties > PHP Email Scripts. In the section named 'Script called before the User Email is sent', insert the following code:

    if(in_array('form1', $_POST['form']['forms']))
    $userEmail['files'][] = JPATH_SITE.'/path_to_your_form1.pdf';
    if(in_array('form2', $_POST['form']['forms']))
    $userEmail['files'][] = JPATH_SITE.'/path_to_your_form2.pdf';
    if(in_array('form3', $_POST['form']['forms']))
    $userEmail['files'][] = JPATH_SITE.'/path_to_your_form3.pdf';
 

Case 2.

Now let's assume that you want to attach a second file based on selections made on two different checkbox fields. Add one more checkbox field, named for example, 'agree' and craft it similar as below:

    1|yes, I agree
    2|no, I disagree

The code we'll need to use would be similar to this:

    if($_POST['form']['forms'][0] == 'form1' && $_POST['form']['agree'][0] == '1')
    $userEmail['files'][] = JPATH_SITE.'/path_to_your_form1.pdf';

The 'form1.pdf' file will be attached only if the two field items are selected, 'form1' - from the 'forms' field, and '1' - from the 'agree' field.

 

Case 3.

If you wish to attach a file to your User Email based on the condition that a textbox field is not left empty, you can achieve this by using the following code:

      if($_POST['form']['textbox_field_name'] != '')
      $userEmail['files'][] = JPATH_SITE.'/path_to_your_desired_file.pdf';
 

The examples mentioned above are not restricted to .pdf files; they were used merely as examples. You can attach any type of file stored on your server.

 

Was this article helpful?

Yes No
Sorry about that