如何在PHP Codeigniter中使用ID从控制器显示重定向[重复]

20jt8wwn  于 7个月前  发布在  PHP
关注(0)|答案(1)|浏览(53)

此问题在此处已有答案

How to pass and get variables in the url in codeigniter 3(2个答案)
Codeigniter send GET parameter with site_url?(2个答案)
4天前关闭。
我有一个按钮,但它就像生成2功能。我想保存数据,并重定向到ID之前查看。但是,我得到了语法中的错误。Message: Too few arguments to function c_transaksi::tambah_shrinkage(), 0 passed in C:\xampp\htdocs\testresult\system\core\CodeIgniter.php on line 532 and exactly 1 expected
这里,控制器

public function tambah_shrinkage($report_no)
    {
        $this->data['detail'] = $this->m_transaksi->get_by_reportmaterial($report_no);
        $this->data['shrinkage'] = $this->m_transaksi->get_shrinkage();
        $this->template->load('admin/layout/templatetesting','admin/transaksi/shrinkage/tambah', $this->data);
    }

 public function tambahaksi_shrinkage()
    {
        $jumlah_shrinkage = count((array)$this->input->post('nama_shrinkage_hidden'));

        $data = [];
        for ($i = 0; $i < $jumlah_shrinkage; $i++){ 
            array_push($data, ['nama_shrinkage' => $this->input->post('nama_shrinkage_hidden')[$i]]);
            $data[$i]['report_no'] = $this->input->post('report_no');
            $data[$i]['before_wash'] = $this->input->post('before_wash_hidden')[$i];
            $data[$i]['after_wash1'] = $this->input->post('after_wash1_hidden')[$i];
            $data[$i]['actual1'] = $this->input->post('actual1_hidden')[$i];
            $data[$i]['after_wash3'] = $this->input->post('after_wash3_hidden')[$i];
            $data[$i]['actual3'] = $this->input->post('actual3_hidden')[$i];
            $data[$i]['after_wash15'] = $this->input->post('after_wash15_hidden')[$i];
            $data[$i]['actual15'] = $this->input->post('actual15_hidden')[$i];
            $data[$i]['c_shrinkage'] = $this->input->post('c_shrinkage_hidden')[$i];
        }

        $this->m_transaksi->insert_shrinkage($data);
        
        redirect('c_transaksi/tambah_shrinkage');
    }

字符串
这里的视图按钮

<button type="submit" class="btn btn-block bg-gradient-info" style="font-weight: bolder; font-size: 13px; color:aliceblue;"  onclick="history.go(-1);">SUBMIT</button>


我试过使用js history.back和aplog,但仍然得到相同的错误
在这里,

$('#simpanshrinkage').on('click', function(){
              
                var currentRow =  $(this).closest("tr");

                const data_shrinkage = {
                    report_no : currentRow.find("td:eq(0)").text(),
                    nama_shrinkage : currentRow.find("td:eq(1)").text(),
                    before_wash : currentRow.find("td:eq(2)").text(),
                    after_wash1 : currentRow.find("td:eq(3)").text(),
                    actual1 : currentRow.find("td:eq(4)").text(),
                    after_wash3 : currentRow.find("td:eq(5)").text(),
                    actual3 : currentRow.find("td:eq(6)").text(),
                    after_wash15 : currentRow.find("td:eq(7)").text(),
                    actual15 : currentRow.find("td:eq(8)").text(),
                    c_shrinkage :currentRow.find("td:eq(9)").text()
                }

                $.ajax({
                    type: 'POST',
                    url: "<?=site_url('c_transaksi/tambahaksi_shrinkage') ?>",
                    data:data_shrinkage,
                    success: function(data){
                       console.log("berhasil");

                       window.history.back();
                    }
                })
            })

j0pj023g

j0pj023g1#

看起来你可以像这样实现它:
1.你的$. aps POST应该返回正确的json

public function tambahaksi_shrinkage() {

     /* Do whatever you want here */

     echo json_encode(['success' => true]);

     // this is not what you want to do inline:
     // redirect('c_transaksi/tambah_shrinkage');

     exit;
 }

字符串
1.在你的处理程序中检查“成功”,然后执行重定向

$.ajax({
     type: 'POST',
     url: "<?=site_url('c_transaksi/tambahaksi_shrinkage') ?>",
     data:data_shrinkage,
     success: function(data){
        console.log("berhasil");

        if(data.success) {
             window.location.href = "<?=site_url('c_transaksi/tambah_shrinkage') ?>";
             return;
        }

        // do something here if !data.success
     },
     dataType: 'json' // its a good habit to tell jquery what kind of response you expect
 });


1.从tambah_shrinkage方法中删除$report_no参数,并尝试通过GET-Param将其发送到端点。我怀疑$report_no类似于存储引擎中的last_insert_id。因此,在JSON中返回它,并将其作为GET发送回端点-或者你也可以将它存储在$_SESSION中,然后在你的端点上从$_SESSION中读取它,但是这取决于你的使用情况。

相关问题