Welcome Guest, Not a member yet? Register   Sign In
Why does $this->input->is_ajax_request() = FALSE?

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?



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

Here is the view:

<!DOCTYPE html>
        &lt;base href="&lt;? echo base_url();?&gt;"&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&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 class="control-group">
<div class="controls">
  <button type="submit" class="btn btn-primary" id="submit">Save</button> or <a >Cancel</a>
&lt;?php echo form_close(); ?&gt;

Here is the controller:

function process() {
  $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;

  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 {
   //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:

// 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) {
    if (data.form_message) {
    if (data.redirect_to) {
     [removed] = data.redirect_to;
  return false;

Messages In This Thread
Why does $this->input->is_ajax_request() = FALSE? - by El Forum - 07-26-2012, 07:57 AM
Why does $this->input->is_ajax_request() = FALSE? - by El Forum - 07-26-2012, 09:26 AM
Why does $this->input->is_ajax_request() = FALSE? - by El Forum - 07-26-2012, 10:23 AM

Theme © iAndrew 2016 - Forum software by © MyBB