HOW TO save form submission result to database?

Discuss everything related to FormBuilder. All versions. Any questions related to the FormBuilder developing or using should be posted to this forum.

Moderators: alexandrleonenko, goas

goas
Newbie
Newbie
Posts: 9
Joined: Apr 23rd, '12, 11:26

HOW TO save form submission result to database?

Postby goas » Jun 20th, '13, 10:31

If you want to save submitted data to database you can use following:
  • $form->IfSubmitted(); this function will show you, if the form was submitted
  • $form->IsError(); this function will tell if there were any errors
  • $form->GetResultArray(); this function contains form result, checked and filtered

The general way to check submission is following:
if($form->IfSubmitted() && !$form->IsError()) {
... your code is here
}


IMPORTANT: $form->IsError() works only if called after $form->GetHtml();

Your code will be looked as:

Code: Select all

$form = new FG_Form("name_email_form"); /* create a form */
// add two fields and a submit button to form
$form->AddTextField('name', 'Name')->IsRequired(1);
$form->AddTextField('email', 'E-mail')->IsRequired(1)->IsEmail(1);
$form->AddSubmitButton("go", "submit");
$form->GetHtml(); /* show the form */

// now when a form was submitted and has no errors it can be saved in database
if($form->IfSubmitted() && !$form->IsError()) {   
    $result = $form->GetResultArray(); /* get result array checked and filtered */
    unset($result['go']);  /* delete submit button field from result array */
    mysql_query(query_from_array("some", $result));   /* send query to currently active database */
}


Used functions:

Code: Select all

// function generates simple insert query from an array
function query_from_array($table_name, array $data){
    $keys = '';
    $values = '';
    foreach($data as $key =>$value){
        $keys .= "$key, ";
        $values .= "'$value', ";
    }
    $keys[strlen($keys) - 2] = ' ';
    $values[strlen($values) - 2] = ' ';
    $query = " insert into '$table_name' ($keys) values ($values)";
    return $query;
}

Return to “ApPHP FormBuilder {developers/users}”