Welcome Guest, Not a member yet? Register   Sign In
Use AJAX to validate a field
#1

[eluser]xcristi[/eluser]
Hello guys,

So, I know little about AJAX, but I need to implement such thing in a CI application. I just need from some directions, because I'm a little confuse here...

I have a form (view) with a input text field, let's say 'name' and a DB with a table that contains field 'names'. What I want to do is to verify the value entered for name in table and display a message 'No duplicate' or 'Duplicate name' (or something like that).

I load the library ajax but I haven't an idea what to put into view and what into controller. Maybe you could show me some direction to follow.

Thanks.
#2

[eluser]rogierb[/eluser]
Implementing ajax into CI is a piece of cake. You can create a controller called 'ajax' or some other name you fancy. This controller is doing the checking, ofcourse db stuff go into the 'ajax' model, view stuff into view.

In your view which has the input field, do an ajax call like you normally would, where you call you ajax controller. like: hhttp://yoursite.com/ajax/checkmyfield/withthisvalue.

so you have:
ajax controller
ajax model
ajax field

and your nomal controller/view

Hope this helps.
#3

[eluser]xcristi[/eluser]
Let's say I have something as
Code:
<input type="text" name="zuzu" id="zuzu" onBlur="..."/>

A controller (without a model for the simplicity sake):
Code:
<?php

class Ajaxtest extends Controller {

function Ajaxtest() {
    parent::Controller();
}

function index() {
    return "ajax test";
}    

}
?>

and in my view <div id="test"></div>.

How can I display "ajax test" in the div above using ajax? What get to onBlur event?

Maybe there are silly questions, but have mercy, I really try to understand it.
Thanks
#4

[eluser]deviant[/eluser]
It really depends if you are using a JS framework and which one you are using.
#5

[eluser]rogierb[/eluser]
I would do something like this:

Code:
&lt;input type="text" name="zuzu" id="zuzu" onBlur="..."/&gt;<div id="ajaxreply"></div>

And I would not use the index() inside the Ajaxtst class but rather:
Code:
function check_field_something($value_to_check=null) {
   echo "this is the reply" //goes into view!
}

so I can re-use the ajax controller.

Now for the "onBlur" part. This all depends on you ajax class.
Let's dasy for arguments sake you normally use callAjax("http://somesite/ajax") as an ajax call. Now you would use

Code:
.. onBlur('http://mysite.com/ajaxtest/'+document.formname.zuzu.value())

to get a call to the controller. But it all depends on your ajax library.
#6

[eluser]xcristi[/eluser]
I want to use AJAX for CodeIgniter (http://codeigniter.com/wiki/AJAX_for_CodeIgniter/)

p.s. if you think there are easier solutions, please let me know
#7

[eluser]rogierb[/eluser]
I would go for jquery. Very lightweight and quite easy to understand. There are some good examples on how to use jquery on the jquery site.

Besides the ajax part, jquery can be used for some very iteresting effects.

But then again, picking an javascript library is a matter of personal taste.
#8

[eluser]xcristi[/eluser]
I will try JQuery and be back with questions Smile

Thanks rogieb.
#9

[eluser]xcristi[/eluser]
I try to put at work the codeigniter class from http://codeigniter.com/wiki/jQuery/.
Has anyone a short example using it? Maybe form related?

Thanks.
#10

[eluser]rogierb[/eluser]
Just include the the jquery JS in your template.


In your view:
Code:
function do_onblur() {
$("your_responsetext_id").append("
    $.ajax({
         type: "POST",
      url: "&lt;?=base_url()?&gt;/ajax/function/"+$("input[@name='youtfieldname']").val()+"/,
      async: false
     }).responseText;
");
}

and add do_onBlur() to your onblur function.
No need to incorporate jquery into a class unless you have the time.




Theme © iAndrew 2016 - Forum software by © MyBB