To Convert Number To Words in PHP Ajax, we use numfmt_create()
and numfmt_format()
function. Both functions are inbuilt PHP functions which also part of Human Language and Character Encoding Support.
These functions are the defined under the intL (Internationalization Functions) functions.
To Convert Number To Words, first, we create an HTML form where we take the number from the user and then send it the PHP file using Ajax with the POST method.
You can check live view below.
Before we start, we have to enable Intl extension on the PHP configuration file,
- Open PHP configuration file (php.ini)
- Path:
C:\xampp\php\php.ini
(if you use Xampp)
- Path:
- Remove semicolon (;) from “
extension=intl
” it. - Now restart the Apache server.
Above are the necessary steps before the code run.
Here is 2 steps to Convert Number To Words in PHP using Ajax,
- Create an HTML input and Ajax Script.
- Create a PHP script to convert the input number to the word.
Create an HTML input With Ajax Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<!DOCTYPE html> <html> <head> <title>How to convert number to words in PHP Ajax</title> </head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <body> <p><input type="number" id="numberInput"></p> <p><button id="convert">Convert to Word</button></p> <p id="word"></p> </body> <script type="text/javascript"> $(document).ready(function (e) { $("#convert").click(function(){ var numberInput = $("#numberInput").val(); $.ajax({ type: 'POST', url: 'numtoword.php', data: { numberInput: numberInput }, success: function(response) { $("#word").html(response); } }); }); }); </script> </html> |
Code Explanations:
- Here we create an input element and button with an ID attribute.
- On the Ajax file, we take the input data by ID attribute.
data: { numberInput: numberInput }
- Then send the data using the POST method to our PHP snippet.
type: 'POST',
url: 'numtoword.php',
data: { numberInput: numberInput },
- At last, take the success message from the PHP file and print it to the web page.
success: function(response) {
$("#word").html(response);
}
PHP script to convert the input number to word
numtoword.php
1 2 3 4 5 6 7 |
if(isset($_POST['numberInput'])){ $numberInput = $_POST['numberInput']; $locale = 'en_US'; $fmt = numfmt_create($locale, NumberFormatter::SPELLOUT); $in_words = numfmt_format($fmt, $numberInput); echo $in_words; } |
Code Explanations:
- First, we check the input is set or not using the PHP
isset()
function. - Then we use
numfmt_create()
andnumfmt_format()
function to convert the number to the word. - At last, we print that out which show by Ajax on the page.
Live View of Convert Number to Words in Ajax
To know more about number format function on the PHP official site, https://www.php.net/manual/en/numberformatter.create.php
Also Check:
- How to Get HTML Tag Value in PHP
- How to Keep Value After Page Reload in PHP
- 2 Ways to Open URL in New Tab Using JavaScript
- How to Embed PDF in Website Using HTML
Happy Coding..!