javascript - 如何在 codeigniter 中使用 ajax 在 View 中显示消息并替换 div?

标签 javascript php jquery ajax codeigniter

我被ajax困住了...我有一个购物车,里面有礼券, i) 代码检查凭证是否有效,如果无效,则应显示消息“无效凭证”。 ii) 如果优惠券有效并在数据库中设置,则使用礼品金额更改购物车总额。

所以我已经设置了消息,但我不知道如何显示它。并且使用礼物我存储了新的购物车总计,即gift_cart_total。在数据库中,我有 cart_total、优惠券、gift_cart_total 的字段。我如何删除我的原始购物车 div 并使用 Gift_cart_total 显示新 div。

查看:

<div class="giftvoucher">
   <input type="text" name="gift_voucher_number" id="gift_voucher_number" placeholder="Voucher Number" value="" size=18>
   <input type="text" name="gift_voucher_pin" id="gift_voucher_pin" placeholder="Voucher Pin" value=""size=18>
   <input type="button" value="Apply" onclick="checkGiftVoucher()" id="apply_button">
</div>
<script type="text/javascript">
function checkGiftVoucher(){
    var gift_voucher_number = $("#gift_voucher_number").val();
    var gift_voucher_pin = $("#gift_voucher_pin").val();

    $.ajax({
         type: "POST",
         url: "<?php echo base_url('cart/giftvalidate'); ?>",
         data: {gift_voucher_number:gift_voucher_number,gift_voucher_pin:gift_voucher_pin} , 

          success: function(data){
             return false;

         },
        });
  }

   </script> 

Controller :

public function giftvalidate(){
    if(isset($_POST['gift_voucher_number']) && $_POST['gift_voucher_number'] != '' && isset($_POST['gift_voucher_pin']) && $_POST['gift_voucher_pin'] != ''){           
        $app21_url      = APP21_URL;
        $country_code   = COUNTRY_CODE;

        $gift_voucher_number    = $_POST['gift_voucher_number'];
        $gift_voucher_pin   = $_POST['gift_voucher_pin'];

        $result         = $this->cart_model->checkValidGift($gift_voucher_number, $gift_voucher_pin, $app21_url, $country_code);
        if($result['databool'] && !empty($result['dataValue'])){

            $discount   = $result['dataValue']['AvailableAmount'];
        }else{

            $gift_flash = $result['datamsg'];
        }

    }elseif(isset($_POST['gift_voucher_number']) && (empty($_POST['gift_voucher_pin']) || $_POST['gift_voucher_pin'] =='')){            
        $result['datamsg']  = "Please enter the gift voucher pin";  
    }

    echo json_encode($result);
}

型号:

