-2

This is my script when the multiple insert form is open:

<script src="<?= base_url('assets/'); ?>vendor/jquery/jquery.min.js"></script>
<script type="text/javascript">
   $(document).ready(function(){
      var i =1;
      $('#add').click(function(){
        i++;

        $('#dynamic_field').append('<tr id="row'+i+'"> <td><select name="bhn_id[]" id="bhn_id" placeholder="enter name" class="form-control form-control-sm bhn_id"><option value="">-- Pilih Bahan --</option><?php foreach ($data_bahan->result_array() as $key => $tb_bahan) : ?>
                            <option <?= set_select('bhn_id', $tb_bahan['id_bhn']) ?> value="<?=$tb_bahan['id_bhn'] ?>"><?= $tb_bahan['nm_bhn'] ?></option><?php endforeach; ?>
                              <?= form_error('bhn_id', '<small class="text-danger">', '</small>'); ?>
                          </select><?= form_error('bhn_id', '<small class="text-danger">', '</small>'); ?></td><td width="100"><input type="number" class="form-control form-control-sm jlh_keluar"  name="jlh_keluar[]" id="jlh_keluar" value="<?= set_value('jlh_keluar'); ?>"></td><td align="center" width="100"><button type="button" name="remove" id="'+i+'" class="btn btn-outline-danger btn-sm btn_remove">x</button></td></tr>');
        });
    $(document).on('click', '.btn_remove', function(){
      var button_id = $(this).attr("id");
      $("#row"+button_id+"").remove();
      });

    $('#submit').click(function(){
      $.ajax({
        url:"<?= base_url('m_form_bahan_keluar/tambah_bahan');?>",
        method:"POST",
        data:$('#add_jenis').serialize()
        // success:function(data)
        // {
        //   $('#add_jenis')[0].reset("");

        // }
      });
    });

   });
</script>

And this is the Controller which run when I click submit button.

public function tambah_bahan()
    {
    
        $mysqli = new mysqli("localhost", "root", "", "db_labgizi");
        $data['title'] = 'Tambah Pengeluaran Bahan';
        $data['data_bahan'] = $this->TransaksiBahan_model->get_bahan();
        $data['data_user'] = $this->Db_model->getid();
        $data['id_formbhnkeluar'] = $this->TransaksiBahan_model->Id_Bahan_keluar();
        
        $a = count($_POST["bhn_id"]);  
        $b = count($_POST["jlh_keluar"]);
        if($a > 0)  {  
           for($i=0; $i<$a; $i++) {  
             if(trim($_POST["bhn_id"][$i] != '') && trim($_POST["jlh_keluar"][$i] > '0')) {  
                $bhn_id = $mysqli -> real_escape_string($_POST['bhn_id'][$i]);
                $jlh_keluar = $mysqli -> real_escape_string($_POST['jlh_keluar'][$i]);
                $id_formbhnkeluar = $mysqli -> real_escape_string($_POST['id_formbhnkeluar']);
                $penerima = $mysqli -> real_escape_string($_POST['penerima']);
                $laboran = $mysqli -> real_escape_string($_POST['laboran']);
                $tgl_bhnkeluar = $mysqli -> real_escape_string($_POST['tgl_bhnkeluar']);
                $ket_formbhnkeluar = $mysqli -> real_escape_string($_POST['ket_formbhnkeluar']);
                $sql="INSERT INTO tb_formbahankeluar(id_formbhnkeluar,tgl_bhnkeluar,laboran,penerima,ket_formbhnkeluar)
                VALUES ('$id_formbhnkeluar', '$tgl_bhnkeluar',
                '$laboran','$penerima','$ket_formbhnkeluar')";
                $sql2="INSERT INTO tb_bahan_keluar(formbhnkeluar_id,bhn_id,jlh_keluar) VALUES
                ('$id_formbhnkeluar', '$bhn_id', '$jlh_keluar')";
                
                 $mysqli -> query($sql);
                 $mysqli -> query($sql2);                      
          }  
         } 
        redirect('M_bahan_keluar/index');
        pesan('save');
                
        }  
                
        else {   
         pesan('disimpan', false);
         }   
        }

The problem is I can't direct to the Main Table (M_bahan_keluar/index) after successful inserting data, it just still open the form. How should it be?

Dharman
  • 30,962
  • 25
  • 85
  • 135
lerionaa
  • 3
  • 2
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Aug 03 '20 at 11:43

2 Answers2

0

It seems like you are calling the redirect from within the ajax endpoint. The ajax call is a separate http request, and things happening on it, such as it being redirected, do not impact the initial connection from your browser to the server.

Instead of redirecting in php within the ajax endpoint, you will need to incorporate the redirect into your javascript / jquery call. You can use the 'done' to accomplish this.

$.ajax({
        url:"<?= base_url('m_form_bahan_keluar/tambah_bahan');?>",
        method:"POST",
        data:$('#add_jenis').serialize()
      }).done(function( msg ) {
    redirect('M_bahan_keluar/index')
  });

the done callback is discussed on https://api.jquery.com/Jquery.ajax/

James Clark
  • 1,285
  • 2
  • 9
  • 14
0

you can set after codes finally redirect your page to your custom address with this:

window.location = "your custom address";