CodeIgniter Forums
Why does $this->input->is_ajax_request() = FALSE? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Why does $this->input->is_ajax_request() = FALSE? (/thread-53496.html)



Why does $this->input->is_ajax_request() = FALSE? - El Forum - 07-26-2012

[eluser]bill19[/eluser]
Hi everyone,

I am working on a project and have come across a CI form generator on github at https://github.com/ariven/Form_Builder-for-CodeIgniter . I have been trying to get this working with help from the author ( a very nice guy ). I have been able to get the graceful fallback ( without JS ) working, but so far cannot get JS working.

Also, looking at application/config/config.php I dont even have CSRF protection on at all, so that shouldn't be the issue.

Would anyone mind looking at this?

Thanks,

Bill

P.S. My form has one field ('a' ) and a submit button for this test project.

Here is the view:

Code:
<!DOCTYPE html>
&lt;html&gt;
    &lt;head&gt;
        &lt;base href="&lt;? echo base_url();?&gt;"&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
        &lt;title&gt;&lt;/title>
    &lt;/head&gt;
    &lt;body&gt;
        <fieldset>
<legend>form</legend>
&lt;?php echo form_open("welcome/process", array("id" => "form", "class" => "form-horizontal")); ?&gt;
<div class="control-group">
<label class="control-label" for="a">a</label>
<div class="controls">
  &lt;input type="text" value="&lt;?php if (isset($a)) { echo $a; } ?&gt;" name="a" id="a" placeholder="a"  /&gt;  
  <span class="form_error" id="a_error">&lt;?php if (isset($error['a'])) { echo $error['a']; } ?&gt;</span>
</div>
</div>
<div class="control-group">
<div class="controls">
  <button type="submit" class="btn btn-primary" id="submit">Save</button> or <a >Cancel</a>
</div>
</div>
&lt;?php echo form_close(); ?&gt;
</fieldset>
        
        [removed][removed]
[removed][removed]
[removed][removed]
[removed][removed]
        
    &lt;/body&gt;
&lt;/html&gt;


Here is the controller:

Code:
function process() {
  $this->load->library('form_validation');
  $this->load->helper('url');
  
  $is_ajax = $this->input->is_ajax_request();
  $ajax_error = FALSE;

  $rules[] = array('field' => 'a', 'label' => 'a', 'rules' => 'trim|required');

  // load any variables to refill the form
  $variables['a'] = $this->input->post('a', TRUE);
  // alternately you can load them this way, if you are preloading an item from a database, and
  // you just want to overwrite the changed variables...
  /*
  $variables = $this->model_name->get($id);
  $posted = $this->input->post(NULL, TRUE);
  if ($posted) {
   foreach ($posted as $key => $value) {
    $variables[$key] = $value;
   }
  }
  */
  

  $this->form_validation->set_rules($rules);
  if ($this->form_validation->run() == FALSE) {
   // first load or failed form
   $error = array(
      'a' => form_error('a'),
    );
   $variables['error'] = $error;
   if ($is_ajax) {
    if (count($error) > 0) {
     $ajax_error = TRUE;
    }
    $message = $error;
   } else {
    //
    // change the view name to the name of what you save the view file as
    //
    $message = $this->load->view('form', $variables, TRUE);
   }
  } else {
   // perform action based on submission of form, if you return $message in this manner
   // it defaults to replacing the form in the page with the message that is returned.
   $message = '<p>You just successfully submitted <strong>this</strong> form!</p>';
  }
  // this is where you display results.  javascript version or full template version
  echo $message;
                if ($is_ajax) {
   if ($ajax_error) {
    echo json_encode($message);
   } else {
    echo json_encode(array('form_message' => $message));
   }
  } else {
                    $this->load->view('form');
   //load view here, or render if using a template library
   $config['content'] = $message;
//   $this->template->add_script('form.js');
//   $this->template->render($config);
  }

} // process()
}

Here is the js:

Code:
// form handler
$("#form").submit(function() {
  // add CSRF token
  $('#submit').before('&lt;input type="hidden" name="ci_csrf_token" value="'+$.cookie("ci_csrf_token")+'" /&gt;');
  $.post("/index.php/welcome/process", $('#form').serialize(), function(data) {
    $("#a_error").html(data.a);
    if (data.form_message) {
     $('#form').html(data.form_message);
    }
    if (data.redirect_to) {
     [removed] = data.redirect_to;
    }
   },'json'
  );
  return false;
});



Why does $this->input->is_ajax_request() = FALSE? - El Forum - 07-26-2012

[eluser]john_j[/eluser]
is_ajax_request just checks whether HTTP_X_REQUESTED_WITH server header has been set. Please see the CI user guide for more info.

tells whether a request is from ajax or not.


Why does $this->input->is_ajax_request() = FALSE? - El Forum - 07-26-2012

[eluser]bill19[/eluser]
Hi John_j,

Thanks for looking at this. I have read http://ellislab.com/codeigniter/user-guide/libraries/input.html . When I run the project ,I see no errors in firebug. When I step through the code with xdebug I find that $this->input->is_ajax_request() outputs FALSE. After some googling I came across http://ellislab.com/forums/viewthread/187859/#888203 which leads to http://ericlbarnes.com/blog/codeigniter-csrf-protection-with-ajax, which is what the file js/plugins.js is about ( according to the author )

My question is what steps do I need to take to figure out why $this->input->is_ajax_request() outputs FALSE.

Thanks,

Bill