0

I'm trying to pass array from ajax to php (controller).

What is wrong with second code as var_dump($data) of first code returns appropriate content and second returns NULL?

FIRST. GOOD.

function myFunction() {
    var elementy = document.getElementsByClassName('inputISBN');
    var data = elementy[0].value;

    $.ajax({
           url: "{{ path('test') }}",
           type: "POST",
           data: { "data": data }

    });   

}

SECOND. BAD

function myFunction() {
    var elementy = document.getElementsByClassName('inputISBN');
    var data = [];    
    data[elementy[0].name] = elementy[0].value;

    $.ajax({
           url: "{{ path('test') }}",
           type: "POST",
           data: { "data": data }
    });   
}

THIRD. UGLY

var elementy = document.getElementsByClassName('inputISBN');
undefined
var data = []; 
undefined
data[elementy[0].name] = elementy[0].value;
"667"

Third one is line by line from the socond code written in browser console. And it's return what it should.


edit

and data is pulled out from here:

<input type="number" class="inputISBN" size="2" name="exampleName" 
                value="666" onchange="myFunction()">

2 Answers 2

2

When passing an array to PHP, you want to include the array indicator: []. I thi8nk you need an Object: {}.

function myFunction() {
    var elementy = $('.inputISBN');
    var data = {};
    $.each(elementy, function(){
      data[$(this).attr('name')] = $(this).val();
    })

    $.ajax({
           url: "{{ path('test') }}",
           type: "POST",
           data: { "data": data }
    });   
}

At this point, you may also want to serialize the data (as was mentioned in the other answer by @Adelphia):

'data': JSON.stringify(data)

jsFiddle: https://jsfiddle.net/Twisty/cw77ann7/

You can call it in PHP: print_r($_POST['data']);

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

10 Comments

What difference would that make for PHP?
Depends... it's not clear from OPs code what he's trying to do. But that is how you pass data to be part of an array. And that's what he asked.
@Twisty elementy[0].name are text type data allways, and elementy[0].value are number. Your hint with array indicator does not work. Still NULL.
Added an update/edit. Let me know if this is more of what you're looking to do.
@Twisty if data is an array in Javascript, it will be an array in PHP when it's sent via AJAX - you don't need the add [] to make that happen
|
1

You want to pass your data variable to PHP, which is an array, right? Why not data = JSON.stringify(data); and then on PHP's side, $data = json_decode($_POST['data'], true);

function myFunction() {
    var elementy = document.getElementsByClassName('inputISBN');
    i = elementy.length;
    data = [];
    while(i--) data[elementy[i].name] = elementy[i].value;
    data = JSON.stringify(data);

    $.ajax({
           url: "{{ path('test') }}",
           type: "POST",
           data: { "data": data }

    });   

}

8 Comments

should I just paste your linedata = JSON.stringify(data); after line data[elementy[0].name] = elementy[0].value; (from my SECOND code) ? If yes then var_dump return array (size=1) 0 => string '[]' (length=2) instead of something like['name']=>'667'
@Sruj make a jsfiddle and let me see what you're actually doing, I'll fix it up for you.
@AdeIphia sorry for waisting your time, i don't belive it might help you jsfiddle.net/f3oLsu9m/1 .
@Sruj - There is no array in your fiddle. I still don't understand what you're doing. I'd like to help, I just don't get it.
@AdeIphia its test version to simplify my real version. I can do it again.
|

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.