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.

How to Convert Number to Words in PHP Ajax
How to Convert Number to Words in PHP Ajax

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)
  • Remove semicolon (;) from “extension=intl” it.
  • Now restart the Apache server.
Enable intl extension PHP

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

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

Code Explanations:

  • First, we check the input is set or not using the PHP isset() function.
  • Then we use numfmt_create() and numfmt_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

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:

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *