Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - set array values to the input with the same name

I was trying to set the array values to the input field named attachments[]

I have array stored in js variable attachArray

What I have tried is,

$('[name="attachments"]').attr('value', attachArray);

or

$('[name="attachments"]').val(attachArray);

But getting empty attachments in the controller like this,

array(1) { ["attachments"]=> array(1) { [0]=> string(0) "" } }

What I'm doing wrong?

EDIT

<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; 
             <input type="hidden" name="attachments[]">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>
like image 438
Keyur Avatar asked Sep 13 '17 10:09

Keyur


People also ask

How do you put an input into an array?

But we can take array input by using the method of the Scanner class. To take input of an array, we must ask the user about the length of the array. After that, we use a Java for loop to take the input from the user and the same for loop is also used for retrieving the elements from the array.

What does val() return jQuery?

val() returns an array containing the value of each selected option. As of jQuery 3.0, if no options are selected, it returns an empty array; prior to jQuery 3.0, it returns null .

What does val() do?

The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.


2 Answers

As far as I understand is that you want to spread the content of an JavaScript array to multiple fields that PHP on the other hand interprets as an array.

I changed the inputs from hidden to text just to make it a little bit more clear and so you can see how the values do look like. Don't forget to undo this in your code.

const attachArray = [
  'val1',
  'val2',
];
const attachments = $('[name="attachments[]"]');
for ( let i = 0; i < attachments.length; i += 1 ) {
  $( attachments[ i ] ).val( attachArray[ i ] );
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; <br>
             <input type="text" name="attachments[]"><br>
             <input type="text" name="attachments[]">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>

But this is a lot of code to do. I think it might be easier to send the array as JSON and use PHPs json_decode to convert it back into an array, like so:

const attachArray = [
  'val1',
  'val2',
];
$('[name="attachments"]').val( JSON.stringify( attachArray ) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-4">
    <div class="form-group ticket-align">
        <label>Attachment</label>
        <label class="btn btn-primary" data-toggle="modal" data-target="#t-attachment-modal">
                    Browse&hellip; <br>
             <input type="text" name="attachments">
         </label>
         <span id="fileList"></span>
         <span class="error" id="error-atachments" style='display: none;'></span>
    </div>
</div>

And do something like

$attachments = json_decode( $_POST[ 'attachments' ] );
like image 88
lumio Avatar answered Nov 10 '22 16:11

lumio


Since it's a hidden input, so

Either:-

$('[name=attachments]').val(attachArray); 

OR

$('input:hidden[name=attachments]').val(attachArray);

Will work.

Note:- Use the backslashes(Escape internal brackets with \\(no space)).

like image 21
Anant Kumar Singh Avatar answered Nov 10 '22 18:11

Anant Kumar Singh