• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie: How do I send multiple arrays from my view to my controller?

#1
[eluser]John Morton[/eluser]
I think this should be an obvious thing, but I'm feeling lost.

I've got a page, sequence_view.php, that operates very much like the following example:
http://interface.eyecon.ro/demos/sort.html

If you click on the link that says "serialize all lists", a javascript alert window comes up with an array. In my example, I've got array that contains that information, plus an additional array of info. I can't seem to figure out how to get it to my controller though. Can you point me in the right direction?

#2
[eluser]John Morton[/eluser]
I think I'm slowing getting somewhere. Here's what I've got so far in my 'view' file, which seems to be sending data to my 'controller'.

... jquery library loaded, and the following code is in a tag

Code:
function serialize(s)
{
    myorder = $.SortSerialize(s);
    myhidden = $.SortSerialize('Inactive');
    myvisible = $.SortSerialize('Active');
    var myinfo=new Array()
    myinfo[0]=myorder;
    myinfo[1]=myhidden;
    myinfo[2]=myvisible;
    //alert(myinfo); // returns 3 objects correctly
    $.post('<?=base_url()?>index.php/admin/updateOrder', myinfo, foobar);

};
function foobar() {
    alert("works");
}

I'm getting the javascript alert to appear which tells me that I am contacting the function in my controller properly. I'm now trying to figure out how to actually 'see' my data on the controller side. I'm trying to use a $_POST variable there, but can't seem to get it to work yet. I'll keep plugging away at it. Any suggestions are appreciated.

#3
[eluser]Pascal Kriete[/eluser]
Ok, the first thing you want to do is create some sort of key => value pattern. The easiest way to do this in javascript is to use object notation, also known as a hash when used like this (in comparison, in php you would use an associatve array - js doesn't know those).

Code:
var myinfo = {
'order' : myorder,
'hidden' : myhidden,
'visible': myvisible
}

Now, if this works correctly and the serialization does what it's supposed to, you should get a post array with those three keys in your backend. A good check would be to print_r the $_POST. Now the problem is that you can echo 'til you're blue in the mouth, you won't see it because the request is run in the background. That's what the callback is for, so try this:
Code:
$.post('<?=base_url()?>index.php/admin/updateOrder', myinfo, function (transport) {
    // I don't know how jquery deals with this; it's one of the two:
    alert(transport);
    alert(transport.responseText);
});

#4
[eluser]John Morton[/eluser]
Thank you very much for the help! Here's where I ended up and it works.

Code:
function serialize(s)
{
    // I used .hash to turn return a text string which I explode into arrays in the model
        myorder = $.SortSerialize(s).hash;
    myhidden = $.SortSerialize('Inactive').hash;
    myvisible = $.SortSerialize('Active').hash;
    var myinfo=new Array()
    var myinfo = {
        'order' : myorder,
        'hidden' : myhidden,
        'visible': myvisible
    }
    $.post('<?=base_url()?>index.php/admin/updateOrder', myinfo, foobar);
};

function foobar() {
    alert("This works thanks to inparo.");
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.