Welcome Guest, Not a member yet? Register   Sign In
AJAX or NO AJAX?
#1

[eluser]Deep Arora[/eluser]
I just started with the AJAX stuff and have a dumb question..

I understand that we can check in CI if the request is ajax or not:

Code:
if (IS_AJAX) {

But doesn't this make our overall code lengthy? I mean, now we need to write the same code twice..one the ajax version, and one without ajax?

Second - if javascript is disabled in the broswer, how will we determine if ajax is failed. That is, how do we auto route to without-ajax part of code in case ajax fails for some reason? Because in my view, I will simply call the javascript function. Once it is called, its ajax call..so do i need to determine it in VIEW itself?

I may have more questions, but little help to begin with is appreciated.

Thanks.
#2

[eluser]kgill[/eluser]
[quote author="Deep Arora" date="1263606926"]I just started with the AJAX stuff and have a dumb question..

I understand that we can check in CI if the request is ajax or not:

Code:
if (IS_AJAX) {

But doesn't this make our overall code lengthy? I mean, now we need to write the same code twice..one the ajax version, and one without ajax?
[/quote]
No, never re-write the same logic, that's a waste. Instead move the common code into a separate function(s) and have each call that so that only the code that is specific to each request needs to be written.

Quote:Second - if javascript is disabled in the broswer, how will we determine if ajax is failed. That is, how do we auto route to without-ajax part of code in case ajax fails for some reason? Because in my view, I will simply call the javascript function. Once it is called, its ajax call..so do i need to determine it in VIEW itself?

If javascript is disabled, AJAX isn't even going to happen there's no need to determine if it failed. So you have to decide do you want the app to always work or do you want to tell the user if you don't want to use JS then you can't use the app. If it's the former, you need to code your apps for graceful degradation, make sure everything works with JS turned off first then you start capturing events in the browser to trigger your AJAX stuff. If it's the latter a simple no script tag stating the app won't work without JS covers that.
#3

[eluser]Deep Arora[/eluser]
Thanks..

Is there any CI AJAX library that most of us use here? I found one but that seems to be outdated.
#4

[eluser]Colin Williams[/eluser]
Usually the only difference with an Ajax request is that the output is different.

Code:
$posts = $this->blog->get_posts();
if (IS_AJAX)
{
   echo json_encode($posts);
}
else
{
   $this->load->view('posts', array('posts' => $posts));
}
#5

[eluser]Deep Arora[/eluser]
jscon_encode passes $posts from controller to ajax code. How do we pass this array ($posts) from ajax code to view and retrieve it there? A sample code is appreciated..
#6

[eluser]Colin Williams[/eluser]
I have no clue what you mean by that.
#7

[eluser]Deep Arora[/eluser]
Ok, let me try this...

My custom ajax file looks like this:

Code:
$(document).ready(function() {


     $('#submit_form').click(function() {
        var form_action = $('#form_action').val();
        var user_name = $('#user_name').val();
        var user_password = $('#user_password').val();

        $.post("http://localhost:8080/devbench/index.php/global/login/dologin", { 'user_name': user_name },
              function(data){
                $('#error-msg').replaceWith(data.result);


          }, "json");

    });
});


In my View, I have this to show the login errors;

Code:
<div class="response-msg success ui-corner-all" id="response-msg">
                            <div id="error-msg">Welcome back. Please enter your username and password to continue..</div>
                            </div>


Now, in my controller, I capture login errors at two places.

One, if form validation fails:

Code:
if ($this->form_validation->run() == FALSE)
        {
            $data = array('result' => $this->lang->line('error_missing_fields'));
            echo json_encode($data);
        }

And second, when the user enters invalid login details:

Code:
if (!$this->session->userdata('user_id'))
                    $data = array('result' => $this->lang->line('error_invalid_login'));
                    echo json_encode($data);
            }

The problem happening is that whatever error is encountered first, it keeps showing that error always..i.e., lets say I enter invalid login details, then it shows correct message as "login details invalid"...but then if I remove the username and pwd on the login screen and hits submit again, it still shows the same message...whereas it should show me the first error message "Mandatory fields are missing"...

I refresh the page, and this time, I hit the login button without entering anything..it shows the correct message "Mandatory fields are missing"...but now if I enter login details and hit submit, it still shows the same message, instead of "login details invalid"..

I should be doing something stupid..I guess I need to reset that array that gets passed to json_encode somewhere, I tried but it doesnt work...

any help??
#8

[eluser]Colin Williams[/eluser]
Use something like Firebug or Webkit inspector to debug the request and response.
#9

[eluser]The Wizard[/eluser]
he probberly is looking for something like that:

Code:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
#10

[eluser]axmed[/eluser]
am facing the same dilemma to use or not to use JavaScript. try reading about progressive enhancement here is a good article at alistapart.
if you will use JavaScript try using a framework like Jquery then you can use this library for CI called taconite they have demos you can try out.




Theme © iAndrew 2016 - Forum software by © MyBB