使用ajax将数据插入数据库,如果使用codeigniter成功则显示模态

时间:2015-12-07 05:44:11

标签: javascript php ajax codeigniter

我在使用ajax将数据插入数据库时​​遇到了一些问题。我的问题是数据成功插入数据库后,模态对话框没有显示。这是我的代码。

Html:

<div class="modal fade" id="buy" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="color:black !important;">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title" id="myModalLabel">Buy Now</h4>
        </div>
        <form action="<?php echo base_url(); ?>cust/req-ord" method="POST" role="form" id="form2">              
            <div class="modal-body form-horizontal">
                <input type="hidden" name="InputType" id="InputType" value="Buy">
                <div class="form-group form-group-lg">
                    <label class="col-md-3 control-label" for="OrderAppsName">Software Type</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-cubes"></i><input type="text" class="input-field" name="OrderAppsName" id="OrderAppsName" placeholder="Application Name" required readonly="">
                    </div>
                </div>
                <div class="form-group form-group-lg">
                    <label class="col-md-3 control-label" for="OrderName">Name</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-user"></i><input type="text" class="input-field" name="OrderName" id="OrderName" placeholder="Your Full Name" required>
                    </div>
                </div>
                <div class="form-group form-group-lg">
                    <label class="col-md-3 control-label" for="OrderAddress">Address</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-map-marker"></i><input type="text" class="input-field" name="OrderAddress" id="OrderAddress" placeholder="Your Address" required>
                    </div>
                </div>
                <div class="form-group form-group-lg">
                    <label class="col-md-3 control-label" for="OrderMail">Email</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-envelope"></i><input type="email" class="input-field" name="OrderMail" id="OrderMail" placeholder="Your Mail" required>
                    </div>
                </div>
                <div class="form-group form-group-lg">
                    <label class="col-md-3 control-label" for="OrderPhone">Phone</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-phone"></i><input type="text" class="input-field" name="OrderPhone" id="OrderPhone" placeholder="Your Phone Number" required>
                    </div>
                </div>
                <div class="form-group form-group-lg">
                    <label class="col-md-3 control-label" for="OrderCompany">Company</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-briefcase"></i><input type="text" class="input-field" name="OrderCompany" id="OrderCompany" placeholder="Your Company Name">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3" style="text-align:right" for="OrderPaymentMode">Payment Mode</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-usd"></i>
                        <select name="OrderPaymentMode" id="OrderPaymentMode" class="input-field" style="width:94%; text-align:center;" onclick="craateUserJsObject.ShowTypeproject();">
                            <option selected disabled="">Chose</option>
                            <option value="1">Subscribe</option>
                            <option value="2">Cut Off</option>
                        </select>
                    </div>
                </div>
                <div class="form-group OrderCountClient" style="display:none">
                    <label class="col-md-3" style="text-align:right" for="OrderClient">Count of client users</label>
                    <div class="col-md-8 label-field">
                        <i class="fa fa-users"></i>
                        <select name="OrderCountClient" id="OrderClient" class="input-field" style="width:94%; text-align:center;">
                            <option selected disabled="">Chose</option>
                            <option value="1-5">1-5</option>
                            <option value="6-10">6-10</option>
                            <option value=">10">>10</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label" for="OrderDesciption">Notes</label>
                    <div class="col-md-8 label-field" style="padding-left:4%; padding-top:2%; padding-bottom:2%">
                        <textarea name="OrderDescription" class="input-field" style="overflow:hidden;" id="OrderDescription" cols="40" rows="2" placeholder="Your Idea Brief"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="form-group">
                    <div class="col-md-12">
                        <label class="col-md-1 control-label" for="OrderCompany">Total: </label>
                        <div class="col-md-4">
                            <input type="text" class="input-field" name="OrderBudget" id="OrderTotal" placeholder="Total of your order" style="text-decoration:underline" required readonly=""> 
                        </div>
                        <button type="button" class="btn btn-default" data-dismiss="modal" style="margin-top:-1%;">Close</button>
                        <button type="submit" class="btn btn-primary btn-c" id="submitbuy" style="margin-top:-1%;">Submit</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

要显示的模式:

<div class="modal fade" id="popconf" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" style="color:black !important;">
    <div class="modal-content">
        <div class="modal-body form-horizontal">
            <div class="form-group form-group-lg">
                <label class="col-md-3 control-label" for="ProductName">Apps Name</label>
                <div class="col-md-8 label-field">
                    <img src="<?php echo base_url() ?>file/dialog/buy.png">
                </div>
            </div>
        </div>
    </div>
</div>

js:

$("#submitbuy").click(function () {
    $.ajax({
        url : "<?php echo base_url();?>cust/req-ord",
        type : 'POST',
        data :{
            "InputType":$("#InputType").val(),
            "OrderAppsName":$("#OrderAppsName").val(),
            "OrderName":$("#OrderName").val(),
            "OrderAddress":$("#OrderAddress").val(),
            "OrderMail":$("#OrderMail").val(),
            "OrderPhone":$("#OrderPhone").val(),
            "OrderCompany":$("#OrderCompany").val(),
            "OrderPaymentMode":$("#OrderPaymentMode").val(),
            "OrderCountClient":$("#OrderClient").val(),
            "OrderDescription":$("#OrderDescription").val(),
            "OrderBudget":$("#OrderTotal").val()
        },
        success : function(data) {
            $('#popconf').modal('show');
        }
    });
});

控制器:

