(PHP)如果用户输入匹配,则转到特定URL
我正试图在我的网站上设置一个带有一个输入字段的表单。如果输入的是与邮政编码数组中的邮政编码匹配的邮政编码,那么我希望访问者被带到特定的url。如果输入与数组中的邮政编码不匹配,那么我希望访问者被带到不同的url 到目前为止,我的代码如下:(PHP)如果用户输入匹配,则转到特定URL,php,html,Php,Html,我正试图在我的网站上设置一个带有一个输入字段的表单。如果输入的是与邮政编码数组中的邮政编码匹配的邮政编码,那么我希望访问者被带到特定的url。如果输入与数组中的邮政编码不匹配,那么我希望访问者被带到不同的url 到目前为止,我的代码如下: <form class="new_address" id="new_address" method="POST" action="<?php bloginfo('stylesheet_directory'); ?>/zipcodes.php"
<form class="new_address" id="new_address" method="POST" action="<?php bloginfo('stylesheet_directory'); ?>/zipcodes.php">
<input placeholder="Enter your zip code here." type="text" id="zipcode" name="zipcode" />
</div>
<div class="small-4 columns">
<input class="button postfix" id="submit" type="submit" value="Book Now" />
</div>
</form>
不需要迭代$input
——事实上,这样做没有任何意义。您只需查看$input
是否在$allowedzips
数组中。我还使用trim()
修剪了用户输入的额外空白,以防万一:
<?php
$allowedzips = array('..','...');
$input = trim($_POST["zipcode"]);
if (in_array($input, $allowedzips)) {
header('Location: http://localhost:63951/booking');
exit;
}
else {
header('Location: http://localhost:63951/out-of-range');
exit;
}
?>
我认为您还不完全了解foreach()
和in_array()
的工作原理。你的问题很简单。
<?php
$allowedzips = array('..','...');
$input = trim($_POST["zipcode"]);
if (in_array($input, $allowedzips)) {
header('Location: http://localhost:63951/booking');
exit;
}
else {
header('Location: http://localhost:63951/out-of-range');
exit;
}
?>