Member Avatar for Member #46692

Sup.

I'm using codeigniter with twitter bootstrap modal window and I would like it to talk to the controller...

How...

Cheers in advance.

Dani AI

Generated

Short answer: treat the modal exactly like any other form. As guessed, the modal contains a form; as noted, examples exist online — a concise, reliable pattern is to submit the modal form with AJAX to a CodeIgniter controller (avoids a full page reload) or use a normal form POST when a redirect is acceptable.

Place the form inside the modal (or output it with form_open so CI injects CSRF when enabled). Bind the submit handler with delegation so it works for dynamic modal content. Example client-side pattern:

$(document).on('submit', '#modalForm', function(e) {
  e.preventDefault();
  var $f = $(this);
  $.ajax({
    url: $f.attr('action'),
    method: 'POST',
    data: $f.serialize(),
    dataType: 'json'
  }).done(function(resp){
    if (resp.status === 'ok') {
      $('#myModal').modal('hide');
      // update page DOM from resp.data if needed
    } else if (resp.errors) {
      $.each(resp.errors, function(name, msg){
        var $field = $f.find('[name="'+name+'"]');
        $field.closest('.form-group').find('.help-block').remove();
        $field.after('<div class="help-block text-danger">'+msg+'</div>');
      });
    }
  }).fail(function(){
    // basic failure handling
    alert('Server error.');
  });
});

Server-side: have the controller validate, return JSON errors or success, and set content-type. Minimal CI-style example:

public function save()
{
  $this->load->library('form_validation');
  $this->form_validation->set_rules('title','Title','required');

  if ($this->form_validation->run() === FALSE) {
    $errors = $this->form_validation->error_array();
    $this->output->set_content_type('application/json')
                 ->set_output(json_encode(['status'=>'error','errors'=>$errors]));
    return;
  }

  $data = $this->input->post(NULL, TRUE); // sanitized
  $this->model->insert($data);
  $this->output->set_content_type('application/json')
               ->set_output(json_encode(['status'=>'ok']));
}

Troubleshooting notes: use form_open() to handle CSRF automatically if CI has it enabled, or include the token hidden field. If modal HTML is loaded by AJAX, ensure event handlers are bound after insertion or use delegated $(document).on(...). Inspect the Network tab for the AJAX request/response (JSON and HTTP status) when debugging. This approach keeps the modal UX smooth and connects cleanly to a CodeIgniter controller without forcing a full-page refresh.

Recommended Answers

All 5 Replies

is it a form?

Member Avatar for Member #46692

Yes the modal window will be a form with a button on it. Thanks in advance.

Please use search engine for this kind of question. Check this simple search.
And these are first 4 results:
a) One.
b) Two.
c) Three.
d) Four.

Member Avatar for Member #46692

Thank you Tpojka... I noticed your username (handle) on the codeigniter forums so I guess you are active there. I will be sure to try out those links and report back here if I have any issues tomorrow.

Member Avatar for Member #120589

I thought he was taking the piss. heh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.