Operations with form data
After form data validation, we can extract fields values for further use.
//Value of specified field $form -> getValue("staff"); //Another way to get a field value $form -> staff; //Value (title, name) of "enum" type field (not a key) $form -> getEnumTitle("gender"); //All form values, returns an associative array $form -> getAllValues(); /Selected form values, returns an associative array $form -> getAllValues(array("company", "about", "staff")); //Pass form values into model object $data = $form -> getAllValues(array("company", "type", "email")); $mv -> clients -> getDataFromArray($data); $mv -> clients -> create(); //Pass form values into object of Record class to create/update a record $values = $form -> getAllValues(array("first_name", "last_name", "email")); $client = $mv -> clients -> findRecordById(34); $client -> setValues($values) -> update(); //Create a list of all fields to send e-mail $message = $form -> composeMessage(); //Create a list of certain fields in a required sequence to send e-mail $message = $form -> composeMessage(array("company", "about", "email"));
Let's assume we have an URL "/question/", by which the contact form should be opened. We will call the template file as "view-contact.php" and specify in "config/routes.php" file the line "question/" => "view-contact.php". Also, we will create in the module of pages the new page with the field "Link" that has "question" value.
Below is the content of "views/view-contact.php" file.
<? $content = $mv -> pages -> findRecord(array("url" => "question", "active" => 1)); $mv -> display404($content); $fields = array( array("Имя", "char", "name", array("required" => true)), array("Email", "email", "email", array("required" => true)), array("Message", "text", "message", array("required" => true)), array("Security code", "char", "captcha", array("required" => true, "captcha" => "extra/captcha-code/"))); $form = new Form($fields); if(!empty($_POST)) { $form -> getDataFromPost() -> validate(); if($form -> captcha) if(!isset($_SESSION["captcha"]) || $_SESSION["captcha"] != $form -> captcha) $form -> addError("Wrong security code."); if($form -> isValid()) { $message = $form -> composeMessage(array("name", "email", "message")); Email :: send($mv -> options -> admin_email, "Question from a website", $message); $mv -> reload("?sent"); } } include $mv -> views_path."main-header.php"; ?> <div id="content"> <div id="page" class="editable"> <h1><? echo $content -> name; ?></h1> <? echo $form -> displayErrors(); if(isset($_GET['sent'])) echo "<p>Your message was successfully sent.</p>"; else { echo $content -> content; $fields = array("name", "email", "message", "captcha"); //We put the form code into separated file and include it if needed include $mv -> views_path."parts/form-typical.php"; } ?> </div> </div> <? include $mv -> views_path."main-footer.php"; ?>
To use CAPTCHA we also need to switch on session use in config file.
Previous section
Form validation process