Categories: php, jquery, ajax, laravel

Sending an array with Ajax to backend nests too many arrays

1 answer

I am building an e-mail sending interface, where you can add as many recipients as you want. In the form there's a button to add new text field pairs (name and e-mail input). the multiplying JQuery looks like that:

var wrapper = $(".input_fields_wrap"); //Fields wrapper var add_button = $(".add_field_button"); //Add button ID  var x = 0; //initial text box number $(add_button).click(function (e) { //on add input button click     e.preventDefault();         x++; //text box increment         $(wrapper).append(             '<div class="form-group row multiplied">\n' +             '    <label for="recipient_name_' + x + '" class="col-sm-1 col-form-label">Name</label>\n' +             '        <div class="recipient_name">\n' +             '            <input class="form-control" type="text" name="recipient_name" id="recipient_name_' + x + '">\n' +             '        </div>\n' +             '        <label for="recipient_email_' + x + '" class="col-sm-1 col-form-label">E-mail</label>\n' +             '        <div class="recipient_email">\n' +             '            <input class="form-control" type="text" name="recipient_email" id="recipient_email_' + x + '">\n' +             '        </div>                         <a href="#" class="ml-2 btn btn-danger remove_field">                             <i class="fas fa-trash" aria-hidden="true"></i>                         </a>                     </div>'         ); //add input box });  $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text     e.preventDefault();     $(this).parent('div').remove(); }); 

After form is submitted I send data to backend via ajax request. Request works fine, I send the multipled fields data like that:

data: {         sender_name : $("#sender_name").val(),         sender_email : $("#sender_email").val(),         subject : $("#subject").val(),         email_content: $("#email_content").val(),         recipient_name : $('input[name="recipient_name"]').serializeArray(),         recipient_email : $('input[name="recipient_email"]').serializeArray(),         }, 


I fire it, and it gets to the backend I could be happy it's working, but the recipient_name and recipient_email variables are arrays that contains both the value and name of the element. in the following form:

array:2 [   0 => array:2 [     "name" => "recipient_name"     "value" => "John Doe"   ]   1 => array:2 [     "name" => "recipient_name"     "value" => "Jane Doe"   ] ] 

But I rather want something like

array:2 [   0 => "John Doe"   1 => "Jane Doe" ] 

With other words:

Now I access my variables like that on the backend:


And I want it like


Unfortunely I'm just learning ajax calls, and I'm a bit a stuck with this issue, believing there's a better solution, only I can't find it. Thanks for any help in advance!

All answers to this question, which has the identifier 61209261

The best answer:

This is just how the data is 'unpacked', you can preprocess it in PHP if you want:

$myValues = array_map(function($val) { return $val['value']; }, request('recipient_name'); 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data