我的服务器上运行了一个脚本,该脚本通过URL传送增值税号,并进行检查
(注意url被分为两个值,前2个字母和后面的所有数字,因为VIES(vat checker)使用这两个变量。脚本工作正常(我把它包含在问题下面以供参考)。叫它..
(website)/vatchecker.php?vat=xx1234567
并返回{“is_valid”:true}或{“is_valid”:false}
现在,我的网站上有一个表格,其中包含以下字段
<input type=“text” name=“vat_number” id=“vat_number”>
<input type=“text” name=“vat_number_confirmed” id=“vat_number_confirmed”>
and
<input type=“hidden” name=“vat_ok” id=“vat_ok” value=“0”>
最后是一个消息框
<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>
网站是PHP,我已经完成了代码块
我的场景,只要在字段vat_number中输入字符,我就想对vat_checkers.php进行异步调用,并在vat_number中输入正在进行的值并检查它。这应该是实时的,并且每当有输入时都是常量的......当它真实时,它会被保存,并且会在下面列出其他一些内容。
即
$(‘#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);
$(“#vat-ok”).show();
$(“#vat-error”).hide();
});
任何人都可以建议如何将所有这些块放在一起工作,因为我的大脑不会掌握这个最后的链接。提前感谢您的任何帮助
供参考......大桶脚本VIES VAT Validator on GitHub
<?php
class VatValidator
{
public $response;
protected $soap;
// WSDL VIES Url Service.
protected static $url_vies = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
// 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 )
{
try
{
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;
?>
编辑:这是我最后的工作解决方案,上面链接的文章和来自
以下用户的反馈的组合<script>
var searchRequest = null;
$(function () {
var minlength = 4;
$("#jform_company_vat_tmp").keyup(function () {
value = $(this).val();
if (value.length >= minlength ) {
if (searchRequest != null)
searchRequest.abort();
searchRequest = $.get("(website)/vies.php", { vat: value } )
.done(function(data) {
console.log(data);
checkBit = JSON.parse(data);
for (var key in checkBit) {
console.log(checkBit[key]);
}
if (checkBit[key] == true) {
$("#vat_switch").val(1);
$("#vat_number_confirmed").val($("#jform_company_vat_tmp").val());
$("#jform_company_vat_tmp").prop( "disabled", true );
$(".vat-is-ok").show();
$(".vat-is-not-ok").hide();
}
});
}
});
});
</script>
答案 0 :(得分:1)
阅读问题,我假设您有来自PHP文件的真/假响应。
$.get("vatchecker.php", { vat: "xx1234567" } )
.done(function(data) {
if (data === "true") {
$("#vat_ok").val(1);
$("#vat_number_confirmed").val($("#vat_number").val());
$("#vat_number").addClass("disabled");
$("#vat-ok").show();
$("#vat-error").hide();
}
});