Created
February 20, 2015 13:45
-
-
Save stephenkeable-allies/74dcb6b94d5f70dcc4b5 to your computer and use it in GitHub Desktop.
Basic PHP Postcode Validation Example using PostCoder Web, find out more at - http://developers.alliescomputing.com/postcoder-web-api/address-lookup/validate-postcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Check if POST request has been made | |
if($_SERVER['REQUEST_METHOD'] === 'POST') { | |
// Basic validation on the postcode field | |
if(array_key_exists('postcode', $_POST) and !empty($_POST['postcode'])) { | |
// Insert your PostCoder Web API Key here | |
// Sign up for free at www.alliescomputing.com/postcoder/sign-up | |
$pcw_api_key = "YOUR-API-KEY-HERE"; | |
// URL we are calling with API key and url encoded postcode | |
$pcw_validate_url = "http://ws.postcoder.com/pcw/". $pcw_api_key ."/codepoint/validatepostcode/" . rawurlencode($_POST['postcode']); | |
// Get and JSON decode the response from the service | |
$response = json_decode(file_get_contents($pcw_validate_url)); | |
// Was the response TRUE or FALSE, show the correct message | |
if($response === FALSE) { | |
echo "<p><strong>" . $_POST['postcode'] . "</strong> - IS NOT a valid postcode</p>"; | |
} else { | |
echo "<p><strong>" . $_POST['postcode'] . "</strong> - IS a valid postcode</p>"; | |
} | |
} | |
} | |
// Basic POST submit form HTML below | |
?> | |
<form action="postcode-validation.php" method="post"> | |
<fieldset> | |
<label for="postcode">Postcode:</label> | |
<input type="text" name="postcode" id="postcode"> | |
</fieldset> | |
<input type="submit" value="Validate"> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment