Welcome Guest, Not a member yet? Register   Sign In
Issues with Ajax Form, Validation, and yes CSRF
#1

[eluser]otherjohn[/eluser]
Hi all,
I have a form loaded via Ajax and submitted via ajax. when the ajax form is loaded, a user can click the save button which using jQuery, submits the form using ajax.
I am getting 2 issues.

1.
When I submit the form, I always get $this->form_validation->run() == FALSE and it never runs to true.
But there is no validation rules setup.
2. if I hit submit again, i get a CSRF error because the token is different but the cookie hasn't changed.

I can't seem to get this simple form to work. There is no validation rules to trip it up! And CSRF token is being sent via ajax

Below is my code:

Code:
My controller Code

function roles($user_id=NULL)
{
if ($this->form_validation->run() == FALSE)
{
   ..... load form up
}
else
{

       echo "Submission worked!";
}
Code:
my ajax code
$('#saveUser').click(function() {
           var myurl = $("#userform").attr('action'); // GRAB THE URL FROM THE FORM
        
        $('#displayblock').hide('fast',loadContent);
        $('#wrapper').append('<span id="load">LOADING...</span>');
        $('#load').fadeIn('normal');  
        
        function loadContent() {  
        
            $.ajax(
            {
            type : 'POST',
            data: $("#userform").serialize(),
            url : myurl,
               dataType: 'html',
            error:  function(xhr, ajaxOptions, thrownError){
                            $('#displayblock').html(xhr.responseText).show('normal',hideLoader());  
                    
                    },
            success : function(data){
                $('#displayblock').html(data).show('normal',hideLoader());  
                }
            })
        }
        
        function hideLoader() {  
            $('#load').fadeOut('normal');  
        }  
        return false;

    });
#2

[eluser]otherjohn[/eluser]
I should mention that I have HMVC installed and just added
$this->load->library('form_validation', array('CI' => $this));
but I am getting a new error
ErrorException [ Fatal Error ]: Cannot use object of type Admin as array
SYSDIR/var/www/vhosts/churchbackend.com/_sys/libraries/Form_validation.php [ 90 ]

Admin is the controller's name




Theme © iAndrew 2016 - Forum software by © MyBB