Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am developing site using codeigniter.I have a form which contains add button and textbox. once the user enters the data i have to check whether it exist in database,if yes generate a dynamic textbox in the page else alert user. I have written javascript to generate dynamic textbox. My question is how to check in database??? how to call controller from javascript or to call javascript function from controller???

share|improve this question

2 Answers 2

up vote 6 down vote accepted

This is actually so much easier than you expect.. and you will start to use this allover your developments once you see how great it is!

So first off -- we're going to use jQuery's native POST function.

Create a function inside your controller that you want to access, my suggestion is to prefix the function name with "ajax_"

So here's an example of the controller function:

function ajax_lookUpUsername(){
    $username = $this->input->post('username');
    $this->db->where('username', $username);
    $query = $this->db->get('accounts');
    if ($query->num_rows() > 0){
       echo 0;
    } else {
       echo 1;
    }
}

and here's your simple onclick javascript function:

function lookUpUsername(name){
    $.post( 
        'http://yourwebsite/controller/ajax_lookUpUsername',
         { username: name },
         function(response) {  
            if (response == 1) {
               alert('username available');
            } else {
               alert('username taken');
            }
         }  
    );
}

the second parameter { username: name } is where your post values will go, the term "username" here is the key, name is the value passed in. So this is a post key-value pair that would normally get sent with a post message.

The variable response being passed into the callback function is the echo given back by your controller. Communication made extremely easy.

The simplicity is amazing, while I only had you deal with php returning 0 or 1, you can return very advanced json objects that you can power an entire front-end program with.

For more advanced responses you can echo from your controller array's this way:

echo json_encode($array_of_data); 

and this will return to you a perfect json dataset that you can use with any object oriented approach. I use this allover, you will soon too im sure :)

Good luck man! Feel free to contact w/ any questions about expanding responses past simple 0 or 1 echos

share|improve this answer
    
Thank you very much.... I am developing autocomplete for the input box in the form....The model will pass array of data...How to display it in view??? –  user735399 May 3 '11 at 9:13
    
firstly you want to response a json_encode($array_stuff) from your controller, so wrap that function around the array you are echo-ing. Now you can read from the data set in javascript as you would any json object. response['key'] = value. depending on the structure of your returned array, you may have to iterate through it or just reference keys. Mark as answered if ur satisfied :) –  Atticus May 3 '11 at 9:53
    
Thank you....i marked it as answered:))) –  user735399 May 4 '11 at 5:49
    
No problemo -- if run into trouble converting your response into json -- try wrapping eval(response) in your call back funciton -- sometimes this needs to be done. Other than that, google search "parse json" for any trouble you may run into gathering results -- but it's pretty much treating it as an array --> response['key'] = value –  Atticus May 5 '11 at 2:57

You can do it by making an ajax call to a php page in the server that checks if data exists in db. If you are using jquery, you can do it in an easier way, here you can find very good examples: http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

If you don't, you can do it anyway, with some more lines of code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.