如何通过URL发送带有选择标记的查询

时间:2019-04-03 11:58:14

标签: php html database

我想通过url发送从select标记接收到的数据作为查询,以便我可以使用它并查询数据库。

我已经尝试过,但是没有显示值。我得到类似:type =&action =

这是我的代码

<select name="types">
    <option value="2 bed room">2 bed room</option>
</select>

<select name="action">
    <option value="rent">rent</option>
</select>

$type = $_POST['types'];
$action = $_POST['action'];

$query = "type={$type}&action={$action}";
<a class="site-btn fs-submit" href="search.php?<?php echo $query; ?>"> Advanced search</a>

我期望:

type=2 bed room&action=rent

2 个答案:

答案 0 :(得分:0)

使用按钮替换链接。将链接中的查询参数附加到表单的操作。您的代码应如下所示。

<form action="search.php?<?php echo $query; ?>">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>

<select name="action">
    <option value="rent">rent</option>
</select>

<?php

$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";

?>

<button class="site-btn fs-submit" type="submit">Submit</button>
</form>

答案 1 :(得分:0)

尝试以下代码。在这里,提交表单时,整个表单数据将传递到search.php,并且表单方法为POST,因此您只需调用$_POST['attribute value of name']就可以访问这些数据。

HTML表单:

<form class="filter-form" method="post" action="search.php" enctype="multipart/form-data"> 
    <input type="text "class="d-block d-md-inline" placeholder="Enter State, City or Area" name="city"> 
    <select name="types">
        <option value="2 bed room">2 bed room</option>
    </select> 
    <select name="action"> 
        <option value="rent">rent</option> 
    </select>
    <input type="submit" value="Advanced search">
</form>

创建一个PHP文件(search.php):

<?php
//Do what you want here
if (isset($_POST['city'])) {
    $city = $_POST['city'];

    echo $city;
}

if (isset($_POST['types'])) {
    $type = $_POST['types'];

    echo $type;
}

if (isset($_POST['action'])) {
    $action = $_POST['action'];

    echo $action;
}
?>