stefangabos / Zebra_Form

A jQuery augmented PHP library for creating secure HTML forms and validating them easily
Other
98 stars 48 forks source link

Automatic Form Rendering #40

Open userc479 opened 4 years ago

userc479 commented 4 years ago

It seems when you use the form render without using template and you have both form and coding before generating the form it causes a duplicate/time out issue which is frustrating as I want to show the form / details before render if form hasn't been submitted or unsuccessful - otherwise a successful would prompt show a thank you page.

This is the code (be kind to me....) - help is appreciated if anyone has the time - many thanks :)

`<?php

//Let's set variable here for form success as false
$site_form_success = false;

//Let's set reference number here
$reference_number = uniqid(true);

//Let's retrieve the data based on the reference passed over
$team_member_query = functions_mysql_select($sitedb, '', definitions_mysql_tables_site_users,'team_reference = ? AND team_active = ?' , array($reference,true), false);

//Do we have an error from attempting to get contact details from above
if ($team_member_query['error'])
{
    //It appears we do have an error- so let's not show the contact details and form
    echo 'Unable to retrieve team member contact details.';
    exit();

}else{

//No error from retrieving query so let's retrieve the actual results now
$team_member = $team_member_query['results'];

//Let's proceed with showing team member details if we have successful results
if($team_member)
{

    //Let's build up our form here

        //SECTION FOR NAME-----------------------------------------------------------------------------------------------------

        // the label for the "name" element
        $form->add('label', 'label_name', 'name', 'Your name:');

        // add the "name" element
        $obj = $form->add('text', 'name', '', array('data-prefix' => '<i class="fa fa-user" style="color: black;" aria-hidden="true"></i>','autocomplete' => 'on'));

        //Let's force the case to appear in Upper case but we'll change it before we save the record
        $obj->change_case('upper');

        // set rules for name
        $obj->set_rule(array(

        // error messages will be sent to a variable called "error", usable in custom templates
        'required' => array('error', 'We need your name.'),
        'length' => array(3,100,'error','Please provide your name (minimum 3 characters and maximum 100 characters')

        //End of rules 
        ));

        //Add a note for this section
        $form->add('note', 'note_name', 'name', 'Please provide your name so we know who we are talking to.');

        //SECTION FOR EMAIL-----------------------------------------------------------------------------------------------------

        // the label for the "name" element
        $form->add('label', 'label_email', 'email', 'Your email address:');

        // add the "name" element
        $obj = $form->add('text', 'email', '', array('data-prefix' => '<i class="fa fa-envelope" style="color: black;" aria-hidden="true"></i>','autocomplete' => 'on'));

        // set rules for name
        $obj->set_rule(array(

        // error messages will be sent to a variable called "error", usable in custom templates
        'required' => array('error', 'We need your email address.'),
        'length' => array(3,100,'error','Please provide your email address (minimum 3 characters and maximum 100 characters'),
        'email'     =>  array('error', 'Email address seems to be invalid!')

        //End of rules 
        ));

        //Add a note for this section
        $form->add('note', 'note_email', 'email', 'Please provide your email address. We will never provide your email address to anyone beyond our team without consent (with exceptions being authorities in some cases).');

        //SECTION FOR MESSAGE-----------------------------------------------------------------------------------------------------

        // the label for the message element
        $form->add('label', 'label_message', 'message', 'Your message:');

        // add the "name" element
        $obj = $form->add('textarea', 'message', '', array('data-prefix' => '<i class="fa fa-pencil" style="color: black;" aria-hidden="true"></i>','autocomplete' => 'off','alphanumeric'));

        // set rules for name
        $obj->set_rule(array(
        //Add a note for this section

        // error messages will be sent to a variable called "error", usable in custom templates
        'required' => array('error', 'We need to know what you would like to talk to us about.'),
        'length' => array(10,2000,'error','Please provide your message (minimum 10 characters and maximum 2000 characters',true),
        'alphanumeric' => array('-.!"\'%&() ', 'error', 'Unacceptable characters found in the message.')
        //End of rules 
        ));

        //Add a note for this section
        $form->add('note', 'note_message', 'message', 'Let us know what you like to message us about (please keep it constructive.');

        //SECTION FOR  HIDDEN GOOGLE CAPTCHA-----------------------------------------------------------------------------------------------------
       $obj = $form->add('hidden', 'recaptchaResponse', 'recaptcha_response');

        //SECTION FOR  SUBMIT BUTTON-----------------------------------------------------------------------------------------------------

        // "submit"
        $form->add('submit', 'btnsubmit', 'Submit message.');

            //Add a note for this section
        $form->add('note', 'note_submit', 'btnsubmit', 'Please note, we use Google Captcha to help us filter spam messages etc.');

        //VALIDATION STARTS HERE-------------------------------------------------------------------------------------------------------------------------------
        if ($form->validate()) {

        }else{

        }

        //NO SUCCESS ON FORM OR NOT SUBMITTED YET---------------------------------------------------------------------------------------------------        

        if (!$site_form_success)
        {

            //Team member exists so let's output the information about our lovely team member
            echo '<h5 class="w3-center bold">'.$team_member['team_information_contact_page'].'</h5><br>';

           //Let's check if this team member can be contacted via telephone/mobile
            if($team_member['team_number'] OR $team_member['team_secondary_number'])
            {
                //Telephone header
                echo '<h2 class="w3-center bold">GET IN TOUCH VIA TELEPHONE</h2>';

                //Let's just give a friendly note here
                echo '<h5 class="w3-center bold">You may get in touch with '. ucfirst(strtolower($team_member['forename'])).' via the following telephone number(s):</h5>';

                //Set up our telephone number section here
                echo '<h4 class="w3-center bold">';

                //Let's output those numbers :)
                if (($team_member['team_number']) && (!$team_member['team_secondary_number']))
                {
                    //The user only has one number available - possibly a landline number
                    echo ($team_member_contact['team_number']);

                }elseif((!$team_member['team_number']) && ($team_member['team_secondary_number']))
                {
                    //The user only has one number available - possibly a mobile number
                    echo ($team_member['team_secondary_number']);

                }elseif(($team_member['team_number']) && ($team_member['team_secondary_number']))
                {
                    //My my my, the user has both possible landline AND a mobile number 
                    echo ($team_member['team_number']).' OR '.($team_member['team_secondary_number']);
                }

                //Let's close our telephone section
                echo '</h4>';

                //Let's just output a nice little notice about contacting us via telephone
                echo '<h6 class="bold w3-center w3-small justify">'.definitions_message_respect_contact_telephone.'</h6><br>';        
            }

        //Okay, so we've dealt with telephone section now, let's create the form part now by doing the Email header
        echo '<h2 class="w3-center bold" id="getintouchemail">GET IN TOUCH VIA EMAIL</h2>';

        //Let's output a message for those with Javascript Disabled here
        echo '<h4 class="w3-center bold"><noscript>THIS FORM MAY NOT WORK AS YOU HAVE JAVASCRIPT DISABLED</noscript></h4>';

        //Let's show nice part above email bit
        echo '<h5 class="w3-center bold">Alternatively, you may contact us via Email using the form below.<br>PLEASE FILL OUT ALL FIELDS - Thank you!</h5><br>';

        //Render out the form    
        $form->render();

        }else{

        }

}else{

    //Team member doesn't exist - so let's inform the user
    echo '<h3 class="w3-center">It appears this team member doesn\'t exist or isn\'t currently active.<br>Please contact an alternative team member via the Get In Touch page.<br><br>Thank you. </h3>';

}

}

?>`

stefangabos commented 4 years ago

I am not sure what you are asking, but you can get the rendered form as a variable like $output = $form->render('', true);

userc479 commented 4 years ago

Basically if you have a a fair amount of HTML before the Automatic Render (without template) it returns duplicate / timeout error. I'll try what you said.

userc479 commented 4 years ago

Doesn't work :(

userc479 commented 4 years ago

Just frustrated - delete this please - sorry.... i'm trying to figure it out... :(

stefangabos commented 4 years ago

Try disabling CSRF

$form->csrf(false)
userc479 commented 4 years ago

Stefangabos - disabling CRSF works but I don't want to disable that as would be a security issue.

userc479 commented 4 years ago

I think it was something to do with whatever information was being held via Sessions through Zebra Sessions - it's not showing up the errors - doing development stages on my laptop rather than site in operation - whether that makes a difference - I don't know