CodeIgniter Forums
[WORKED AROUND] Passing a checkbox array... Nerve-racking. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [WORKED AROUND] Passing a checkbox array... Nerve-racking. (/showthread.php?tid=2386)



[WORKED AROUND] Passing a checkbox array... Nerve-racking. - El Forum - 08-02-2007

[eluser]Morty[/eluser]
Hi there !

My code is partly based on Derek Allard sample application. However I need to pass a checkbox array to my CI AJAX receiver:

Code:
$filter = $this->input->post('filter');

Without AJAX it works gracefully but when I try to pass the array through via the following JS function, it does not look like my standard array.

JS function:
Code:
function inline_results() {
    $filterstring = '';

    res = document.getElementsByName('filter[]')
    for (i = 0; i < res.length; i++)
        if (res[i].checked == true)
            $filterstring += '&filter;[]=' + res[i].value;

    if ($filterstring == '')
        $filterstring = '&filter=false';

    alert('stock_site='+$F('stock_site'+'&stock;_product='+$F('stock_product'+$filterstring);

    new Ajax.Updater ('stock_values', base_url+'stocks/ajaxstocksearch', {method:'post', postBody:'stock_site='+$F('stock_site')+'&stock;_product='+$F('stock_product')+$filterstring});

There should be a way to obtain the same array as if it was posted by a standard form. At the moment, what I get from this is :

1. 'ITEM1' and 'ITEM2' checked : array('ITEM1' = 0, 'ITEM2' = 1); => Nearly what I want
2. 'ITEM3' checked with or without other choices : 'ITEM3' never shows up;
3. Nothing checked : string false. => Not exactly what I want but I can convert it.

I can post other details if it can help you to put me on the right tracks.

Thanks in advance,
Morty

P.S: It seems that ";" are being added in my code by the forum. They don't exist in my actual code.


[WORKED AROUND] Passing a checkbox array... Nerve-racking. - El Forum - 08-02-2007

[eluser]coolfactor[/eluser]
[quote author="Morty" date="1186060878"]P.S: It seems that ";" are being added in my code by the forum. They don't exist in my actual code.[/quote]

Yes, that is an annoying, but necessary security measure apparently. The &, followed immediately by some text will lead to the ; being inserted, as the post parser thinks (or is trying to convert) that to an html entity.


[WORKED AROUND] Passing a checkbox array... Nerve-racking. - El Forum - 08-02-2007

[eluser]Morty[/eluser]
It does not really annoy me, I just wanted people to know that my code does not contain such mistakes Smile


[WORKED AROUND] Passing a checkbox array... Nerve-racking. - El Forum - 08-02-2007

[eluser]Morty[/eluser]
I had to send an imploded version of my checkboxes then exploded via the PHP script.

JS :

Code:
function inline_results() {
    $filterstring = '';

    // alert(Form.serialize($('filter[]')));

    res = document.getElementsByName('filter[]')
    $filterstring = '';

    for (i = 0; i < res.length; i++)
        if (res[i].checked == true)
            if ($filterstring == '')
            {
                $filterstring += res[i].value;
            }
            else
            {
                $filterstring += '|'+res[i].value;
            }

    new Ajax.Updater ('stock_values', base_url+'stocks/ajaxstocksearch', {method:'post', postBody:'stock_site='+$F('stock_site')+'&stock;_product='+$F('stock_product')+'&filter;='+$filterstring});

With a simple :

Code:
$filter = explode("|", $filter);

to give back the array. I am not quite satisfied with the result, because an empty array/string is not really a FALSE boolean, but it will save me from drowning in despair.