function checkValidGift($gift, $gift_pin, $app21_url, $country_code){

    $returndata = array();
    $dataValue  = array();
    $datamsg    = '';
    $databool   = false;
    $cartTotal  = $this->getTotalCart();
    $GVValid_url = some url here;

    $headers = array(
        "GET HTTP/1.1",
        "Content-type: text/xml",
        "Accept: Version_2.0"
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $GVValid_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 900);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

    $data = curl_exec($ch);

    if (curl_errno($ch)) {

        $result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);


        $databool = false;
        $datamsg  = 'Gift Voucher & pin are not Valid';

    }else if($gift='100038' && $gift_pin='7655'){
        $gift='100038';
        $AvailableAmount = '200';

        if ($AvailableAmount > $cartTotal) {
            $gift_cart_total = $cartTotal;
        }else{
            $gift_cart_total = $cartTotal-$AvailableAmount;
        }


        if(!$this->session->userdata('s_uid') || $this->session->userdata('s_uid')==''){
            $user_type = 'Guest';
            $user_id   = $this->session->userdata('__ci_last_regenerate');
        }else{
            $user_type = 'Member';
            $user_id = $this->session->userdata('s_uid');
        }

        $insertData = array(
                            'user_id'   => $user_id,
                            'gift_id'   => $gift,
                            'pin'       => $gift_pin,
                            'gift_amount'    => $AvailableAmount,
                            'gift_cart_total' =>$gift_cart_total
                        );

        $table      = 'cart_mast';
        $sql        = "SELECT * FROM $table WHERE user_id = '{$user_id}'";

        $query      = $this->db->query($sql);


        if($query->num_rows() != 0){
            $carts = $query->result_array();
            foreach ($carts as $row) {
                $this->db->where('user_id', $user_id);
                $this->db->update($table, $insertData);
                //echo $this->db->last_query(); exit;
            }
        }else{
            $this->db->insert($table, $insertData);
        }

        $dataValue = array(
                            'giftId'          => $gift,
                            'AvailableAmount' => $AvailableAmount,
                            'gift_cart_total' => $gift_cart_total
                       );

        $databool = true;
        $datamsg  = '';
    }
 else{

        $xml = new SimpleXMLElement($data);

        if(count($xml) <= 2 && !empty($xml->ErrorCode)){

            $databool = false;
            $datamsg  = 'The voucher has expired';

        }else{

            $VoucherNumber   = (int)($xml->VoucherNumber);
            $gift_pin        = $pin;
            $ExpiryDate      = (int)($xml->ExpiryDate);
            $AvailableAmount = (int)($xml->AvailableAmount);

            $AvailableAmount = number_format($AvailableAmount, 2);
            $user_id    = $this->session->userdata('session_id');

            $table      = 'gift_voucher';
            $sql        = "SELECT * FROM $table WHERE user_id = '{$user_id}'";
            $query      = $this->db->query($sql);
            $insertData = array(
                                'user_id'   => $user_id,
                                'gift_id'   => $VoucherNumber,
                                'pin'       => $gift_pin,
                                'amount'    => $AvailableAmount
                            );

            if($query->num_rows() != 0){

                $carts = $query->result_array();

                foreach ($carts as $row) {
                    $this->db->delete($table, array('user_id' => $user_id)); 
                    $this->db->insert($table, $insertData);
                }

            }else{
                $this->db->insert($table, $insertData);
            }

            $giftId = $this->db->insert_id();


            $dataValue = array(
                                'giftId'          => $giftId,
                                'VoucherNumber'   => (!empty($ss_id))? $ss_id:$VoucherNumber,
                                'AvailableAmount' => $AvailableAmount
                           );

            $databool = true;
            $datamsg  = '';

        }

    }

    curl_close($ch);

    $returndata['databool']   = $databool;
    $returndata['datamsg']    = $datamsg;
    $returndata['dataValue']  = $dataValue;

    return $returndata;
}

最佳答案

ise .html() 替换 div 的整个 html 或使用 append() 追加到 div

success: function(data){

    $("id or class of the div where you want to display the message").html(data);
    return false; 
},

由于您只有一个回复,请更改此内容

echo json_encode($result);

echo $result['datamsg'];

或者 在这里使用变量和 echo 而不是数组

关于javascript - 如何在 codeigniter 中使用 ajax 在 View 中显示消息并替换 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33886272/

相关文章:

javascript - 当我关闭 Canvas 外边栏时,背景覆盖不会消失

javascript - 如何使用 Javascript 监听值变化的特定键?

javascript - jsp &lt;input&gt; 值未通过类 form-control 显示

javascript - 如何在不同的View中捕获来自不同页面的按钮点击事件并在源页面中进行DOM操作?

php - 在 Mysql PHP 查询中组合 Where 子句

php - 在网页中包含 Javascript

php - 从 MySql 获取 BLOB 图像到 CSS 内联样式

php - 使用php备份数据库mysql

javascript - 加载 dom 后禁用媒体查询

javascript - 从该窗口本身控制浏览器窗口(顶部窗口而不是弹出窗口)的高度和宽度