Create Simple Code Generator

Let's create simple code generator for computer. This could be use for documenting computer owner in office.

Files Needed

  • codegenerator.html
  • generator.php
Let's create codegenerator.html and paste this code:

<html>
<head>
    <title>Simple Code Generator</title>
    <style type="text/css">
    .container {
        width: 970px;
        padding: 0 15px;
        margin-left: auto;
        margin-right: auto;
    }
    
    #generatorInput {
        border: 1px solid #888;
        height: 42px;
        padding: 8px 10px;
        width: 50%;
    }
    
    #result {
        border-top: 1px solid #ddd;
        margin-top: 30px;
        padding: 15px;
        width: 100%;
    }
    </style>
</head>
<body>
    <div class="container">
        <h1>Code Generator</h1>
        <input type="text" id="generatorInput">
        <div id="result"></div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function() {
        $('#generatorInput').keyup(function() {
            var $val = $(this).val();
            var $data = {
                'generatorInput': $(this).val()
            };
            if ($val == "" || $val == " ") {
                // alert('Input cannot be empty!')
                $('#result').html('<p style="color: red; border: 1px solid red; padding: 15px;">Input cannot be empty!</p>');
            } else {
                $.ajax({
                    url: 'generator.php',
                    data: $data,
                    type: 'post',
                    success: function(result) {
                        $('#result').html(result);
                    }
                });
            }
        });
    });
    </script>
</body>
</html>

This is how it looks like.


Next, we will create generator.php and paste this code.

<?php
if (isset($_POST['generatorInput'])) {
 $input = $_POST['generatorInput'];
 echo "<strong>Mouse:</strong><br> MOU-" . strtoupper(md5($input)) . "<br><br>";
 echo "<strong>Keyboard:</strong><br> KEYB-" . strtoupper(md5($input)) . "<br><br>";
 echo "<strong>Monitor:</strong><br> MON-" . strtoupper(md5($input)) . "<br><br>";
 echo "<strong>System Unit:</strong><br> SYS-" . strtoupper(md5($input)) . "<br><br>";
}
?>

Now, if you type empty it will generate error.

You can type the name of the computer owner or anything that you like.

Thats it! It automatically generate the code for you.
SHARE
    Blogger Comment
    Facebook Comment

1 comments :

  1. i was just browsing along and came upon your blog. just wanted to say good blog and this article really helped me. free eshop code

    ReplyDelete