PHP - Convert Decimal To Hexadecimal

In this tutorial we will create Convert Decimal To Hexadecimal using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So let's now do the coding...

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/. Lastly, this is the link for the jquery that i used in this tutorial https://jquery.com/.

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 you text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Convert Decimal To Hexadecimal</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <h3>Decimal to Hexadecimal</h3>
  20. <br />
  21. <div class="form-group">
  22. <label>Enter number</label>
  23. <input type="number" id="decimal" class="form-control" />
  24. </div>
  25. <button class="btn btn-primary" type="button" id="convert"><span class="glyphicon glyphicon-random"></span> Convert</button>
  26. <div id="result1"></div>
  27. </div>
  28. </div>
  29. <script src="js/jquery-3.2.1.min.js"></script>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </html>

Creating the Script

This code contains the script of the application. This code use jQuery ajax to make a request to server without refreshing the form values To do that just copy and write this block of codes inside the text editor, then save it inside the js directory as script.js.
  1. $(document).ready(function(){
  2. $('#convert').on('click', function(){
  3. if($('#decimal').val() != ""){
  4. var decimal = $('#decimal').val();
  5. $.ajax({
  6. url: 'converter.php',
  7. type: 'POST',
  8. data: {decimal: decimal},
  9. success: function(data){
  10. $("#result1").html("<center><h3 class='text-success'>"+data+"</h3></center>");
  11. }
  12. });
  13. }else{
  14. alert("Please enter something!");
  15. }
  16. });
  17. });

Creating Main Function

This code contains the main function of the application. This code will convert the decimal value into a hexadecimal when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as converter.php.
  1. <?php
  2. if(ISSET($_POST['decimal'])){
  3. $decimal = $_POST['decimal'];
  4.  
  5. $converted = dechex($decimal);
  6.  
  7. echo $converted;
  8. }
  9. ?>
There you have it we successfully created Convert Decimal To Hexadecimal 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