I am trying to create a simple postcode form where a users enters a postcode and if the postcode exists, the user is redirected to a link.
So, I have this html form:
<form method="get" action="index.php">
<input type="text" class="form-control input-lg" name="postcode" id="postcode" placeholder="Postcode">
<span class="input-group-btn">
<input class="btn btn-default btn-lg find" name="did_submit" id="submithome" type="submit" value="Find My Matchmaker" class="searchbutton">
</span>
</form>
And the PHP script:
<?php
if(isset($_GET['postcode'])){
$valid_prefixes = array(
2 => array('NH', 'AQ'),
3 => array('NZ2', 'GT5'),
4 => array('NG89', 'NG76')
);
foreach($valid_prefixes as $length => $prefixes) {
if (in_array(substr($_GET['postcode'], 0, $length), $prefixes)) {
header("Location: http://www.google.com/");
} else {
echo "<script> $('#myModal').modal('show') </script>";
}
exit;
}}
?>
So, if the entered postcode is CV or NG, the script will redirect the user to google and if not, a modal would be launched.
Now the question. Obviously, the users would enter CV12DB (full postcode) and they would get the modal as if the postcode doesn't exist. What I am trying to do with the PHP script is to search what the user has entered and if his postcode contains CV or NG or SH, redirect him to google.
I have tried using "preg_match" or using keys instead of the array, but with no luck.. I can't seem to figure out how to make it work.
UPDATE After replacing the code, for some reason, it only recognizes the two-letter array. For example if NG76PL is entered, it doesn't recognize it so it goes to "else"