Skip to main content

PHP - Coupon Code Generator

Screenshot
Body
In this tutorial we will create a Coupon Code Generator using PHP. This code will generate a random coupon codes when user click the generate button. The code use substr() to cut the give characters one by one and with str_shuffle() it will shuffle randomize the characters to create a unique code. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_coupon, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_coupon");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <?php require'conn.php'?>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  7. </head>
  8. <body>
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Coupon Code Generator</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="submit.php">
  20. <div class="form-group">
  21. <label>Get coupon here</label>
  22. <input type="text" class="form-control" name="coupon" id="coupon" required="required" readonly="readonly"/>
  23. </div>
  24. <center><button type="button" class="btn btn-primary" id="generate">Generate Coupon</button>
  25. <br /><br />
  26. <button class="btn btn-success" name="submit" style="display:none;" id="submit">Submit</button></center>
  27. </form>
  28. </div>
  29. <div class="col-md-2"></div>
  30. <div class="col-md-6">
  31. <form method="POST" action="">
  32. <div class="form-group">
  33. <label>Use coupon code here</label>
  34. <input type="text" class="form-control" name="coupon"required="required"/>
  35. </div>
  36. <center><button class="btn btn-primary" name="use">Use Coupon</button></center>
  37. </form>
  38. <br />
  39. <?php include"use.php"?>
  40. </div>
  41. </div>
  42. <script src="js/jquery-3.2.1.min.js"></script>
  43. <script src="js/bootstrap.js"></script>
  44. <script type="text/javascript">
  45. $(document).ready(function(){
  46. $('#generate').on('click', function(){
  47. $.get("generate.php", function(data){
  48. $('#coupon').val(data);
  49. });
  50. $(this).attr("disabled", "disabled");
  51. $('#submit').show();
  52. });
  53. });
  54. </script>
  55. </body>
  56. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the form inputs to the MySQLi server. To do that just copy and write this block of codes inside the text editor, then save it as shown below. submit.php
  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['submit'])){
  4. $coupon=$_POST['coupon'];
  5.  
  6. mysqli_query($conn, "INSERT INTO `coupon` VALUES('', '$coupon', '')") or die(mysqli_error());
  7.  
  8. header("location: index.php");
  9. }
  10. ?>
use.php
  1. <?php
  2. require_once'conn.php';
  3. if(ISSET($_POST['use'])){
  4. $coupon=$_POST['coupon'];
  5.  
  6. $query=mysqli_query($conn, "SELECT * FROM `coupon` WHERE `coupon_code`='$coupon'") or die(mysqli_error());
  7. $row=mysqli_num_rows($query);
  8. $fetch=mysqli_fetch_array($query);
  9. if($row > 0){
  10. if($fetch['status']==''){
  11. echo "<div class='alert alert-info'>Coupon code activated!</div>";
  12. mysqli_query($conn, "UPDATE `coupon` SET `status`='used' WHERE `coupon_id`='$fetch[coupon_id]'") or die(mysqli_error());
  13. }else{
  14. echo "<div class='alert alert-danger'>Your coupon code has been used!</div>";
  15. }
  16. }else{
  17. echo "<div class='alert alert-danger'>Your coupon code is not available!</div>";
  18. }
  19. }
  20. ?>

Creating the Main Function

This code contains the main function of the application. This code will generate a random coupon code when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as generate.php.
  1. <?php
  2. function coupon($l){
  3. $coupon = substr(str_shuffle(str_repeat('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',$l-2)),0,$l-2);
  4.  
  5. return $coupon;
  6. }
  7.  
  8. echo coupon(10);
  9. ?>
There you have it we successfully created Coupon Code Generator using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment