-1

为什么我的 ajax 不执行?我检查了控制器和模型,它们都很好。问题是主要的ajax是:

function updateTable()  {
var count = document.getElementsByName('chk[]').length;
var academic = document.getElementById('cb_AcademicYear').value;
var acyear = academic.substr(0,4);
var sem = "SE00"+academic.substr(5);
var yearlevel = document.getElementById('cb_Yearlevel').value;
var acstart = "";
var acend = "";
var atstart = "";
var atend = "";
var status = "";
var successCount =0;
var ele = "";
var data = "";
for (i=0;i<count;i++)
{
    if(document.getElementById('chk_'+i).checked)
    {
        ele = i+"[]";
        acstart = document.getElementById('acstart_'+i).value;

        acend = document.getElementById('acend_'+i).value;

        atstart = document.getElementById('atstart_'+i).value;

        atend = document.getElementById('atend_'+i).value;

        status = document.getElementById('cmb_'+i).value;

        var postData = {
            'acyear' : acyear,
            'sem' : sem,
            'yearlevel' : yearlevel,
            'acstart' : acstart,
            'acend' : acend,
            'atstart' : atstart,
            'atend' : atend,
            'status' : status
        };

        $.ajax({
        url: '<?php echo base_url()?>academicCalendar_entry/update', 
        data: postData ,
        type: 'POST',
        success: function (data){
            successCount++;
        }
        });
    }
}
alert("Successfully inserted ("+successCount+") data(s) including: "+count+ele+acstart+acend+atstart+atend+status+"\n"+academic+acyear+sem+yearlevel+"\n\nand this is the link for the jquery : <?php echo base_url()?>academicCalendar_entry/update/");

}

我把 postdata 作为一个数组,这应该没问题。但我没有把它作为 json 数据类型。我是否必须将其作为 json 数据类型?在错误控制台中,我没有看到任何错误消息,但它仍然没有执行到数据库。

任何人都可以解决这个问题吗?

2012 年 12 月 12 日编辑:这是我的控制器中的 update() 函数:

function update()
    {
        $academic_year = $_POST['acyear'];  //trim($this->input->post('acyear'));
        $semester = $_POST['sem'];          //trim($this->input->post('sem'));
        $yearlevel = $_POST['yearlevel'];       //trim($this->input->post('yearlevel'));
        $acstart = $_POST['acstart'];           //trim($this->input->post('acstart'));
        $acend = $_POST['acend'];               //trim($this->input->post('acend'));
        $atstart = $_POST['atstart'];           //trim($this->input->post('atstart'));
        $atend = $_POST['atend'];               //trim($this->input->post('atend'));
        $status = $_POST['status'];         //trim($this->input->post('status'));
        $auditname = "Admin";
        $auditactivity = "U";
        $audittime = date("Y-m-d");
        $this->load->model('calendar_Model','cm');
        $this->cm->updateAcademicEntry($academic_year,$semester,$yearlevel,$acstart,$acend,$atstart,$atend,$status,$auditname, $auditactivity, $audittime);
        return true;
    }

我尝试过使用 $this->input->post、trim 和 $_POST(),但它们都没有解决它。

这就是我放置 updateTable() 函数的地方:

<input type="submit" name="btn_save" id="btn_save" value="Save" onclick="updateTable()"/>

这是我在 Model 中的 updateAcademicEntry() 函数:

function updateAcademicEntry($academic_year,$semester,$yearlevel,$acstart,$acend,$atstart,$atend,$status,$auditname, $auditactivity, $audittime)
{
    $data = array(
                'Tahun_Akademik'=>$academic_year,
                'Tanggal_Mulai_Akademik'=>$acstart,
                'Tanggal_Akhir_Akademik'=>$acend,
                'Tanggal_Mulai_Kehadiran'=>$atstart,
                'Tanggal_Akhir_Kehadiran'=>$atend,
                'Status'=>$status,
                'NamaAudit'=>$auditname,
                'AktivitasAudit'=>$auditactivity,
                'TanggalAudit'=>$audittime,
                'ID_Semester'=>$semester,
                'ID_Level_Year'=>$yearLevel
                );
    $this->db
            ->where('Tahun_Akademik',$academic_year)
            ->where('ID_Level_Year',$yearLevel)
            ->where('ID_Semester',$semester)
            ->update('trkalenderakademik',$data);
}
4

1 回答 1

0

有2个可能的问题:

  1. 您的控制器中有错误。在这种情况下,您必须更改它。

  2. 您的意思是在回调中添加警报。当前,如果该功能有效或无效,警报就会运行。此外,由于 ajax 与警报异步运行,因此警报不会有更新值successcount

    $.ajax({
      url: '<?php echo base_url()?>academicCalendar_entry/update', 
      data: postData ,
      type: 'POST',
      success: function (data){
         successCount++;
         alert("Successfully inserted ("+successCount+") data(s) including: "+count+ele+acstart+acend+atstart+atend+status+"\n"+academic+acyear+sem+yearlevel+"\n\nand this is the link for the jquery : <?php echo base_url()?>academicCalendar_entry/update/");`
    
       }
    });`
    
于 2012-12-11T18:11:11.830 回答