I have one field (for zip code), if 开发者_开发百科user enters zip code that matches one in my array and click "go" - they are redirected to the next step, if not - display "out of our service area" message. Form and php script are on the same page. How Do I do redirection inside That's what I have so far.
<form>
<input type="text" id="zipcode" name="zipcode" />
<input type="submit" id="submit" value="GO" />
</form>
<?php
$allowedzips = array("10051", "10061", "10071", "10081");
$input = echo $_POST["zipcode"];
$input = str_split($input);
$message = "Out of our service area";
foreach($input as $zip) {
if (in_array($zip, $allowedzips)) {
$message //redirect goes here
break;
}
}
echo $message;
?>
A PHP redirect is quite easy to do:
header('Location: www.example.com');
exit;
Make sure to exit;
, as that will stop the script from executing any further.
I think it would be better to use javascript instead of PHP for how you are looking to do this.
What I would do is add this into the head of your html
<script type="test/javascript">
function checkZipCode(zip) {
var myZipCodes=new Array("10051", "10061", "10071", "10081");
for(i=0;i<myZipCodes.length;i++) {
if (zip = myZipCodes[i]) {
form.submit;
}
else
window.location = "redirectpage";
}
}
</script>
And then on submit call and pass the zip code to checkZipCode.
I would do this in PHP to ensure that you're not tricked on the client side
Use the in_array()
function to determine if the element you're looking for is within you list of valid zip codes, and redirect accordingly.
精彩评论