user200
user200

Reputation: 301

Ajax from submit is not working in codeigntier with out page refresh

guys, I am trying to submit my form using ajax but I don't know exactly what happened it's not posting the values to my table in the database, This is the first time I am using ajax for form submit can anyone help me what mistake I have done.

Here is my view code:

<html>

<head>
    <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
    <script type='text/javascript' src="<?php echo base_url(); ?>assets/theme1/js/jquery-2.1.3.min.js"></script>
    <!-- <script type="text/javascript"> -->
    <script type = "text/javascript">

        // Ajax post
        $(document).ready(function() {
            $('form').submit(function(e) {
                e.preventDefault();
                var organisation_name = $("input#organisation_name").val();
                jQuery.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>" + "Organisation/createOrg",
                    dataType: 'json',
                    data: { organisation_name: organisation_name },
                    success: function(res) {
                        if (res) {
                            // Show Entered Value
                            jQuery("div#result").show();
                            jQuery("div#value").html(res.organisation_name);
                        }
                    }
                });
            });
        });
    </script>
    <div class="modal fade" id="createGroup" tabindex="-1" role="dialog" aria-labelledby="createGroup" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content" id="modal-content">
                <form action="" id="user-groups-create" class="form-horizontal" method="post">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">Create a New Organisation</h4>
                    </div>
                    <div class="modal-body" id="modal-body">
                        <div class="form-group">
                            <label for="group_name" class="col-sm-4 control-label">New Organisation Name : </label>
                            <div class="col-md-8">
                                <input type="text" id="organisation_name" name="organisation_name" class="form-control" placeholder="Organisation Name" />
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" value="submit" class="btn btn-primary submit" id="submit">Create Organisation</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

Here is my controller's method createOrg:

public function createOrg() {
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    //Validating Name Field
    $this->form_validation->set_rules('organisation_name', 'organisation_name', 'required|min_length[5]|max_length[15]');
    if ($this->form_validation->run() == FALSE) {
        $this->session->set_flashdata('error', 'Organisation name need to be more than 3 characters and less than 15.');
        redirect('Organisation', $error);
    } else {
        //Setting values for tabel columns
        $data = array(
            'organisation_name' => $this->input->post('organisation_name')
        );
        //Transfering data to Model
        $this->Org_model->orgInsert($data);
        $this->session->set_flashdata('success', 'Organisation created.');
        //Loading View
        redirect('Organisation');
    }
}

Here is my Model's method orgInsert:

function orgInsert($data) {
    // Inserting in Table(organisation)
    $this->db->insert('organisation', $data);
}

Can anyone help me what mistake I have done and I have checked my code properly I didn't find exactly where I have done a mistake and I want my modal popup should be there after submitting it until a user clicks on the close button. when I try to keep alert after jQuery.ajax({ it is not coming alert.. and I can able to get the value from var organisation_name in alert...

Thanks in advance.

Upvotes: 0

Views: 59

Answers (2)

Pradeep
Pradeep

Reputation: 9707

Hope this will work you :

$('#user-groups-create').on('submit',function(e){
    var organisation_name = $("#organisation_name").val();
    $.ajax({
      type: "POST",
      url: "<?=site_url('Organisation/createOrg');?>",
      dataType: 'json',
      data: {'organisation_name': organisation_name},
      success: function(res) {
        if (res)
        {
          alert(res);
          window.location.href = "<?=site_url('Organisation');?>";
          $("div#result").show();
          $("div#value").html(res.organisation_name);
        }
      },
    });
  e.preventDefault();
});

Your controller's method createOrg should be like this :

public function createOrg() 
{
    $data = array(
        'organisation_name' => $this->input->post('organisation_name')
    );
    //Transfering data to Model
    $this->Org_model->orgInsert($data);
    $this->session->set_flashdata('success', 'Organisation created.');
    echo json_encode($data);
    exit;
    }
}

Upvotes: 1

user200
user200

Reputation: 301

Working by changing the script to like this

   <script type="text/javascript">
// Ajax post
 $(document).ready(function() {
 $('form').submit(function(e){
 e.preventDefault();
 var organisation_name = $("input#organisation_name").val();

$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "Organisation/createOrg",
dataType: "html",
data: {organisation_name: organisation_name},
success: function(data) {
alert('success');
}
});
});
});

</script>

Upvotes: 0

Related Questions