0

I am trying to add ajax with codeigniter. But it's not working. Here is my code. If anyone help me I will be glad

tournament_record.php

<?php

class Tournament_record extends CI_Controller {
function edit_record2()
{
//Get all the tournaments in the system
$this->load->model('tournament_model');
$data["tournaments"] = $this->tournament_model->get_tournaments();
//Loading the view for the record edition
$this->load->view('edit_record',$data);
}

 function branch_insert()
 {
    $this->load->model('tournament_model');
    $this->tournament_model->insert();
    $this->load->view('succesfull');
 }

}
?>

match.php

<?php
class Match extends CI_Controller {

 public function index()
 {

 }

 public function list_dropdown()
 {
 $this->load->model('tournament_model');
 $tnmnt = $this->input->post('tnmnt'); //Read the tournament id sent by POST
 $tnmnt_id = intval($tnmnt);
 $matches = $this->tournament_model->get_match_by_tournament($tnmnt_id); //Get the matches list for that tournament from the DB
/*$teams = array(); //We store the names of the teams in the form "team_id" => "team_name"
     if(!empty($matches))
     {
         foreach($matches as $match)
         {
             if(!array_key_exists($match["teama"], $teams))
             {
                 $team_name_A = $this->team_model->get_team_name($match["teama"]);
                 $teams[$match["teama"]] = $team_name_A;
             }
             if(!array_key_exists($match["teamb"], $teams))
             {
                 $team_name_B = $this->team_model->get_team_name($match["teamb"]);
                 $teams[$match["teamb"]] = $team_name_B;
             }
         }
 }*/
 $data['matches'] = $matches;
/* $options = array(
              'small'  => 'Small Shirt',
              'med'    => 'Medium Shirt',
              'large'   => 'Large Shirt',
              'xlarge' => 'Extra Large Shirt',
            );

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');
*/
 if(empty($matches))
{
    echo "<option value=''>No available matches</option>";
}
else
{
    foreach($matches as $match)
    {
        $this->db->where->('id', $match->dept_id);
    $query1 = $this->db->get('department');
    $row = $query1->row();
        echo "<option value='".$row->id."'>".$row->name."</option>";
    }
}
 }

 }

?>

tournament_model.php

<?php
class Tournament_model extends CI_Model {

    function get_tournaments() {
        $query = $this->db->get('branch');

        if ($query->num_rows > 0) {
            return $query->result();
        }
    } 

        function get_match_by_tournament($tnmnt_id) {
            $this->db->where('branch_id', $tnmnt_id);
            $query1 = $this->db->get('branch_dept');

            if($query1->num_rows > 0) {
                return $query->result();
            }
        }

        function insert()
        {
            $new_branch = array(
            'dept_id'=>$this->input->post('department'),
            'branch_id'=>$this->input->post('branch')
            );

            $insert = $this->db->insert('branch_dept', $new_branch);
        }
    }

    ?>

edit_record.php

<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="shortcut icon" href="<?php echo base_url(); ?>bootstrap/assets/ico/favicon.png">
        <title>Online Office Management</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function() {
                //When the DOM is ready, we disable the matches list
                $('select#match_list').attr('disabled', true);
            });

            function activate_match() {
                var tnmt_id = $('select#tournament_list').val(); //Get the id of the tournament     selected in the list
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>match/list_dropdown", //We are going to make the     request to the method "list_dropdown" in the match controller
                    data: "tnmnt=" + tnmt_id, //POST parameter to be sent with the tournament id
                    success: function(resp) { //When the request is successfully completed, this function will be executed
                        //Activate and fill in the matches list
                        $('select#match_list').attr('disabled', false).html(resp); //With the ".html()" method we include the html code returned by AJAX into the matches list
                    }
                });
            }
        </script>
    </head>

    <body>
        <?php echo form_open( 'tournament_record/branch_insert'); ?> <strong>Branch:</strong>

        <select id="tournament_list" onchange="activate_match()" name="branch">
            <?php foreach($tournaments as $tournament) { ?>
            <option value="<?php echo $tournament->id; ?>">
                <?php echo $tournament->name; ?></option>
            <?php } ?>
        </select>
        <br /> <strong>Department:</strong>

        <select id="match_list" onchange="show_clips()" name="department"></select>
        <?php echo form_submit( 'mysubmit', 'Submit Post!'); echo form_close(); ?>
    </body>

</html>

succesfull.php

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="shortcut icon" href="<?php echo base_url(); ?>bootstrap/assets/ico/favicon.png">
        <title>Online Office Management</title>
        <!-- Bootstrap core CSS -->
        <link href="<?php echo base_url(); ?>bootstrap/dist/css/bootstrap.css" rel="stylesheet">
        <!-- Custom styles for this template -->
        <link href="css/signin.css" rel="stylesheet">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="../../assets/js/html5shiv.js"></script>
            <script src="../../assets/js/respond.min.js"></script>
        <![endif]-->
    </head>

    <body>suucessful</body>

</html>

In this code , at first i am showing a dropdown box with branch name for a office. If anyone select a branch then it will show the departments under this brunch. When anyone submit the form branch id and department id will be stored in the database.

Database table:

branch: attributes(id, name) department: attributes(id, name) branch_dept: attributes(id, dept_id, branch_id)

tournament_record controller would get all the branch info using tournament model from branch table and show it to the dropdown box in edit_record.php

when a option is selected ajax code will pass the id of branch to match controller class and it will get all the department id using branch id from branch_dept table in database and will show the department name in dropdown box. Then a form will be submitted and branch id and department id will be stored in database. But my ajax code cannot show the department list when a branch is selected. Please help me.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • There is so much code here. You really just have to show us the ajax request you are making, and the associated error you got. It could be as simple as not attaching the CSRF token from CI into the AJAX request. – Connor Tumbleson Feb 27 '14 at 18:33

1 Answers1

0

You're doing data: incorrectly.

It should be data: {"tnmnt":tnmt_id}

Also these two will surely be useful:

How to properly use AJAX?

How to debug AJAX?

Community
  • 1
  • 1
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • I changed data portion as you said. But it's not working – user3256010 Feb 28 '14 at 05:44
  • You have posted WAY too much code. Can you please follow the steps from **[How to debug AJAX?](http://stackoverflow.com/a/21617685/2191572)** and identify which portion of your code is failing? Is the AJAX call never being initiated? Is the ID not being passed? Is your query failing? Do your controllers and models get the expected parameters? – MonkeyZeus Feb 28 '14 at 14:04
  • Thank you. I used firebug to find my error. And i found it. It's a silly mistake in coding. Now it's working. I made mistakes in writting query. Thank you for your suggestions. Thank you. I am trying to solve it for two weeks. – user3256010 Feb 28 '14 at 16:57