2017-08-10 100 views
0

我有一个php web应用程序,我可以创建发票和添加客户端。为了我自己的方便,我尝试实现一项功能,当我输入客户的增值税号码时,“其他”细节(如adres,电话号码,公司名称等)应自动填充,加载。试图通过使用jquery窗体自动填充 - ajax和php-curl

所以为了实现这一点,我把一个fa-search-icon放在了vat输入栏旁边。每当有人填写增值税号并点击搜索图标,jquery处理程序就会向特定的php文件发送一个ajax请求,在这里我使用公司增值税号卷曲一个站点。我将卷曲页面保存为html文件(输出为html)。 html文件包含所需的详细信息,如adres,电话号码等。

更新注意:值(phone,adres,name)为html实体格式,例如:K P   D生态r之间&#否则代码

IVE认沽间隔会自动改革(当你删除所有的空格)KP装饰,所以上面的HTML实体K P D生态r被写入。

我不是一个真正的专业人士,所以我的问题是我怎么能把这些价值观传递给我的addclient形式。

HTML端:

<div class="col-md-8"> 
<input type="text" id="btwnr" name="client_tax_number" class="form-control" placeholder="<?php _e('placeholder_tax_number'); ?>" /> 
</div> 
<div class="control-label col-md-1"> 
<i class="fa fa-search fa-2x" id="btwopvragen"></i> 
</div> 

jQuery的一部分:

<script> 
$(document).ready(function(){ 
    $("#btwopvragen").click(function(){ 
    var btwnrVal = $("#btwnr").val(); 
    $.ajax({ 
    type: "GET", 
    url: "https://www.example.com/FOLDER/FOLDER/btwopvragen.php", 
    data: {btwnrVal}, 
    success: function() { 
     $("#bedrijfsnaam").load(
      "http://www.domain.eu/map/540806177.html #StatNameLabel"); 
    } 
    }); 
    }); 
}); 
</script> 

PHP的一部分btwopvragen.php:

<?php 
$btwnrVal = $_GET['btwnrVal']; 
// $btwnrVal = "09999999"; MANUEL TEST 
$curlUrl = "https://trendstop.knack.be/nl/detail/".$btwnrVal; 

// create curl resource 
    $ch = curl_init(); 

    //opening text File 
    $fp = fopen($btwnrVal, "w"); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, $curlUrl); 

    curl_setopt($ch, CURLOPT_FILE, $fp); 

    //whether to include the header in the curl, set to false. 
    curl_setopt($ch, CURLOPT_HEADER, 0); 

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    //save output to File 
    fwrite($fp, $output); 

    // close curl resource to free up system resources 
    curl_close($ch); 

    //closes the txt File 
    fclose($fp); 
    //prints the output 
    // echo $output; 
?> 

enter image description here

+0

你在cURL响应中得到了什么? HTML? JSON? XML?文本?图片?你需要创建一个json响应,你的PHP文件返回到ajax函数,它只是用正确的值更新表单。 –

+0

而'$ _GET [btwnrVal]'应该是'$ _GET ['btwnrVal']'。感谢您指出单引号 –

+0

。至于输出它是在html中,但我想要的值显示为html值。例如: &# 75; P &# 32; &# 68; eco &# 114;如果您删除间隔它自动改革为KP装饰。 你可以详细介绍一下创建json并返回ajax吗?第一次这样做。我试图在互联网上搜索,但没有多少发现。 –

回答

0

你需要像下面:

$.ajax({ 
    url:'https://www.example.com/FOLDER/FOLDER/btwopvragen.php', 
    dataType:'json', 
    success:function(data) { 
    $("#btwopvragen").val(data.btw.opvragen); 
    } 
}) 

这假设你有一个ID为btwopvrageb的元素。

您也不需要type: GET,因为它是默认值。