开发者

AJAX reply as array from HTML form

This is my situation : When the user clicks on "private members only开发者_高级运维" option a search option appears . He can select the users typing their names and this is done with ajax . Now I want to print the members selected in the form so that the user can remove later if he wants before submitting the form . For that I want the id's of the searched members in an array . But I am doing something wrong , only the last selected member's id appears . Please help me getting the selected members array .

Javascript :

http.onreadystatechange = function()
    {
        if(http.readyState == 4 && http.status == 200)
        {
            //alert("selected"+id);
            document.getElementById("selected").innerHTML = http.responseText;
            //alert(http.responseText)

        }
    }
    var url = "index.php?menu=selected&ajax=ajax&id="+id;
    http.open("POST",url,true);
    http.send(null);

PHP:

            $id[] = $_REQUEST['id'];
            $this->printSelectedMembers($id);
            break;

function printSelectedMembers($id = array())
{

    var_dump($id);//returns last selected member's id
}


It's because you're replacing your div content. You should append to it. Like this:

document.getElementById("selected").innerHTML += http.responseText + ", "; //A comma to separate

You can use any separator instead of ,

Note that we replaced the + by the += operator

Hope this helps, cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