0

I'm trying to loop through an un-ordered list with form fields in each. When I try to append it to formdata and process it through PHP it just returns

team: "[object Object],[object Object]"

JS is here:

var arr = [];
$( '#team_list li' ).each(function(idx, li) {
    $team_name = $( li ).find('input[name="team_name"]').val();
    $team_role = $( li ).find('input[name="team_role"]').val();
    $team_citizen = $( li ).find('#country').val();
    $team_status = $( li ).find('#id_team_status').val();
    arr.push({ name: $team_name, role: $team_role, citizen: $team_citizen, status: $team_status, });
});

fd.append('team', arr );
2
  • If you just want to display the array then try fd.append('team:', JSON.stringify(arr) ); otherwise you need elements to pass the values into. Say you have a div to display $team_name you would need to do something like this: <div class="team-name">'+arr.name+'</div> Commented Dec 11, 2019 at 22:07
  • 1
    Side note; just going off of your logic, it looks like your html is repeating the country and id_team_status ids. Ids are expected to be unique per document Commented Dec 11, 2019 at 22:10

1 Answer 1

1

The second argument to FormData.append needs to be a string or a file; anything that is not one of these is converted to a string (see the manual). So what you are seeing is the string representation of arr. What you need to do is to JSON encode arr (using JSON.stringify) i.e.

fd.append('team', JSON.stringify(arr));

And then in your PHP code you can write

$teams = json_decode($_POST['team']);
Sign up to request clarification or add additional context in comments.

Comments

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.