How to insert data using modal sweet alert ajax with codeigniter

1.2k views Asked by At

i have trouble to insert data using modal sweet alert, this is my script :


    function gettour(){
                    swal.setDefaults({
                      input: 'text',
                      confirmButtonText: 'Next',
                      showCancelButton: true,
                      animation: false,
                      progressSteps: ['1', '2']
                    })

                    var steps = [
                      {
                        input: 'number',
                        title: 'Question 1',
                        text: 'Berapa quantity yang di pesan'                   
                      },
                      {
                        input: 'text',
                        title: 'Question 2',
                        text: 'Tanggal Keberangkatan'
                      },

                    ]

                    swal.queue(steps).then(function (result) {
                      swal.resetDefaults()
                      swal({

                      })
                    }, function () {
                      swal.resetDefaults()

                        var qty = result[0];
                        var tglgo = result[1];
                        var dataString = 'qty='+qty+'tglgo='+tglgo;

                        $.ajax({
                            type:'POST',
                            data:dataString,
                            url:'travel.yes/garden/request',
                            success:function(data) {
                                alert(data);
                            }   
                        });

                    })

                };

And this is my Controller


    public function request()
        {
            $qty = $this->input->post('qty');
            $tglgo = $this->input->post('tglgo');
            $results = $this->model_crud_admin->request_tour($qty,$tglgo);

            if($results){
                redirect('garden/member_area?auth=tour','refresh');
            }
        }

Thank you :)

2

There are 2 answers

0
Rohit Bhalani On

You can do this using stored procedure with codignaitor. Call the SP and just pass sp name and array(model) as parameter.

1
Rohit Bhalani On
public function CallData($procName, $parameters = null, $isExecute =false, $intColumns= null,$ProvideDb=null){
    $syntax = "";
    for ($i = 0; $i < count($parameters); $i++) {
        $syntax .= (!empty($syntax) ? ',' : '') . '?';
    }

    $syntax = 'CALL ' . $procName . '(' . $syntax . ');';
    if($ProvideDb!=''){
        $pdo=DB::connection($ProvideDb)->getPdo();
    }
    else {
        $pdo = DB::connection()->getPdo();
    }

    $pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
    $stmt = $pdo->prepare($syntax,[\PDO::ATTR_CURSOR=>\PDO::CURSOR_SCROLL]);

    for ($i = 0; $i < count($parameters); $i++) {
        $stmt->bindValue((1 + $i), $parameters[$i]);
        //$stmt->bindParam((1 + $i), $parameters[$i], \PDO::PARAM_INT);
    }

    $exec = $stmt->execute();
    if (!$exec) return $pdo->errorInfo();
    if ($isExecute) return $exec;

    $results = [];
     do {
        try {
            $results[] = $stmt->fetchAll(\PDO::FETCH_OBJ);
        } catch (\Exception $ex) {

        }
    } while ($stmt->nextRowset());

    // This code is added to update string values to int values
    if(!empty($intColumns) ) {
        for ($i = 0; $i < count($results); $i++) {
            $results[$i]= Common::setSelectedPropertyValueToIntOfList($results[$i], $intColumns);
        }
    }

    return $results;
}