0

I'm currently implementing a address lookup from Postcode into my checkout.

I have got it working with my checkout but it's all pure JavaScript at the moment, which I can't really do as that means my Postcode Anywhere key is also on display.

Obviously I need to do part of this stuff server side (in my case with PHP). But I'm not quite sure how much and how it all connects. I built my current solution around the PCA_Find_By_Parts.html JavaScript example here:

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

but to me this seems not very secure as I mentioned with the key being on display.

halfer
  • 19,824
  • 17
  • 99
  • 186
David
  • 139
  • 3
  • 15

1 Answers1

1

You will want to do all the "processing" on the backend(PHP) and just pass over the relevant data using an Ajax function.

There's a great example over here:

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

There are PHP examples on the site at:

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

You can also use the following code to get you started:

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

use ajax to make the call when required:

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) {}
    }
});
AO_
  • 2,573
  • 3
  • 30
  • 31
  • Hi yeah I have tired these but they all seem to revolve around doing a post form request which I can't really do as the postcode lookup with is within a form and you shouldn't have a form within a form. I was hoping for a ajax sort of solution but I can't work out how I would do this? – David Feb 28 '14 at 10:32
  • @David: this _is_ an AJAX solution. It does not need to use a form if you don't want it to. – halfer Feb 28 '14 at 10:42
  • 1
    Opps sorry didn't see the jquery bit, thanks will give it a try! – David Feb 28 '14 at 10:53