Welcome Guest, Not a member yet? Register   Sign In
Javascript array to codeigniter controller
#1

[eluser]bennyhill[/eluser]
How would I pass a javascript array to a codeigniter controller function on a link click?
#2

[eluser]Bramme[/eluser]
There's several ways, one of the easiest being an Ajax call I think. Or if you really want to change the page's location, you could write the array to a hidden input field in a form and submit that form with the link.

Would would you exactly want to do?
#3

[eluser]bennyhill[/eluser]
i want the user to be able to select ten names from a list and put those names into another list. Then insert that list of 10 names into the database.

Note:
I still haven't figured out how to get the name/values from one list to another with javascript because I suck at javascript. I wish I could use flash!!!
#4

[eluser]Nick Husher[/eluser]
I think you're overthinking the problem: why not have a list of names with checkboxes next to them? Have a listener count the number of checkboxes that are checked, when the count reaches 10, disable all the unchecked boxes and display a message explaining why they're disabled. Then you can pass the checkbox values in through a simple HTML form.

Here's an example from a project that was never greenlit from eight months or so ago: Candidate Tracker. There's an element that tracks the number of checkboxes checked (maximum 4) and grays out everything after the fourth is selected. Then it's possible to submit that data through POST and parse as you will.
#5

[eluser]narkaT[/eluser]
php provides a nice feature to submit arrays:

assuming that we have this form:
Code:
<form ...>
    <input type="hidden" name="users[]" value="1"
    <input type="hidden" name="users[]" value="4"
    <input type="hidden" name="users[]" value="14"
    <input type="hidden" name="users[]" value="214"
    <input type="hidden" name="users[]" value="21"
</form>

you could acces your values as an simple array:
Quote:$users = $this->input->post('users');
foreach($users as $user) {
//...
}

that should do the job Wink
#6

[eluser]johnwbaxter[/eluser]
@narkaT - I don't think that is what he's talking about. That is not a js array.
#7

[eluser]xwero[/eluser]
[quote author="bennyhill" date="1224186753"]
Note:
I still haven't figured out how to get the name/values from one list to another with javascript because I suck at javascript. I wish I could use flash!!![/quote]
In jquery you can use the serialize method to get the values.
#8

[eluser]narkaT[/eluser]
[quote author="audiopleb" date="1224248357"]@narkaT - I don't think that is what he's talking about. That is not a js array.[/quote]
it could represent one:
Code:
var users = [1,2,3,45,125,187];
jQuery.each(users, function(k, v) {
    jQuery('#myForm').append('<input type="hidden" name="users[]" value="'+ v +'" />');
});

Smile

if a more complex array-structure is given he could also use JSON
and submit the JSON-string directly.
but for a simple list of users... Wink
#9

[eluser]Nick Husher[/eluser]
[quote author="audiopleb" date="1224248357"]@narkaT - I don't think that is what he's talking about. That is not a js array.[/quote]

Yes, but Javascript and PHP are not the same language. You have to step into a lingua franca to get from one to the other. He and I are suggesting submitting the data to the server via form-based POST. You could pass a POST with a JSON object in it as well, but that seems needlessly complex.

Generally you can't pass a data structure in raw format between operating environments, which is why JSON and XML are so valuable--they can be handed around in a language-ignorant way. On the other hand, actually making use of those technologies in a maintainable way can be painful.
#10

[eluser]bennyhill[/eluser]
@Nick Husher - I have a list of almost 300 attendees, do you think your option is still viable?

I found some code online and adapted it and it seems to work the way I want. Not exactly sure why though.

Code:
[removed]
function moveOptions(theSelFrom, theSelTo)
{
    var tableMax = 10;
    var tableCount = 0;
    var table = document.getElementById('sel4');
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
    
    var i;
    
    if(theSelTo.id == 'sel4' && tableCount >= tableMax)
    {
        alert('Table only seats ' + tableMax);
    }else{
        // Find the selected Options in reverse order
        // and delete them from the 'from' Select.
        for(i=selLength-1; i>=0; i--)
        {
            if(theSelFrom.options[i].selected)
            {
                selectedText[selectedCount] = theSelFrom.options[i].text;
                selectedValues[selectedCount] = theSelFrom.options[i].value;
                deleteOption(theSelFrom, i);
                selectedCount++;
            }
        }
        
        // Add the selected text/values in reverse order.
        // This will add the Options to the 'to' Select
        // in the same order as they were in the 'from' Select.
        for(i=selectedCount-1; i>=0; i--)
        {
            addOption(theSelTo, selectedText[i], selectedValues[i]);
        }
            
        if(NS4) history.go(0);

    }
}

function selectAllOptions(selStr)
{
  var selObj = document.getElementById(selStr);
  for (var i=0; i<selObj.options.length; i++) {
    selObj.options[i].selected = true;
  }
}
[removed]
<div id="tableNames">

<table border="0">
    <tr>
        <td>
            <h3>Current Attendee List</h3>
            <select id="sel3" size="20">
                &lt;?php
                    foreach($attendee->result() as $row)
                    {
                        echo '<option value="'.$row->name;
                        if($row->guest){
                            echo ','.$row->guest.'">'.$row->name.' & '.$row->guest.'</option>';
                        }else{
                            echo '">'.$row->name.'</option>';
                        }
                    }
                ?&gt;
            </select>
        </td>
        <td align="center" valign="middle" id="centerButtons">
            &lt;form&gt;
            &lt;input type="button" value="--&gt;"
            &gt;&lt;br />
            &lt;input type="button" value="&lt;--"
            &gt;
            &lt;/form&gt;
        </td>
        <td>
            <h3>Your Table</h3>
            &lt;form action="&lt;?php echo $base.'registration/submitTable'; ?&gt;" method="post"&gt;
            <select name="selPHP[]" id="sel4" multiple="multiple" size="10" />
            <br />
            &lt;input type="submit" value="Submit" /&gt;
            &lt;/form&gt;
        </td>
    </tr>
</table>




Theme © iAndrew 2016 - Forum software by © MyBB