Updating list using AJAX in Codeigniter (within a form)

dericcain

I have a form that adds clients to a database clients/add_client. The add_client method has a parameter of $state which is passed to another method list_centers(). When someone is talking to a potential client, they have all of our centers in a sidebar. There is a <select> above the list of centers in which lists the states where we have centers. When they change the select to another state, it should list all of the centers in that state. Now, I have this working by passing the parameter in the URL like this: localhost/clients/add_clients/GA lists all of the centers in Georgia. The problem is that I want to do this with AJAX and not have the page refresh. I cannot figure out how to pass this data via ajax. I know that I have to reconstruct the list each time but I am stuck. Here is what I have tried:

$('#center_select').change(function(){
        var data = $(this).val();
        var url = 'add_client/' + data;
        $.ajax({
            type: 'POST',
            dataType: 'html',
            data: data,
            url: url,
            success: function(data) {
                console.log(data);
            },
            error: function(data) {
                console.log(data);  
            } 
        });
        return false;
    });

Just in case you need the method:

public function add_client($state = false) {

        $this->load->model('centers_model');

        $data = array(
            'page_title' => 'Add Client',
            'client_status' => $this->clients_model->list_client_status(),
            'centers' => $this->centers_model->list_centers(null, $state),
            'center_states' => $this->centers_model->list_center_states(),
        );

        $this->load->view('header');
        $this->load->view('clients/add_client', $data);
        $this->load->view('footer');

    }

View:

<div class="col-sm-3">
            <aside id="centers_sidebar" class="well">
                <h2>List of Centers</h2>
                <select class="form-control" name="center_select" id="center_select">
                    <option value="all">All</option>
                    <?php
                        foreach ($center_states as $center_state) {
                            echo '<option value="' . $center_state->center_state . '">' . $center_state->name . '</option>';
                        }
                    ?>
                </select>
                <ul id="center_list">
                    <?php
                        foreach ($centers as $center) {
                            $output = '<li class="center">';
                            $output .= '<h5>' . $center->center_name . '</h5>';
                            $output .= '<p>' . $center->center_type . '</p>';
                            $output .= '<p>' . $center->center_city . ', ' . $center->center_state . '</p>';
                            $output .= '<p>' . $center->center_phone . '</p>';
                            $output .= '</li>';
                            $output .= '<hr>';

                            echo $output;
                        }
                    ?>
                </ul>   
            </aside>
        </div>
DFriend

I failed to notice that you request a POST but setup for a GET. So here we supply the proper structure to ajax.data

select handler

  $('#center_select').change(function () {
    var st = $(this).val();
      var url = 'update_centers';
    $.ajax({
      type: 'POST',
      dataType: 'html',
      data: {state: st},
      url: url,
      success: function (data) {
        console.log(data);
        $("#center_list").html(data);
      },
      error: function (jqXHR, textStatus, errorThrown) {
        //useful for trouble shooting & error handling
        console.log(textStatus, errorThrown);
      }
    });
    return false;
  });

AJAX responder method - builds html to send back to ajax.success We need to pull the input from $_POST (using input->post) I've put in a bunch of validity checks and a general purpose ajax error response function too. No extra charge.

function update_centers()
{
  $this->load->model('centers_model');
  $state = $this->input->post('state');
  if(!isset($state))
  {
    $this->ajax_bad_request_error("No state data received");
    return;
  }

  $centers = $this->centers_model->list_centers(null, $state);
  if(!isset($centers))
  {
    $this->ajax_bad_request_error("The database failed to find centers in $state");
    return;
  }

  $output = "";
  foreach($centers as $center)
  {
    $output .= "<li class='center'><h5>$center->center_name</h5>"
      ."<p>$center->center_type</p>"
      ."<p>$center->center_city, $center->center_state</p>"
      ."<p>$center->center_phone</p></li><hr>";
  }
  echo $output;
}

function ajax_bad_request_error($msg)
{
  //All purpose reporting of ajax failure
  header('HTTP/1.1 400 Bad Request');
  header('Content-Type: application/json; charset=UTF-8');
  $data = array('type' => 'error', 'message' => $msg);
  echo json_encode($data);
}

Cannot guarantee this will work perfectly as is - syntax errors may exist. But the concept is sound.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Updating database using AJAX form

From Dev

updating a column in database using jQuery Ajax in codeigniter

From Dev

Codeigniter Form Not Updating And Not Redirecting

From Dev

codeigniter form upload with images using ajax

From Dev

Upload form with image in codeigniter using ajax

From Dev

Updating a list within a tuple

From Dev

Updating a list within a dictionary

From Dev

Ajax form within ajax form

From Dev

How do I call a function within ajax using codeigniter to update

From Dev

updating database on form submit codeigniter

From Dev

Passing form data to controller using AJAX and jquery with Codeigniter

From Dev

Codeigniter updating database record list

From Dev

Returning a form using ajax/jquery that includes html within an input

From Dev

Updating session using AJAX

From Dev

Updating in mysql database using Codeigniter

From Dev

Error : In updating the Data using codeigniter

From Dev

How to validate form data using form_validation in codeigniter submitted using ajax

From Dev

How to validate form data using form_validation in codeigniter submitted using ajax

From Dev

Combine dataframe within the list to form a single dataframe using pandas in python

From Dev

updating value within object using "this"

From Dev

Update a record within a list using ajax, Rails, Coffeescript, jQuery

From Dev

Update a record within a list using ajax, Rails, Coffeescript, jQuery

From Dev

How to target an area within a list using JQuery Ajax?

From Dev

Codeigniter ajax form validation error

From Dev

codeigniter form_validation with ajax

From Dev

Ajax Query using CodeIgniter?

From Dev

How to show errors inline in codeigniter form validation using ajax form submission?

From Dev

using an iframe within a <form>

From Dev

using an iframe within a <form>

Related Related

  1. 1

    Updating database using AJAX form

  2. 2

    updating a column in database using jQuery Ajax in codeigniter

  3. 3

    Codeigniter Form Not Updating And Not Redirecting

  4. 4

    codeigniter form upload with images using ajax

  5. 5

    Upload form with image in codeigniter using ajax

  6. 6

    Updating a list within a tuple

  7. 7

    Updating a list within a dictionary

  8. 8

    Ajax form within ajax form

  9. 9

    How do I call a function within ajax using codeigniter to update

  10. 10

    updating database on form submit codeigniter

  11. 11

    Passing form data to controller using AJAX and jquery with Codeigniter

  12. 12

    Codeigniter updating database record list

  13. 13

    Returning a form using ajax/jquery that includes html within an input

  14. 14

    Updating session using AJAX

  15. 15

    Updating in mysql database using Codeigniter

  16. 16

    Error : In updating the Data using codeigniter

  17. 17

    How to validate form data using form_validation in codeigniter submitted using ajax

  18. 18

    How to validate form data using form_validation in codeigniter submitted using ajax

  19. 19

    Combine dataframe within the list to form a single dataframe using pandas in python

  20. 20

    updating value within object using "this"

  21. 21

    Update a record within a list using ajax, Rails, Coffeescript, jQuery

  22. 22

    Update a record within a list using ajax, Rails, Coffeescript, jQuery

  23. 23

    How to target an area within a list using JQuery Ajax?

  24. 24

    Codeigniter ajax form validation error

  25. 25

    codeigniter form_validation with ajax

  26. 26

    Ajax Query using CodeIgniter?

  27. 27

    How to show errors inline in codeigniter form validation using ajax form submission?

  28. 28

    using an iframe within a <form>

  29. 29

    using an iframe within a <form>

HotTag

Archive