我正在尝试向正在构建的注册页面添加一些功能。该功能是从邮政编码中查找地址,并将该数据添加到输入表单中。我找到了一种方法来执行此操作,但是我执行该操作的方式涉及一些问题。目前,我正在使用带有提交按钮的表单,然后使用if(isset($ _ post),这意味着一旦点击了我不希望的提交按钮,页面将被重新加载。我的想法是,我需要使用一些jquery或ajex来完成此任务,而无需重新加载表单,但是老实说,我不确定从哪里开始。我编写了一个函数,该函数以数组形式返回我需要的数据postcode_api_test.php<?phpfunction lookuppostcode($postcode){ $postcode = str_replace(" ", "", $postcode); $key = 'MY API KEY'; $request = 'https://api.getAddress.io/v2/uk/' . $postcode . '?api-key=' . $key; $response = file_get_contents($request); $jsonresponse = json_decode($response, true); $returnData = array(); $returnData['lat'] = $jsonresponse['Latitude']; $returnData['lon'] = $jsonresponse['Longitude']; $returnData['totalAddresses'] = count($jsonresponse['Addresses']); $addressArray = explode(',', $jsonresponse['Addresses'][0]); $returnData['locality'] = $addressArray[4]; $returnData['city'] = $addressArray[5]; $returnData['county'] = $addressArray[6]; $returnData['addresses'] = array(); foreach ($jsonresponse['Addresses'] as $address) { $addressArray = explode(',', $address); $fullAddress = ''; foreach ($addressArray as $item) { if ($item != ' ') { $fullAddress .= $item . ","; } } $fullAddress = trim($fullAddress, ","); $returnData['addresses'][] = array( 'line1' => $addressArray[0], 'line2' => $addressArray[1], 'line3' => $addressArray[2], 'line4' => $addressArray[3], 'locality' => $addressArray[4], 'city' => $addressArray[5], 'county' => $addressArray[6], 'fullAddress' => $fullAddress, ); } return $returnData;}?>
1 回答
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报
0/150
提交
取消