Generate coupon code and call ajax in codeigniter

Its easy to call ajax in codeigniter and coupon code generate in codeigniter, onclick event to call ajax in javascript, create a randon coupon code of with length of code.

You have a setup codeigniter to your server or get codeignter for this, in form create a input box to get a value and create a button to call ajax.





   <div class="col-sm-2">
       <input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="Promocode">
   </div><br>
   <div class="col-sm-2">
      <button type="button" onclick="getpromocode()"  class="btn btn-success">Generate</button>
   </div> 
ajax function to get a coupon code from generated code.


  <script src="<?=base_url()?>/assets/jquery-2.1.4.min.js"></script>
 <script type="text/javascript">
 <!-- call function -->  
function getpromocode() {
         var length = 4;
  $.ajax({    
  url: "http://localhost/admin/index.php/coupon/", 
  method: 'post',
  data: {type:'coupon_action',length: length},
  dataType: 'html',
  success: function(response){
    response=response.replace(/(\r\n|\n|\r)/gm,"");
    $('#p_code').val(response);
       }
         });
}
</script>
 
Set url for calling ajax with example   http://localhost/admin/index.php/coupon/ or
http://localhost/admin/coupon/


   url: "Your app name/index.php/your controller name" 

COMPLETE CODE HERE 

1. Create a file Application/controller/Coupon.php 



<?php defined('BASEPATH') OR exit('No direct script access allowed');

//create Coupon.php file in codeigniter application/controller 
//

class Coupon extends CI_Controller {

        
 public function index(){
     
     
      if($this->input->post()) {
        
       $type=$_POST['type'];
     
         if($type == 'coupon_action'){
       
      $length         = $_POST['length'];
        $useLetters     =  true;
        $useNumbers     =  true;
        $useSymbols     =  false;
        $useMixedCase   =  false;
        $uppercase    = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'];
        $lowercase    = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        $numbers      = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        $symbols      = ['`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '\\', '|', '/', '[', ']', '{', '}', '"', "'", ';', ':', '<', '>', ',', '.', '?'];

        $characters   = [];
        $coupon = '';

        if ($useLetters) {
            if ($useMixedCase) {
                $characters = array_merge($characters, $lowercase, $uppercase);
            } else {
                $characters = array_merge($characters, $uppercase);
            }
        }

        if ($useNumbers) {
            $characters = array_merge($characters, $numbers);
        }

        if ($useSymbols) {
            $characters = array_merge($characters, $symbols);
        }
 
            for ($i = 0; $i < $length; $i++) {
                $coupon .= $characters[mt_rand(0, count($characters) - 1)];
            }
      
       echo $coupon;
     exit(0);
   
  }
   } 
   //create a coupon php file in application/view path 
  $this->load->view('coupon');

  }  
}



2. Create a file  Application/view/coupon.php



<html> 
<head>
<title>Generate coupon</title>
</head>
<!-- include a jquery and add javascript-->
 <script src="<?=base_url()?>/assets/jquery-2.1.4.min.js"></script>
 <script type="text/javascript">
 <!-- call function -->  
function getpromocode() {
        var length = 4;
       $.ajax({    
     url: "http://localhost/admin/index.php/coupon/", 
     method: 'post',
     data: {type:'coupon_action',length: length},
     dataType: 'html',
     success: function(response){
       response=response.replace(/(\r\n|\n|\r)/gm,"");
         $('#p_code').val(response);
          
     }
     });
}
</script>

<body align="center">
<h1>Generate Coupon code with using AJAX,JAvascript in codeigniter</h1>
        <div class="col-sm-2">
           <input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="Promocode">
      </div><br>
             <div class="col-sm-2">
            <button type="button" onclick="getpromocode()"  class="btn btn-success">Generate</button>
              </div> 
</body>
</html>

DOWNLOAD SOURCE CODE




No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...