2014-02-28 5 views
0

현재 우편 요금에서 내 결제로 주소 조회를 구현하고 있습니다.주소 찾기 자바 스크립트에서 우편 번호를 사용하여 어디서나, PHP에 키를 넣어야합니다

나는 계산대와 함께 작업하고 있지만 지금은 실제로는 할 수없는 순수한 JavaScript입니다. 즉, 내 우편 번호 아무 데나 키도 함께 표시된다는 의미입니다.

분명히 나는이 물건 서버 쪽 (PHP로 내 경우에는)의 일부를 할 필요가있다. 그러나 나는 그것이 얼마나 많이 그리고 어떻게 연결되어 있는지에 대해 잘 모릅니다. 나는 여기에 PCA_Find_By_Parts.html 자바 스크립트 예제 주위에 내 현재 솔루션 구축 :

http://www.postcodeanywhere.co.uk/support/sample-code.aspx

을하지만 디스플레이의 핵심 존재로 언급 한 바와 같이 나에게 이것은 매우 안전하지 보인다.

답변

1

백엔드 (PHP)에서 모든 "처리"를 수행하고 Ajax 기능을 사용하여 관련 데이터를 전달하기를 원할 것입니다.

이상 여기 좋은 예있다 :

http://downloads.postcodeanywhere.co.uk/dev/pcaphpsamples.zip 
http://downloads.postcodeanywhere.co.uk/dev/pcaphpsamples.zip 
http://downloads.postcodeanywhere.co.uk/dev/phpSOAP.zip 

당신은 또한 당신이 시작하는 데 다음과 같은 코드를 사용할 수 있습니다 :

https://www.postcodeanywhere.co.uk/address-validation/guide/default.aspx?reg=1 

은 사이트에서 PHP 예제

있습니다

class PostcodeAnywhere_Interactive_FindByPostcode_v1_00 
{ 

    //Credit: Thanks to Stuart Sillitoe (http://stu.so/me) for the original PHP that these samples are based on. 

    private $Key; //The key to use to authenticate to the service. 
    private $Postcode; //The postcode to search with find. 
    private $UserName; //The username associated with the Royal Mail license (not required for click licenses). 
    private $Data; //Holds the results of the query 

    function PostcodeAnywhere_Interactive_FindByPostcode_v1_00($Key, $Postcode, $UserName) 
    { 
     $this->Key = $Key; 
     $this->Postcode = $Postcode; 
     $this->UserName = $UserName; 
    } 

    function MakeRequest() 
    { 
     $url = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/FindByPostcode/v1.00/xmla.ws?"; 
     $url .= "&Key=" . urlencode($this->Key); 
     $url .= "&Postcode=" . urlencode($this->Postcode); 
     $url .= "&UserName=" . urlencode($this->UserName); 

     //Make the request to Postcode Anywhere and parse the XML returned 
     $file = simplexml_load_file($url); 

     //Check for an error, if there is one then throw an exception 
     if ($file->Columns->Column->attributes()->Name == "Error") 
     { 
     throw new Exception("[ID] " . $file->Rows->Row->attributes()->Error . " [DESCRIPTION] " . $file->Rows->Row->attributes()->Description . " [CAUSE] " . $file->Rows->Row->attributes()->Cause . " [RESOLUTION] " . $file->Rows->Row->attributes()->Resolution); 
     } 

     //Copy the data 
     if (!empty($file->Rows)) 
     { 
     foreach ($file->Rows->Row as $item) 
     { 
      $this->Data[] = array('Id'=>$item->attributes()->Id,'StreetAddress'=>$item->attributes()->StreetAddress,'Place'=>$item->attributes()->Place); 
     } 
     } 
    } 

    function HasData() 
    { 
     if (!empty($this->Data)) 
     { 
     return $this->Data; 
     } 
     return false; 
    } 

} 

//Example usage 
//------------- 
//$pa = new PostcodeAnywhere_Interactive_FindByPostcode_v1_00 ("AA11-AA11-AA11-AA11","WR2 6NJ","David"); 
//$pa->MakeRequest(); 
//if ($pa->HasData()) 
//{ 
// $data = $pa->HasData(); 
// foreach ($data as $item) 
// { 
//  echo $item["Id"] . "<br/>"; 
//  echo $item["StreetAddress"] . "<br/>"; 
//  echo $item["Place"] . "<br/>"; 
// } 
//} 


http://www.postcodeanywhere.co.uk/support/webservices/PostcodeAnywhere/Interactive/FindByPostcode/v1/default.aspx 

필요한 경우 ajax를 사용하여 전화 걸기 :

jQuery.ajax({ 
    url: "yourScript.php", 
    type: 'POST', 
    data: {action: 'get_postcode', id: ui.item.id }, 
    success: function(data) { 
     try { 
      var response = jQuery.parseJSON(data); 
      jQuery('#pickPostcode').val(response.postcode); 
     } catch(err) {} 
    } 
}); 
+0

안녕하세요 저는이 모든 것을 지쳤습니다.하지만 모두 포스트 코드 조회가 양식 내에 있고 양식 내에 양식이 없어야하므로 실제로 할 수없는 게시 양식 요청을 수행하는 것처럼 보입니다. 나는 아약스와 같은 종류의 해결책을 원했지만 어떻게 할 것인가? – David

+0

@David : 이것은 AJAX 솔루션입니다. 양식을 사용하지 않으려면 양식을 사용할 필요가 없습니다. – halfer

+1

Opps 미안하지만, jquery 비트를 보지 못했 덕분에 그것을 시도 줄 것이다! – David