Codeigniter ajax发布数据错误

时间:2012-12-11 17:45:31

标签: php jquery ajax codeigniter

为什么我的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-&gt; input-&gt; post,with trim和$ _POST(),但没有一个解决它。

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

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

这是我在模型中的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);
}

1 个答案:

答案 0 :(得分:0)

有两个可能的问题:

  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/");`
    
       }
    });`