简体   繁体   English

如何在php ajax中获取多个列表框值以获取URL?

[英]How to pass multiple listbox values in php ajax get url?

I also want a listbox with multiple selection to use along with the above code, but its not working 我还希望将具有多个选择的列表框与上面的代码一起使用,但是它不起作用

$cnty is the variable (listbox - multiselection). $ cnty是变量(列表框-多选)。

Below is my complete ajax function used . 下面是我使用的完整ajax函数。

<script language="javascript" type="text/javascript">


function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            //document.myForm.time.value = ajaxRequest.responseText;
            document.getElementById("result").innerHTML=ajaxRequest.responseText

        }
    }

    var dav = document.getElementById('dav').value;
    var pathogen = document.getElementById('pathogen').value;
    var topic1 = document.getElementById('topic1').value;
    var ind1 = document.getElementById('ind1').value;
    var subindg1 = document.getElementById('subindg1').value;
    var cnty = document.getElementById('countryRF').value;

    var queryString = "?dav=" + dav + "&pathogen=" + pathogen + "&topic1=" + topic1 + "&ind1=" + encodeURIComponent(ind1) + "&subindg1=" + encodeURIComponent(subindg1) +  "&cnty=" + encodeURIComponent(cnty);
    ajaxRequest.open("GET", "sortby.php" + queryString, true);
    ajaxRequest.send(null);
}


</script>

sortby.php page sortby.php页面

<?php

                                $con = mysql_connect("localhost","root","adminpp");
                                mysql_select_db("pdata", $con);
$datav=$_GET["dav"];
$pathogen=$_GET["pathogen"];
$topic1=$_GET["topic1"];
$ind1=$_GET['ind1'];
$subindg1=$_GET["subindg1"];
$cnty=$_GET['cnty'];



echo $subindg1;
echo $cnty;

?>

There's a variety of ways to pass a multiselect list to the server through Ajax. 有多种方法可以通过Ajax将多选列表传递到服务器。 This is just one of many... and probably not even the best. 这只是众多中的一种...也许甚至不是最好的。 :) :)

I'm going to use the variable name multisel throughout so you can find it easily and see how to use it. 我将在整个文章中使用变量名multisel ,以便您可以轻松找到它并了解如何使用它。

Add this function to your javascript 将此功能添加到您的javascript

function loopSelected(selObj)
{
    var selectedArray = new Array();
    var i;
    var count = 0;
    for (i=0; i<selObj.options.length; i++) {
        if (selObj.options[i].selected) {
            selectedArray[count] = selObj.options[i].value;
            count++;
        }
    }
    return selectedArray;
}

Now, add the following lines to ajaxFunction just after your variables. 现在,在变量之后ajaxFunction添加到ajaxFunction

var selObj = document.getElementById('multistore');

var multisel = loopSelected(selObj).join('~');  // join array into a string

Finally, in PHP, add these lines 最后,在PHP中,添加以下行

$multisel = $_GET['multisel'];
$multisel_array = explode('~',$multisel);   // split the items into an array

var_dump($multisel_array);

At this point all of the selected items are in $multisel_array . 此时,所有选定项都在$multisel_array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM