我正在尝试使用codeigniter添加ajax。但它不起作用。 这是我的代码。如果有人帮助我,我会很高兴
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>
在此代码中,首先我显示一个带有办公室分支名称的下拉框。如果有人选择一个分支,那么它将显示这个早午餐下的部门。当任何人提交表单时,分支ID和部门ID将存储在数据库中。
数据库表:
分支:属性(id,name) department:属性(id,name) branch_dept:attributes(id,dept_id,branch_id)
tournament_record控制器将使用分支表中的锦标赛模型获取所有分支信息,并将其显示在edit_record.php的下拉框中
当选择一个选项时,ajax代码将传递分支的id以匹配控制器类,它将使用数据库中branch_dept表的分支id获取所有部门ID,并在下拉框中显示部门名称。然后将提交表单,并将分支ID和部门ID存储在数据库中。但是,当选择分支时,我的ajax代码无法显示部门列表。请帮帮我。
答案 0 :(得分:0)