![](/img/trans.png)
[英]How do I get the value of a form dropdown from bootstrap 4 when immediatly selected using PHP?
[英]How do I get a value from dynamically created form in a dropdown menu using PHP?
我正在尝试构建一个应用程序,该应用程序将根据用户输入的邮政编码将用户定向到其他网站。 为了缩小范围,在某些情况下,用户可能需要从动态创建的街道名称下拉列表中进行选择。 该应用程序使用PHP和一个mySQL数据库。 PHP部分下的代码注释中详细说明了我遇到的问题。 如果可能,请提供PHP驱动的解决方案。 如果jQuery,Javascript,AJAX等是唯一的选择,请给我一些示例,说明如何在此示例中实现它。 谢谢!
这是HTML的代码段:
<div id="mybox">
<form method="post">
<input type="text" name="zipcode">
<input type="submit" value="Enter Zip Code" name="submitzip">
</form>
</div>
这是通过单击Submit按钮调用的PHP:
<?php
if (isset($_POST['submitzip'])) {
ZipLookup(); }
function ZipLookup() {
$zipcode = $_POST["zipcode"];
$zipcode = trim($zipcode);
if (strlen($zipcode) != 5) {
echo ("<strong>Please enter a valid five digit Zip Code.</strong>");
}
else {
$servername = "ip here";
$username = "username here";
$password = "password here";
$dbname = "dbname here";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT ZipCode, StreetName, URL FROM db table here WHERE ZipCode ='$zipcode'";
$result = $conn->query($sql);
if ($result->num_rows > 1) {
echo '<hr><p><strong>We service more than one area in ' .$zipcode. '. Please select your street name.</strong></p>';
echo '<form method="post">';
echo '<select name="streetname">';
while($row = $result->fetch_assoc()) {
echo '<option value="' . $row["URL"] . '">' . $row["StreetName"] . '</option>';
}
echo '</select> <input type="submit" value="Submit" name="submitname"> </form>';
echo '<p>If your street name is not listed, please contact us.</p>';
// THIS IS WHERE I HAVE PROBLEMS... Values from dropdown don't appear to be stored. When running var_dump, PHP exports the array from the first button. Clicking the dynamic submit button just reloads the page.
if (isset($_POST['submitname'])) {
$url = $_POST["streetname"];
wp_redirect($url); }
}
elseif ($result->num_rows == 1) {
while($row = $result->fetch_assoc()) {
$url = $row["URL"];
wp_redirect($url);}
}
else {
echo '<strong>We do not provide service to this area.</strong>';
$conn->close();
}
}
}
?>
第二种形式正在发回到该页面,并且第二种形式似乎没有传递邮政编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.