How to Generate a coupon/randon string code in php using Ajax, using a coupon code you can provide to your customer discount,offers for your business, let's we go to ahead in details to to create a coupon code in php with ajax.
Explain the step by step in the video you can see how it work and generate coupon in integer, string or symbols also and Download Source code From here
Step 1 : Create index. php file
<html>
<head>
<title>Coupon code Generator using ajax and php</title>
<script src="/jquery-2.1.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="Promocode"/>
<button type="button" onclick="getpromocode()" class="btn btn-success">Generate</button>
</body>
</html>
<head>
<title>Coupon code Generator using ajax and php</title>
<script src="/jquery-2.1.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="Promocode"/>
<button type="button" onclick="getpromocode()" class="btn btn-success">Generate</button>
</body>
</html>
Add this script to index.php call ajax to get a coupon code
function getpromocode() {
var length = 8;
$.ajax({
url: "coupon.php",
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>
create coupon.php and add the code to coupon.php
if($_POST['type'] == 'coupon_action'){
$length = $_POST['length'];
$useLetters = true;
$useNumbers = true;
$useSymbols = 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)];
}
//print coupon and exit go to back to the index file
echo $coupon;
exit(0);
}
?>
How to get success message in ajax jquery?
ReplyDeleteAJAX JSON Example
ReplyDeleteHow AJAX works?
ReplyDelete