MOLEDesign MOLEDesign - 1 month ago 7x
PHP Question

Making an asychronus call to a php script

I have a script running on my server that takes a VAT number delivered via URL, and checks it

(note the url is broken into two values the first 2 letters and all the numbers behind it as VIES (vat checker) uses those two variables. The script works fine (I have included it below the question for reference). To call it..


and it returns {“is_valid”:true} or {“is_valid”:false}

Now, I have a form on my site with the following fields

<input type=“text” name=“vat_number” id=“vat_number”>
<input type=“text” name=“vat_number_confirmed” id=“vat_number_confirmed”>
<input type=“hidden” name=“vat_ok” id=“vat_ok” value=“0”>

and finally a message box

<div id=“vat-ok” style=“display:none;”>Your VAT is ok</div>
<div id=“vat-error” style=“display:block;”>Your VAT is not valid</div>

The site is PHP and I have already done chunks of the code

My scenario, as soon as characters are typed in the field vat_number I want to do an async called to vat_checkers.php with the ongoing value being typed in vat_number and check it. This should be live, and constant everytime there is an input.. and when its true, it gets saved, and does a few others thing listed below.


$(‘#vat_number’).on('input', function() {
// do my check on vat checker.php with vat=$(“#vat_number”).val()

// when returned response is {“is_valid”:true}

document.getElementById(“vat_ok”).value = “1”;
document.getElementById(“vat_number_confirmed”).value = $(“#vat_number”).val()
$(“#vat_number”).attr(“disabled”, true);


Can anyone advise how to put all these blocks together into a working method as my brain will not grasp this final link. Thank you in advance for any assistance

For reference... the vat script VIES VAT Validator on GitHub


class VatValidator
public $response;
protected $soap;

// WSDL VIES Url Service.
protected static $url_vies = '';

// Valid european countries ISO codes.
protected static $european_countries = array(
'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK'

public function __construct ()
$this->soap = new SoapClient( self::$url_vies );

* Check if it's a valid vat number.
public function checkVat ( $country, $number )
$response = array( 'is_valid' => false );
$vat = $this->prepareVat( $country, $number );
if ($vat)
$this->response = array( 'is_valid' =>$this->soap->checkVat( $vat )->valid );
return json_encode( $this->response );

* Checks that there are all needed params ( Code Country and number );
protected function prepareVat( $country, $number )
if ( empty( $country ) || empty( $number ) )
throw new Exception( "Both 'country' and 'number' params are mandatory" );

if ( !in_array( $country, self::$european_countries ) )
throw new Exception( "Invalid country" );

$vat = array(
'vatNumber' => $number,
'countryCode' => $country,

return $vat;

catch( Exception $e )
$this->response = array( 'error_message' => $e->getMessage() );
return false;

// API Call
$vies = new VatValidator();

if ($_GET['vat']) {
$vat = $_GET['vat'];
$country = substr($vat, 0, 2);
$number = substr($vat, 2);
} else {
$country = "";
$number = "";

$vies->checkVat( strtoupper( $country ), $number);
$response = json_encode( $vies->response);
echo $response;

EDIT: This was my final working solution, combination of the article linked above and feedback from a user below

var searchRequest = null;

$(function () {

var minlength = 4;

$("#jform_company_vat_tmp").keyup(function () {
value = $(this).val();

if (value.length >= minlength ) {
if (searchRequest != null)
searchRequest = $.get("(website)/vies.php", { vat: value } )
.done(function(data) {
checkBit = JSON.parse(data);
for (var key in checkBit) {
if (checkBit[key] == true) {

$("#jform_company_vat_tmp").prop( "disabled", true );



Reading the question, I assume you have the true/false response from the PHP file.

$.get("vatchecker.php", { vat: "xx1234567" } )
    .done(function(data) {
        if (data === "true") {