2017-08-10 102 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,因爲它是默認值。