Generate md5 Hash in PHP and Ajax without Form Submit
md5 is a hash function that generates the md5 hash of an input string. This is used for the encoding process. Though it is not used as password encryption, it is widely used to store sensitive data using 128-bit hash value.
In this topic, we will see how we can generate the md5 hash of a string using PHP and Ajax. A user can enter an input string and by hitting a button md5 hash value can be displayed.
Folders and Files
We will use a folder called 'md5' under 'xampp/htdocs'.
'css' folder is for custom stylesheet.
'js' folder is for our JavaScript file.
index.php is the main program and get_md5.php generates md5 hash and it is called by Ajax.
Create an input screen (index.php)
We will design a small screen that will have an input box. Users can enter the string for which the md5 hash is to be generated. There will be an output box where the generated md5 hash will be displayed. See the below screenshot:
We are calling generate_md5() JavaScript function on onclick event of the button. These functions are defined in md5.js file.
Write JavaScript/jQuery for Ajax Call (md5.js)
Let us now write the JavaScript function generate_md5(). We will make an Ajax request with the input value entered by the user. Response from the Ajax call will be displayed as the output. Let's see the below code:
md5.js
function genearte_md5() {
var x = document.getElementById("inputStr").value;
x = x.trim();
if (x !="") {
$.ajax({
type:"post",
data:"str="+x,
url:"get_md5.php",
success: function (response){
$("#md5hash").html(response);
}
});
}
else alert("Please enter a string");
}
function clear_str() {
$("#inputStr").val("");
$("#md5hash").html("");
}
In the function generate_md5(), we are calling get_md5.php using Ajax with the input string. The other function clear_str() simply removes the data from the input textarea field and clears the generated md5 hash.
Write PHP code to generate md5 hash (get_md5.php)
We will take the value of the input string sent by Ajax as a parameter and then call the md5() function on the input string. Then display it using an echo statement.
Make sure services are running in your XAMPP control panel. Run localhost/md5 in the browser. You will see the page displayed.
Enter some string in the left input box and click on the "Generate md5 hash" button. You will see the hash generated and displayed on the right side. I entered 123 and clicked the blue button. I got the output as below:
Hope you could develop and test it successfully.
Note
md5() hash is not recommended to use for password encryption. For training or testing purpose you may use it, but not in actual project. For password encryption you can use password_hash() and password_verify() functions.
Download Source Code
Download the source code by clicking on the download button below:
Conclusion
This was also an example of using Ajax without submitting a form. So, this gives you an idea on how you can use Ajax with PHP. In this case, we generated an md5 hash using Ajax in PHP. I hope it will be useful for you.
Post a Comment