Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
how to pass values from multiple textfields with same name attributes to a variable in form of an array

I am building a form that passes a set of numbers in form of an array to a variable as seen below

  var users=["1","2"];

the main purpose of this is to then make an Ajax request with these numbers and get their corresponding content in my database which I then pass to their respective divs, please see below

 var users=["1","2"];
   
var async_request=[];
var responses=[];
for(i in users)
{
    // you can push  any aysnc method handler
    async_request.push($.ajax({
        url:'back.php', // your url
        method:'post', // method GET or POST
        data:{user_name: users[i]},
        success: function(data){
            console.log('success of ajax response')
            responses.push(data);
          
        }
    }));
}


$.when.apply(null, async_request).done( function(){
    // all done
    console.log('all request completed')
    console.log(responses);
      $( '#responses' ).html(responses[1]);
      $( '#responses1' ).html(responses[0]);
       
});

This works perfectly.

But now I want to make some adjustments to my solution specifically

Im looking to replace the method of passing the numbers to the variable users

from

  var users=["1","2"];   // Im looking to replace the method

to this

var users = $('[name="tom[]"]').val(attachArray);

 <input type="text" name="tom[]" value="1" /><br>
        <input type="text" name="tom[]" value="2" /><br>

but I am unable to get the the ids from the two textfields and then pass to my database using my Ajax script as I did before with this

  var users=["1","2"]; 
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You mean

const arr = ["1", "2"]
$('[name^=tom]').val(function(i) {
  return arr[i]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="tom[]" value="" /><br>
<input type="text" name="tom[]" value="" /><br>

or

$('[name^=tom]').each(function() { 
  async_request.push($.ajax({        
    url:'back.php',
    method:'post', 
    data:{user_name: this.value }, 
about 4 years ago · Juan Pablo Isaza Report

0

You forgot to use input key value :

var users = $('input[name="tom[]"]').val();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!