0

In HTML I have:

<input name="feature[1]" value="1" type="hidden">
<input name="feature[2]" value="1" type="hidden">
<input name="feature[3]" value="1" type="hidden">

And in Javascript I have:

features = $.map($('#add-place [name^="feature"]'), function(item, index)
{
    if($(item).val()=='1')
    {
        return $(item).attr('name').replace(/[^0-9]/g, '');
    }
});

When I console.log(features) it returns an array but when I send it through ajax with new FormData in PHP I will have an string containing the numbers in the features array so if they are [1, 3] I will get 1,3 as string in PHP.

What causes this?

EDIT

form = new FormData();
features = $.map($('[name^="feature"]'), function(item, index){if($(item).val()=='1'){return $(item).attr('name').replace(/[^0-9]/g, '');}});
form.append('features', features);
$.ajax({
    url: url,
    type: 'POST',
    dataType: 'json',
    enctype: 'multipart/form-data',
    cache: false,
    processData: false,
    contentType: false,
    data: form,
    complete: function(response)
    {
        //
    }
});
kodfire
  • 1,612
  • 3
  • 18
  • 57
  • The map() method is creating an array, are you also creating an array in PHP separate to this? – Matadeleo Jun 26 '18 at 15:44
  • I just wanted to go through it with `foreach` loop however it returned an error that it's not an array. When I `print_r` it, it returned an string `1, 3` – kodfire Jun 26 '18 at 15:46
  • Before this it was working but now when I used `new FormData` it destroyed!! – kodfire Jun 26 '18 at 15:47
  • Why don't you create an array using the `$_POST` results? Something like this: http://sandbox.onlinephpfunctions.com/code/2d4e4da860c3b32d13999d0e459463971d48cb4c – Matadeleo Jun 26 '18 at 16:01
  • Can you show formdata code? – Vinay Jun 26 '18 at 16:58
  • Can you show formdata code? – Vinay Jun 26 '18 at 16:59
  • Maybe [this](https://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php) old SO question can help you – Jeroen Heier Jun 26 '18 at 17:48
  • _“What causes this?”_ - your attempt at stuffing an array into your formdata … the append method works with either a string or a blob for the value parameter. You supplied an array, so that has to be converted to a string first, and the default toString implementation for an array is simply that of returning the elements joined together with commas. – CBroe Jun 27 '18 at 08:44
  • And how can I send it as array? @CBroe – kodfire Jun 27 '18 at 09:26
  • Your question is missing a proper description of what you actually want to _achieve_ here to begin with, so please go and add that first. For example what the JS mapping and replacing part is for - unclear. Might not even be necessary to use append, if you just passed the form element reference to the FormData constructor. – CBroe Jun 27 '18 at 09:30
  • When I send the features it is string in PHP I want it as an array. Is it possible? – kodfire Jun 27 '18 at 09:31

1 Answers1

0

Try to set processData to false on ajax query:

$.ajax({
    url: '',
    data: formData,
    processData: false
});
arczinosek
  • 139
  • 5