如何使用一个表单元素的多个值获取请求

时间:2016-04-22 15:03:41

标签: php get html-form

我有一个表单,其元素是下拉菜单,您可以从中选择多个值。

例如:

<form class="ui form" id="filterImageForm" action="query.php">
    <div class="field" id="species" style="display: none;">
        <label>Species:</label>
        <select class="ui dropdown multiple" name="species" id="speciesSelect">
        <option></option>
        </select>
    </div>
    <input type="submit" class="ui primary button">
</form>

这些值由以下代码确定:

function populateSelect(selectName){

    // don't cache get request responses
    $.ajaxSetup({ cache : false });

    // form get request
    var data = "request="+selectName;

    // send get request to formFill.php which interacts with db
    $.getJSON("formFill.php", data, function(response){

    // select the select element
    var select = document.getElementById(selectName+'Select');

    // for each response from db, create new option for select and add the option to the select box
    for (var i = 0; i < response.length; i++) {
    var opt = document.createElement('option');

    if (selectName == 'trapper' || selectName == 'trapper_site') {
        opt.innerHTML = response[i].option_id;
        opt.value = response[i].option_id;
    } else {
        opt.innerHTML = response[i].option_id + ' - ' + response[i].option_name;
        opt.value = response[i].option_id;


        select.appendChild(opt);
    }
});

其中formfill.php是一个查询数据库并在数组中回显结果的脚本。

因此,表单将有一个可供选择的物种列表。

在提交时,表单将传递给query.php。但是,如果选择了多个物种,则php页面的URL类似于:http://localhost/query.php?species=Hedgehog&species=Rabbit

如何提交它以便我可以使用这两个值,而不是第二个值被第二个值覆盖(在这种情况下,如果我回显$_GET['species']我得到&#34; Rabbit&#34;,和&#39; Hedgehog的价值丢失了)。理想情况下,我会得到一个包含一系列物种的值。

提前致谢。

0 个答案:

没有答案