Welcome Guest, Not a member yet? Register   Sign In
Using Ajax for CI with forms
#1

[eluser]ufasoli[/eluser]
Hello again,
I have been asking a lot of questions lately, and you have been really helpful guys, so here is another one...

Here is the situation, I have an inscription form that is 4 pages and I've decided to do all the validation through Ajax and even though I have done this before manually ( This is my first time using a framework) I'm having trouble understanding how Ajax for code Igniter works...

What I want to do is when the user clicks on the next button to submit the entire form through Ajax do my validation on a function in my controller named page_1 and if there are errors update a div in my view with an error message using as well an effect to make it look more "sexy" ( I hope I've been clear on what I would like to achieve). Here is my code


this is my controller function
Code:
function page_1()
{
        
        
    ..... some validation    
            
           if (!$error) // if some error occurred during validation
            {
                $this->ajax->visual_effect('BlindUp','testAjax');      
                $this->ajax->replace_html('testAjax', 'test');
            }
        
        //$this->validation->set_rules($rules);
            echo "test";


this is my view
Code:
echo $this->ajax->form_remote_tag( array('url'=>'page_1') ) ;

once the view has been parsed here is what I get :


Code:
<form method="post" onsubmit="new Ajax.Request('page_1',{evalScripts:true,     parameters:Form.serialize(this)}); >

The thing is that I'm able to recover the post data that I transmit through Ajax, but I can't do the update thing on the Dom Element

Thanks in advance

Ulises
#2

[eluser]Crafter[/eluser]
Are you implementing a "wizard" like form.

Then in your view you need a placeholder for your form stuff
Code:
<div id='form_container'>
&lt;form method="post" onsubmit="new Ajax.Request('page_1.php',{evalScripts:true,     parameters:Form.serialize(this)}); &gt;
...
&lt;/form&gt;
&lt;/div&gt;

You also need to ensure your Ajax.Request call has a sucess handler, which will process teh output from page1.php and overwrite the contents of form_container.

Also noteh that the evalScripts does not always work as advertised, so rather don't embed scripts in your AJax generated code - rtaher keep them in your main file amd refere to them as you need.
#3

[eluser]ufasoli[/eluser]
Hi and thanks for the answer, I finally managed to get things working, I just needed to understand how did CI managed the Ajax functions, you just need to echo them in a div and it works like a charm.. However I have another question.

What I do now is change the innerHTML of a div if the validation succeeded with an appear effect, which actually works the only problem is that I get a 'Notice error' in the page that makes it look awful

here is the code

This is the ajax - helper function that I use to generate the effect

Code:
echo $this->ajax->tag($this->ajax->visual_effect('Fade', 'formulaire', $options = array('duration'=>'0.5')));

this is the output produced by the the visual effect function

Code:
new Effect.Fade('formulaire',{duration:0.5})

finally this is the error message I get from the page (even though the effect works fine)

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: queue

Filename: libraries/Ajax.php

Line Number: 872
#4

[eluser]Crafter[/eluser]
Ha. You stubled accross a debate that will go on for eternity.

There's two views:
a) Your program is displaying an error in the code, so even without your effect, it (in your own words) "makes it look awful".
The solution here is to test, fix, test, fix, test, fix. ...

b) Your code should include error handling. In my opinion, Code Igniter does a teriible job of error handling because it mostly just blurts out this
intrusive error message. My preffered way is to allow your application to capture and/or override the framework error handler.

Either way, it boils down to code quality. Wink
#5

[eluser]ufasoli[/eluser]
Hi,
thanks for your answer. The thing is that I'm quite new to programming so it's difficult for me... Still I don't get it, why the effect works but gives an error message anyway?? weird stuff...
Could you show me where I can find some info on handling errors myself instead of the application??
#6

[eluser]Crafter[/eluser]
Quote:Still I don’t get it, why the effect works but gives an error message anyway?? weird stuff…
Not at all. You are making a call to server, and that call is generating output in the form af an error message which you are then asking to place into a DIV. So the application is behaving as expected.

Quote:Could you show me where I can find some info on handling errors myself instead of the application??
http://www.php.net/manual/ - all you need to know, and more!
#7

[eluser]ufasoli[/eluser]
thanks for the help!!




Theme © iAndrew 2016 - Forum software by © MyBB