if($_POST){
        date_default_timezone_set('Asia/Jakarta'); 

        $this->load->helper('url');

        $mounts             = Date('m');
        $mount              = $this->KonDecRomawi($mounts);
        $years              = Date('Y');
        $year               = $this->KonDecRomawi($years);
        $value              = $this->m_vcorder->getLastOrder($mount.'/'.$year)->result_array();
        if(empty($value)){
            $OrderNumber    = '001/BTK/RTL/'.$mount.'/'.$year; 
        }else{
            $OrderId    = $value[0]['OrderNumber'];
            $getLimit   = intval(substr($OrderId, 0, 3));
            $Sum        = intval($getLimit)+1;  
            $Sum_Result = sprintf("%03s",$Sum);

            $OrderNumber    = $Sum_Result.'/BTK/RTL/'.$mount.'/'.$year; 
        }

        $OrderAppsName          = $this->input->post('OrderAppsName');
        $OrderName              = $this->input->post('OrderName');
        $OrderAddress           = $this->input->post('OrderAddress');
        $OrderMail              = $this->input->post('OrderMail');
        $OrderPhone             = $this->input->post('OrderPhone');
        $OrderCompany           = $this->input->post('OrderCompany');
        $OrderCountClient       = $this->input->post('OrderCountClient');
        $OrderPaymentMode       = $this->input->post('OrderPaymentMode');
        $OrderBudget            = $this->input->post('OrderBudget');
        $OrderDescription       = $this->input->post('OrderDescription');
        $InputType              = $this->input->post('InputType');

        $OrderConfirmationCode  = $this->generateRandomString();
        $Now                    = date('Y-m-d H:i:s');
        $OrderStatus            = '0';
        $Discount               = $this->m_vcorder->getConditionData("where OrderConditionName='Discount'")->result_array();
        $Tax                    = $this->m_vcorder->getConditionData("where OrderConditionName='Tax'")->result_array();

        $OB                     = preg_replace('/\D/', '', $OrderBudget);
        $OrderDiscount          = $Discount[0]['OrderConditionValue'];
        $OrderTax               = $Tax[0]['OrderConditionValue'];
        $Budget                 = $OB - (($OB*$OrderDiscount)/100) + (($OB*$OrderTax)/100);
        $OrderTotal             = "Rp. ".number_format($Budget, 0, ',', '.').",-";

        $config = Array(
            'protocol'      => 'smtp',
            'smtp_host'     => 'ssl://smtp.gmail.com',
            'smtp_port'     => 465,
            'smtp_timeout'  => '7',
            'smtp_user'     => '', // change it to yours
            'smtp_pass'     => '', // change it to yours
            'mailtype'      => 'html',
            'sendmail_path' => '/usr/sbin/sendmail',
            'charset'       => 'iso-8859-1',
            'wordwrap'      => TRUE
            );

        $mailc = array(
            'OrderNumber'           => $OrderNumber,
            'OrderConfirmationCode' => $OrderConfirmationCode,
            'OrderName'             => $OrderName,
            'OrderAddress'          => $OrderAddress,
            'OrderAppsName'         => $OrderAppsName,
            'OrderCountClient'      => $OrderCountClient,
            'OrderBudget'           => $OrderBudget,
            'OrderDiscount'         => $OrderDiscount,
            'OrderTax'              => $OrderTax,
            'OrderTotal'            => $OrderTotal,
            'OrderPhone'            => $OrderPhone,
            'InputType'             => $InputType,
            );
        $message = $this->load->view('mail', $mailc, TRUE);

        $this->load->library('email', $config);
        $this->email->clear(TRUE);
        $this->email->set_newline("\r\n");
        $this->email->from(mail); // change it to yours
        $this->email->to(''.$OrderMail.'');// change it to yours
        $this->email->subject('Confirmation Order');
        $this->email->message($message);


            $data = array(
                'OrderNumber'               => $OrderNumber,
                'OrderAppsName'             => $OrderAppsName,
                'OrderName'                 => $OrderName,
                'OrderAddress'              => $OrderAddress,
                'OrderMail'                 => $OrderMail,
                'OrderPhone'                => $OrderPhone,
                'OrderCompany'              => $OrderCompany,
                'OrderCountClient'          => $OrderCountClient,
                'OrderBudget'               => $OrderBudget,
                'OrderDescription'          => $OrderDescription,
                'OrderConfirmationCode'     => $OrderConfirmationCode,
                'OrderPaymentMode'          => $OrderPaymentMode,
                'OrderCreateDate'           => $Now,
                'OrderType'                 => '3',
                'OrderStatus'               => $OrderStatus,
                );
        }else{
            $data = '';
        }

        if($this->email->send()){
            $Success = $this->m_vcorder->insertdata('orders',$data);
            if($Success){

            }
        }else{
            /*echo "<script>alert('Terjadi kesalahan. Harap ulangi kembali!'); window.location.href='" . base_url('product/content/2') . "';</script>";*/
        }
    }
    else {
        echo "Page Not Found";
    }

我想在提交后显示#popconf模式。

2 个答案:

答案 0 :(得分:0)

尝试使用css显示弹出窗口。

success : function(data) {
     $('#popconf').css('display','block');
}

答案 1 :(得分:0)

  

在控制器中返回json数组。

XML::Simple
  在ajax请求中

          if($this->email->send()){
            $Success = $this->m_vcorder->insertdata('orders',$data);
            if($Success){
              $result = array('status' =>1 ,'message'=>"data save ");
              echo json_encode($result);
              die();
            }
        }else{
            $result = array('status' =>0 ,'message'=>"data not save ");
            echo json_encode($result);
            die();
        }