0

I have a checkbox list of items. I need to update my database to which items were selected. The list of items is displayed dynamically.

The problem is how can i pass list of ID's through ajax?

here is my ajax code:

function update(form,div,PhpFile,type)
{
if (request)
    {
        var obj = document.getElementById(divId);
        request.open("POST",PhpFile);
        //setting the header
        request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

        request.onreadystatechange = function()
        {   
            if (request.readyState == 4 &&
                    request.status == 200){
                obj.innerHTML =request.responseText;
            }
        }

        if(type=='news')
            request.send(?????);

    }
}

I've marked with ??? the space that I don't know what to put there.

Of course I can update each of the check boxes individually but it is a very bad solution.

1
  • why do you not use ajax from jquery library ? Commented Apr 10, 2012 at 18:42

3 Answers 3

1

PHP 5 can decode JSON and in JS you will need to use a library. This seems to be a popular answer.

Serializing to JSON in jQuery

And JSON in PHP http://www.php.net/manual/en/function.json-decode.php

Sign up to request clarification or add additional context in comments.

1 Comment

Or just use JSON.stringify(theArrayVar), that way you don't need to include a huge lib. JSON is widely supported, even IE won't complain
0

I think you can serialize the array in JavaScript, pass it as a string with AJAX, and then unserialize it in PHP. Either that or convert the array values to a delimited string (comma or pipe separated) and then explode that string in PHP. That leaves your transmitted data human readable, which is nice for debugging.

Comments

0

Convert the array to JSON with JSON.stringify(), send it, then convert the JSON string back to an object in PHP with json_decode().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.