单击按钮时函数不会调用ajax请求

时间:2019-11-13 16:19:28

标签: javascript jquery ajax codeigniter

我在下面显示一个按钮:

<a class="action submit btn btn-danger btn-lg pull-right" onclick="return simpan_akhir();"><i class="glyphicon glyphicon-stop"></i> Selesai Ujian</a>

这是函数:

simpan_akhir = function() {
            if (confirm('Anda yakin akan mengakhiri tes ini..?')) {
                var f_asal = $("#_form");
                var form = getFormData(f_asal);

                $.ajax({
                    type: "POST",
                    url: base_url + "adm/ikut_ujian/simpan_akhir/" + id_tes,
                    data: JSON.stringify(form),
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8'
                }).done(function(r) {
                    if (r.status == "ok") {
                        window.location.assign("<?php echo base_url(); ?>adm/sudah_selesai_ujian/" + id_tes);
                    }
                });

                return false;
            }

为什么单击按钮时没有调用ajax? 这是adm / ikut_ujian / simpan_akhir内部的内容:

else if ($uri3 == "simpan_akhir") {
            $p          = json_decode(file_get_contents('php://input'));

            $jumlah_soal = $p->jml_soal;
            $jumlah_benar = 0;
            $nilai_twk = 0;
            $nilai_tiu = 0;
            $nilai_tkp = 0;
            //$jumlah_bobot = 0;
            $update_ = "";
            //nilai bobot 
            $array_bobot    = array();
            $array_nilai    = array();
            for ($i = 1; $i < $p->jml_soal; $i++) {
                $_tjawab    = "opsi_" . $i;
                $_tidsoal   = "id_soal_" . $i;
                $jawaban_   = empty($p->$_tjawab) ? "" : $p->$_tjawab;
                $cek_jwb    = $this->db->query("SELECT id_mapel,bobot,bobot_a,bobot_b,bobot_c,bobot_d,bobot_e, jawaban FROM m_soal WHERE id = '" . $p->$_tidsoal . "'")->row();
                //untuknilai bobot
                $bobotnya   = $cek_jwb->bobot;
                $array_bobot[$bobotnya] = empty($array_bobot[$bobotnya]) ? 1 : $array_bobot[$bobotnya] + 1;

                $q_update_jwb = "";
                if ("A" == $jawaban_) {
                    //jika jawaban benar
                    $jumlah_benar = $jumlah_benar + $cek_jwb->bobot_a;
                    $array_nilai[$bobotnya] = empty($array_nilai[$bobotnya]) ? 1 : $array_nilai[$bobotnya] + 1;
                    $q_update_jwb = "UPDATE m_soal SET jml_benar = jml_benar + 1 WHERE id = '" . $p->$_tidsoal . "'";
                    if ($cek_jwb->id_mapel == 1) {
                        $nilai_twk = $nilai_twk + $cek_jwb->bobot_a;
                    }
                    if ($cek_jwb->id_mapel == 2) {
                        $nilai_tiu = $nilai_tiu + $cek_jwb->bobot_a;
                    }
                    if ($cek_jwb->id_mapel == 3) {
                        $nilai_tkp = $nilai_tkp + $cek_jwb->bobot_a;
                    }
                } else if ("B" == $jawaban_) {
                    //jika jawaban benar
                    // $jumlah_benar++;
                    $jumlah_benar = $jumlah_benar + $cek_jwb->bobot_b;
                    $array_nilai[$bobotnya] = empty($array_nilai[$bobotnya]) ? 1 : $array_nilai[$bobotnya] + 1;
                    $q_update_jwb = "UPDATE m_soal SET jml_benar = jml_benar + 1 WHERE id = '" . $p->$_tidsoal . "'";
                    if ($cek_jwb->id_mapel == 1) {
                        $nilai_twk = $nilai_twk + $cek_jwb->bobot_b;
                    }
                    if ($cek_jwb->id_mapel == 2) {
                        $nilai_tiu = $nilai_tiu + $cek_jwb->bobot_b;
                    }
                    if ($cek_jwb->id_mapel == 3) {
                        $nilai_tkp = $nilai_tkp + $cek_jwb->bobot_b;
                    }
                } else if ("C" == $jawaban_) {
                    //jika jawaban benar
                    // $jumlah_benar++;
                    $jumlah_benar = $jumlah_benar + $cek_jwb->bobot_c;
                    $array_nilai[$bobotnya] = empty($array_nilai[$bobotnya]) ? 1 : $array_nilai[$bobotnya] + 1;
                    $q_update_jwb = "UPDATE m_soal SET jml_benar = jml_benar + 1 WHERE id = '" . $p->$_tidsoal . "'";
                    if ($cek_jwb->id_mapel == 1) {
                        $nilai_twk = $nilai_twk + $cek_jwb->bobot_c;
                    }
                    if ($cek_jwb->id_mapel == 2) {
                        $nilai_tiu = $nilai_tiu + $cek_jwb->bobot_c;
                    }
                    if ($cek_jwb->id_mapel == 3) {
                        $nilai_tkp = $nilai_tkp + $cek_jwb->bobot_c;
                    }
                } else if ("D" == $jawaban_) {
                    //jika jawaban benar
                    // $jumlah_benar++;
                    $jumlah_benar = $jumlah_benar + $cek_jwb->bobot_d;
                    $array_nilai[$bobotnya] = empty($array_nilai[$bobotnya]) ? 1 : $array_nilai[$bobotnya] + 1;
                    $q_update_jwb = "UPDATE m_soal SET jml_benar = jml_benar + 1 WHERE id = '" . $p->$_tidsoal . "'";
                    if ($cek_jwb->id_mapel == 1) {
                        $nilai_twk = $nilai_twk + $cek_jwb->bobot_d;
                    }
                    if ($cek_jwb->id_mapel == 2) {
                        $nilai_tiu = $nilai_tiu + $cek_jwb->bobot_d;
                    }
                    if ($cek_jwb->id_mapel == 3) {
                        $nilai_tkp = $nilai_tkp + $cek_jwb->bobot_d;
                    }
                } else if ("E" == $jawaban_) {
                    //jika jawaban benar
                    // $jumlah_benar++;
                    $jumlah_benar = $jumlah_benar + $cek_jwb->bobot_e;
                    $array_nilai[$bobotnya] = empty($array_nilai[$bobotnya]) ? 1 : $array_nilai[$bobotnya] + 1;
                    $q_update_jwb = "UPDATE m_soal SET jml_benar = jml_benar + 1 WHERE id = '" . $p->$_tidsoal . "'";
                    if ($cek_jwb->id_mapel == 1) {
                        $nilai_twk = $nilai_twk + $cek_jwb->bobot_e;
                    }
                    if ($cek_jwb->id_mapel == 2) {
                        $nilai_twk = $nilai_tiu + $cek_jwb->bobot_e;
                    }
                    if ($cek_jwb->id_mapel == 3) {
                        $nilai_twk = $nilai_tkp + $cek_jwb->bobot_e;
                    }
                } else {
                    //jika jawaban salah
                    $array_nilai[$bobotnya] = empty($array_nilai[$bobotnya]) ? 0 : $array_nilai[$bobotnya] + 0;
                    $q_update_jwb = "UPDATE m_soal SET jml_salah = jml_salah + 1 WHERE id = '" . $p->$_tidsoal . "'";
                }

                $this->db->query($q_update_jwb);

                $update_    .= "" . $p->$_tidsoal . ":" . $jawaban_ . ",";
            }
            //perhitungan nilai bobot
            ksort($array_bobot);
            ksort($array_nilai);
            $nilai_bobot_benar = 0;
            $nilai_bobot_total = 0;
            foreach ($array_bobot as $key => $value) {
                $nilai_bobot_benar = $nilai_bobot_benar + ($key * $array_nilai[$key]);
                $nilai_bobot_total = $nilai_bobot_total + ($key * $array_bobot[$key]);
            }
            $update_        = substr($update_, 0, -1);
            // $nilai = ($jumlah_benar / ($jumlah_soal - 1)) * 100;
            $nilai = $jumlah_benar;
            $nilai_bobot = ($nilai_bobot_benar / $nilai_bobot_total) * 100;

            /*
            echo var_dump($array_bobot);
            echo var_dump($array_nilai);
            echo "Benar bobot : ".$nilai_bobot_benar."<br>";
            echo "Jml bobot : ".$nilai_bobot_total."<br>";
            echo "Nilai bobot : ".$nilai_bobot."<br>";
            //akhir perhitungan nilai bobot
            exit;
            */
            $this->db->query("UPDATE tr_ikut_ujian SET jml_benar = " . $jumlah_benar . ", nilai_bobot = " . $nilai_bobot . ", nilai = '" . $nilai . "', list_jawaban = '" . $update_ . "', status = 'N',nilai_twk='" . $nilai_twk . "',nilai_tiu='" . $nilai_tiu . "',nilai_tkp='" . $nilai_tkp . "' WHERE id_tes = '$uri4' AND id_user = '" . $a['sess_konid'] . "'");
            $a['status'] = "ok";
            j($a);
            exit;

起初我以为ajax没有成功调用,因为查询不起作用,这的确是查询不起作用。然后,我尝试删除一些代码行,只返回$ a ['status'] =“ ok”,但这不起作用。

2 个答案:

答案 0 :(得分:2)

这对我有用

点击链接以下易于理解的makitweb.com

答案 1 :(得分:0)

// u忘记了此“}”

simpan_akhir = function(){if(confirm('Anda yakin akan mengakhiri tes ini ..?')){                     var f_asal = $(“ #_ form”);                     var form = getFormData(f_asal);

                $.ajax({
                    type: "POST",
                    url: base_url + "adm/ikut_ujian/simpan_akhir/" + id_tes,
                    data: JSON.stringify(form),
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8'
                }).done(function(r) {
                    if (r.status == "ok") {
                        window.location.assign("<?php echo base_url(); ?>adm/sudah_selesai_ujian/" + id_tes);
                    }
                });

                return false;
            }
        }//u forget this