Custom Forms
Contacular can use preset contact form types or custom, bespoke setups, as demonstrated here
In previous versions of Contacular, you could only implement contact forms with the built it preset form types than the Contacular class provides. In the newer versions of Contacular (0.15 'evolution' and above) you can easily create a custom PHP contact form using the 'addField' function.
You should also note that these custom contact forms are entirely flexible and support all the same features as a preset setup Contacular form.
Take a look at the custom form code below. Note that this example initialises the Contacular class with the 'custom' type, which generates a blank form. After this, the 'addField' function is used to add the custom fields to the form.
<?
$form = new ContacularForm("custom"); // Create a custom (blank) Contacular Form
// Add custom fields, defining their name, human readable label, field type, height,
// width and other options where required.
$form->addField("from_name", "Name", "mandatorytext", null, 200);
$form->addField("age", "Age", "mandatorytext", null, 50);
$form->addField("gender", "Gender", "select", null, 70, array("Male", "Female"));
$form->addField("from_email", "E-mail", "email", null, 200);
$form->addField("enquiry", "Message", "mandatorytextarea", 100, 200);
$form->addField("picture", "Add a picture", "file");
$form->addField("response", "Want a response?", "checkbox");
$form->addRecipient("foobar@contacular.co.uk"); // Add a recipient
if ($form->processResponse()) // If we processed a response from the form...
{
echo "Thank you for contacting us!"; // display a 'thanks' message
}
else // otherwise
{
echo $form->getErrors(); // show any validation errors
echo $form->getCode(); // and output the form code.
}
?>
This demonstration code shows the use of various Contacular form field types. The Contacular form field types used in this example are described below.
- mandatorytext - The same as a standard text type, but with existance check validation.
- select - This generates a select (drop down) box, the available options are specified in the array passed to the AddField function, as shown in the example above.
- email - The same as a standard text type, but with e-mail address validation applied.
- mandatorytextarea - The same as a standard textarea (multiline text) type, but with existance check validation.
- file - Provides an option to upload a file within the form, which is then sent to the Contacular form's recipient(s) as an attachment.
- checkbox - Provides a checkbox type which allows a basic yes/no response to a question.
The contact form below shows how the example code on this page will look.
