user1692333
user1692333

Reputation: 2597

How to pass values of html to array in mootools?

I have simple line witch returns me error

Window.addEvent('domready', function(){
    function sendPost(){
        var values = $('input[name="database[]"]'​​​​​​​​​​​​​​).map(function(e) { return e.value; });
    // var myRequest = new Request({
    //  url: '<?php echo JURI::root();?>administrator/index.php?option=com_component',
    //  method: 'post',
    //  data: values

    // });

    // myRequest.send();
}
});

And here is error. error p.s. My script is after mootols.

Upvotes: 0

Views: 229

Answers (4)

Nils
Nils

Reputation: 2061

The code bellow will post it and in PHP you access it with $_REQUEST['somename']

Window.addEvent('domready', function(){
function sendPost(){
    var values = $('input[name="database[]"]'​​​​​​​​​​​​​​).map(function(e) { return e.value; });
    new Request({
        url: '<?php echo JURI::root();?>administrator/index.php?option=com_component',
        method: 'post',
        data: {
            'somename': values
        }

    }).send();
}
});

However, it it's a form you would like to post you can do it in Mootools with Form.Request, see http://mootools.net/docs/more/Forms/Form.Request for more information.

If you add some more information I could probably help you a bit more when it comes to Mootools (not Joomla). For instance, you are not doing anything with the data coming back from the server.

Edit: There is another way to get the form data as well:

$('theForm').toQueryString().parseQueryString();

So you can use it as:

Window.addEvent('domready', function(){
function sendPost(){
    new Request({
        url: '<?php echo JURI::root();?>administrator/index.php?option=com_component',
        method: 'post',
        data: $('theForm').toQueryString().parseQueryString();
    }).send();
}
});

Edit #2: You are aware that in your code in the example you do not call the function sendPost? so it actually don't do anything and does not have to be attached to the domready event.

Upvotes: 0

Carlos
Carlos

Reputation: 5072

var database = ($$('input[name="database[]"]').map(
    function (element) {
        return 'database[]=' + element.get('value');
    }
)).join('&');

This produces a string ready to be used for passing data in a HTTP request:

database[]=<value-0>&database[]=<value-1>&database[]=<value-N>

Easy as pie.

Upvotes: 0

dan-lee
dan-lee

Reputation: 14502

Use the double dollar sign for this:

$$('input[name="database[]"]')

on jsFiddle

Upvotes: 1

Sibu
Sibu

Reputation: 4617

Change

var values = $('input[name="database[]"]'​​​​​​​​​​​​​​).map(function(e) { return e.value; });

to

 var values = $('input[name=database\[\]]'​​​​​​​​​​​​​​).map(function(e) { return e.value; });//escape array operator

Upvotes: 0

Related Questions