---
title: "PHP Scripts - Convert Forms"
description: "Are you looking for a way to do some extra field validation during form submission? Perhaps, you'd like to silently post submitted data to another U"
url: "https://www.tassos.gr/docs/convert-forms/developers/php-scripts"
date: "2026-05-06T22:42:27+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/index.php?option=com_content&view=category&layout=blog&id=24&Itemid=1088) / [ Convert Forms ](https://www.tassos.gr/index.php?option=com_content&view=category&id=43) / [ Developers ](https://www.tassos.gr/index.php?option=com_content&view=category&id=61)

#  PHP Scripts

Are you looking for a way to do some extra field validation during form submission? Perhaps, you'd like to silently post submitted data to another URL? With the Convert Forms PHP Scripts section and the proper knowledge of PHP and MySQL you can do just about anything.

![Joomla! Forms with Custom PHP Scripts execution support](https://www.tassos.gr/images/convertforms_phpscripts.png)

## [Form Prepare](#form_prepare)

The PHP script added in this area is executed just before the form's data is prepared and sent to the form display function. The main focus in this area is the **$form** (Array) variable which contains the form's settings. This area is rather useful when you need to populate dynamically a field, add more options to a dropdown listbox or modify the styling of the form.

### [Populate a Textbox Field](#populate_textbox)

This example sets the default value of the field 'email'.

 ```
$form['fields']['email']['value'] = 'my@email.com';
```

### [Populate a Checkboxes Field](#populate_checkboxes)

The following example inserts new options to a Checkboxes field and changes its value:

 ```
// The name of the field
$fieldName = 'checkbox';

// The list of the options
$options = [
    [
        'label' => 'Red',
        'value' => 'red'
    ],
    [
        'label' => 'Blue',
        'value' => 'blue'
    ],
    [
        'label' => 'Green',
        'value' => 'green'
    ]
];

// Optionally, you can pre-check some options using this variable
$value = ['blue', 'red'];

// DO NOT EDIT BELOW
$form['fields'][$fieldName]['choices']['choices'] = $options;
$form['fields'][$fieldName]['value'] = $value;
```

### [Populate a Dropdown with data from the database](#populate_dropdown_from_db)

The example below returns all articles from the database and adds each article data zs an option to a dropdown field

 ```
$query = $db->getQuery(true)
    ->select('id, title')
    ->from('#__content');

$db->setQuery($query);

$articles = $db->loadObjectList();
$choices = [];

foreach ($articles as $article)
{
    $choices[] = [
        'value' => $article->id,
        'label' => $article->title,
    ];
}

$form['fields']['articlesDropdown']['choices']['choices'] = $choices;
```

### [Advanced Examples](#form_prepare_advanced)

A list of advanced examples that can be used before the form has been prepared and served to the user.

- [How to Auto-Populate a Form Field with an Article Data](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-auto-populate-a-form-field-with-an-article-data)
- [How to Populate Drop Down, Radio Buttons or Checkboxes with a CSV File](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-populate-drop-down-radio-buttons-or-checkboxes-with-a-csv-file)

## [Form Display](#form_display)

The PHP script added in this area is executed just before the form is displayed. The main focus in this area is the **$formLayout** (String) variable which contains the HTML code of the form. You can also access the form settings here using the **$form** (Array) variable.

### [Restrict access to a form](#restrict_access_to_form)

In this example, guests will see a warning message while logged-in users will see the actual form.

 ```
if ($user->guest) {
	$formLayout = '<p>Please, log in to access this form.<p>';
}
```

### [Display form submissions count](#display_submissions_count)

The total number for form submissions can be helpful if you want to display the number of users who participated in a survey or have entered content. This number can easily be calculated with the example below:

 ```
$count = ConvertForms\Api::getFormSubmissionsTotal($form['id']);
$formLayout .= 'This form was submitted ' . $count . ' times.';

```

### [Hide the form after X submissions](#hide_form_after_x_submissions)

The example below will hide the form after it has been submitted X number of times.

 ```
$count = ConvertForms\Api::getFormSubmissionsTotal($form['id']);
if ($count >= 50) {
     $formLayout = '';
}
```

### [Hide form on a mobile device](#hide_form_on_mobile)

The example below uses the Convert Forms API to detect if the user is browsing with a mobile device and removes completely the form from the page.

 ```
if (ConvertForms\Api::isMobile()) {
   $formLayout = "";
}

```

## [Form Process](#form_process)

The PHP code added in this area is executed just before the form data has been saved into the database regardless if the submission is valid or not. This area is rather useful when you need to process calculations, make advanced validation or modify the value of a field. Any modifications to the **$post** (Array) variable performed here, will be reflected in the submission entry. You can also access the form settings here using the **$form** (Array) variable.

### [Populate a Field](#populate_field)

This example changes the post variable for the field name

 ```
$post['name'] = 'John Doe';
```

### [Populate a field using the value from another field](#populate_field_with_another_field)

This example changes the post variable for field text\_1 to the value of field text\_2

 ```
$post['text_1'] = $post['text_2'];
```

### [Custom Validation Error](#validate_limit_characters)

This example displays an error message when the field message exceeds the characters limit

 ```
$max_chars = 50;
$error = "Maximum character limit reached.";

if (strlen($post["message"]) > $max_chars) {
   throw new Exception($error);
}
```

### [Limit how many submissions your form accepts](#limit_number_of_submissions)

This example makes your form accept X number of submissions and once the limit is reached, an error message will appear and it won't accept any new submissions.

 ```
// Set the maximum submissions
$max_submissions = 40;

// Specify the error message for reaching maximum submissions.
$error_message = 'We do not accept further submissions.';

// Do not edit below
if (ConvertForms\Form::getSubmissionsTotal($form['id']) >= $max_submissions)
{
    throw new Exception($error_message);
}
```

### [Advanced Examples](#form_process_advanced)

A list of advanced examples that can be used just before the form data has been saved into the database regardless if the submission is valid or not.

- [How to Ensure a Unique Value is Entered Into a Field](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-ensure-a-unique-value-is-entered-into-a-field)
- [How to Block Form Submissions Containing Profanity (Bad Words)](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-block-form-submissions-containing-profanity-bad-words)
- [How to Authenticate and Login a User with a Custom Joomla Form](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-authenticate-and-login-a-user-with-a-custom-joomla-form)
- [How to Allow Form Submissions in Specific Date Range](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-allow-form-submissions-in-specific-date-range)

## [After Form Submission](#after_form_submission)

The PHP code added in this area is executed after the form has been successfully submitted and the data has been saved into the database. This is rather useful when you are trying to run some extra tasks such as silently posting data to another URL or feeding data to third-party applications. The **$submission** (Array) variable is available in this hook and contains all submitted values.

### [Delete submission from the database](#delete_submissions_from_the_database)

There are cases when you'd like to delete the submitted data from the database after the submission has been processed. The example below uses the **submission\_delete()** built-in API method to delete the current submission from the database.

 ```
ConvertForms\Api::removeSubmission($submission->id);
```

### [Modify the form success message](#modify_the_success_message)

The example below modifies the success message that will be displayed in the form

 ```
$submission->form->successmsg = "We have received your request. Submission ID: #" . $submission->id;

```

### [Display a different success message based on a field value](#success_message_conditionally)

The example below modifies the success message based on the value of a submitted field

 ```
$field_value = $submission->params['somefieldkey'];
$message = $field_value == 'somevalue' ? 'some message' : 'alternative message';
$submission->form->successmsg = $message;
```

### [Advanced Examples](#after_form_submission_advanced)

A list of advanced examples that can be used after the form has been submitted

- [How to Automatically Save Each Submission to a JSON file](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-automatically-save-each-submission-to-a-json-file)
- [How to Silently POST Submitted Data to Any API or URL](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-silently-post-submitted-data-to-any-api-or-url)
- [How to Automatically Delete Submissions Older Than X Days](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-automatically-delete-submissions-older-than-x-days)
- [How to Conditionally Redirect User to a URL After Form Submission](https://www.tassos.gr/joomla-extensions/convert-forms/docs/how-to-redirect-the-user-after-a-successful-submission#conditional_redirect)

## [Supported PHP variables](#supported-php-variables)

Convert Forms provides some variables within PHP Scripts sections for commonly used jobs. You can find the list below:

 | Variable Name | Description |
|---|---|
| $app | The Joomla Application Object |
| $doc | The Joomla document object |
| $db | The Joomla database object |
| $user | The currently logged-in user object |
| $itemid | The "Itemid" value of the current request |

## [Troubleshooting](#troubleshooting)

### [My PHP code does not run. I don't even see an error message.](#my-php-code-does-not-run-i-don-t-even-see-an-error-message)

It's very likely your PHP code is ignored because it contains one of the following forbidden functions:

- fopen
- popen
- unlink
- rmdir
- dl
- escapeshellarg
- escapeshellcmd
- exec
- passthru
- proc\_close
- proc\_open
- shell\_exec
- symlink
- system
- pcntl\_exec
- eval
- create\_function

Additionally, when a PHP snippet contains backticks `` won't be executed too. In this case, you should replace backticks with single quotes.

## [Frequently Asked Questions](#faq)

#### [Can I include PHP files within my code?](#include_php_file)

The PHP Scripts sections allow you to include any external file from within your Joomla installation as long as you have the correct path. This is quite useful if you need to use the same PHP code on multiple forms, you can place the code in a PHP file and include it in your form's PHP Scripts section.

 ```
include 'file/to/path.php';
if (functionFromFile())
{
	// Do something...
}
```

Note: For debugging purposes, you can always use die(); statements after echo or var_dump() commands. For example: var_dump($post); die();

 Last updated on Mar 3rd 2026 09:03

## Schema

```json
{
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
        {
            "@type": "ListItem",
            "position": 1,
            "name": "Home",
            "item": "https://www.tassos.gr"
        },
        {
            "@type": "ListItem",
            "position": 2,
            "name": "Home",
            "item": "https://www.tassos.gr/docs"
        },
        {
            "@type": "ListItem",
            "position": 3,
            "name": "Convert Forms",
            "item": "https://www.tassos.gr/docs/convert-forms"
        },
        {
            "@type": "ListItem",
            "position": 4,
            "name": "Developers",
            "item": "https://www.tassos.gr/docs/convert-forms/developers"
        },
        {
            "@type": "ListItem",
            "position": 5,
            "name": "PHP Scripts",
            "item": "https://www.tassos.gr/docs/convert-forms/developers/php-scripts"
        }
    ]
}
```

```json
{
    "@context": "https://schema.org",
    "@type": "Article",
    "mainEntityOfPage": {
        "@type": "WebPage",
        "@id": "https://www.tassos.gr/docs/convert-forms/developers/php-scripts"
    },
    "headline": "PHP Scripts",
    "image": {
        "@type": "ImageObject",
        "url": "https://www.tassos.gr/"
    },
    "publisher": {
        "@type": "Organization",
        "name": "Tassos",
        "logo": {
            "@type": "ImageObject",
            "url": "https://www.tassos.gr/https://www.tassos.gr/media/brand/logo-text.png"
        }
    },
    "author": {
        "@type": "Person",
        "name": "Tassos Marinos",
        "url": "https://www.tassos.gr/docs/convert-forms/developers/php-scripts"
    },
    "datePublished": "2019-03-12T12:20:57+02:00",
    "dateCreated": "2019-03-12T12:20:57+02:00",
    "dateModified": "2026-03-03T09:50:23+02:00"
}
```
