CodeIgniter Forums
Problem with form_validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Problem with form_validation (/showthread.php?tid=62902)



Problem with form_validation - StratoKyke - 09-06-2015

This is the code:
controller:
Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Contact extends MY_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->model('contacts');
    }
    public function recaptcha($str=''){
        $google_url="https://www.google.com/recaptcha/api/siteverify";
        $secret= info('privatekey', 'site');
        $ip=$_SERVER['REMOTE_ADDR'];
        $url=$google_url."?secret=".$secret."&response=".$str."&remoteip=".$ip;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
        $res = curl_exec($curl);
        curl_close($curl);
        $res= json_decode($res, true);
        if($res['success']){
            return TRUE;
        }else{
            $this->form_validation->set_message('recaptcha', 'Il campo reCAPTCHA mi dice che sei un robot. Vuoi fare un altro tentativo?');
            return FALSE;
        }
     }
    public function index() {
               $rules = array(
                array(
                    'field'=>'name',
                    'label'=>'Name',
                    'rules'=>'trim|required|min_length[6]|max_length[16]'
                ),
                               array(
                    'field'=>'surname',
                    'label'=>'Surname',
                    'rules'=>'trim|required|min_length[6]|max_length[16]'
                ),
                               array(
                    'field'=>'email',
                    'label'=>'Email',
                    'rules'=>'required|min_length[6]|valid_email'
                ),
                               array(
                    'field'=>'subject',
                    'label'=>'Subject',
                    'rules'=>'trim|required|min_length[3]|max_length[16]'
                ),
                array(
                    'field'=>'g-recaptcha-response',
                    'label'=>'Captcha',
                    'rules'=>'callback_recaptcha'
                ),
                               array(
                    'field'=>'text',
                    'label'=>'text',
                    'rules'=>'required|min_length[6]'
                )
            );
        $this->form_validation->set_rules($rules);
        if($this->form_validation->run() == FALSE) {
            $this->smarty->view('contact.tpl');
        } else {
            $this->contacts->send();
            $this->smarty->view('contactSuccess.tpl');
        }
    }
}

VIEW:

Code:
<section id="content" class="content">
    <div>
        {if "{info('contact', 'function')}" == "1"}
            <header class="boxHeadline">
                <h1>{varL('star_contact')}</h1>
            </header>
            <article>
                               {form url="contact" type=""}
                {if validation_errors() != ""}<div id="errorMessage" class="error">{validation_errors}</div>{/if}
                    <fieldset class="form">
                        <dl>
                            <dt>
                                <label for="Name">{varL('star_contact_name')}</label>
                            </dt>
                            <dd>
                                <input type="text" id="name" name="name" />
                            </dd>
                        </dl>
                                               <dl>
                            <dt>
                                <label for="Surname">{varL('star_contact_surname')}</label>
                            </dt>
                            <dd>
                                <input type="text" id="surname" name="surname" />
                            </dd>
                        </dl>
                                               <dl>
                            <dt>
                                <label for="Surname">{varL('star_contact_email')}</label>
                            </dt>
                            <dd>
                                <input type="email" id="email" name="email" />
                            </dd>
                        </dl>
                                               <dl>
                            <dt>
                                <label for="Subject">{varL('star_contact_subject')}</label>
                            </dt>
                            <dd>
                                <input type="text" id="subject" name="subject" />
                            </dd>
                        </dl>
                                               <dl>
                            <dt>
                                <label for="Text">{varL('star_contact_text')}</label>
                            </dt>
                            <dd>
                                <textarea type="text" id="text" name="text"></textarea>
                            </dd>
                        </dl>
                        <dl>
                            <div class="g-recaptcha" data-sitekey="{info('publickey', 'site')}"></div>
                        </dl>
                        <div class="formSubmit">
                            <input type="submit" id="SubmitButton" name="submitButton" value="{varL('star_button_contact')}" />
                        </div>
                    </fieldset>
                {form}
            </article>
        {else}
            <header class="boxHeadline">
                <h1>{varL('star_function_denied')}</h1>
            </header>
            <article>
                <div id="errorMessage" class="error">{varL('star_access_denied_description')}</div>
            </article>
        {/if}
    </div>
</section>


The problem is that in the local with xampp everything works perfectly. But when I try to use the form in my vps when sending the form it is as if I reflesh page. Without sending the form and the data. For this reason there is not control of form and the associated error messages.


How can I fix?


RE: Problem with form_validation - StratoKyke - 09-06-2015

The problem is fixed.


RE: Problem with form_validation - RobertSF - 09-07-2015

I just read the original and was stumped. Smile

What was the solution? It might help someone else.