Solved

How can I use a Javascript function parameter to choose an array element?

Posted on 2015-01-19
6
108 Views
Last Modified: 2015-01-23
Hi, ‪‎Javascript‬ friends!
How can I use a function parameter to choose an array element?

Example:

function populateSelect(id_select, function_name, field) {
if ($('#'+id_select).size() == 1) {
$.post('php/db_functions.php', 'function='+function_name, function(data, textStatus) {
elements = data.items;
$.each(elements, function(index, elements) {
$('#'+id_select).append('<option value="' + elements.id + '">' + elements.field + '</option>'); // I would like to use in this row the field parameter
});
$('#'+id_select).trigger('change');
}, "json");
}
}

Open in new window

0
Comment
Question by:ltpitt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40558663
Change elements.field to:

field[index]
0
 
LVL 1

Author Comment

by:ltpitt
ID: 40558743
I don't get it...

Can you please be more explicit with code example?
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40558837
Maybe I misunderstood the question. You are passing the field parameter, an array according to your question, to the populateSelect() function. The comment in the $.each loop says you want to use the field parameter in the row. You had <option value="' + elements.id + '">' + elements.field + '</option>. You are using "field" as if it were a property of "elements". If field is an array then you must use an index to access the items inside. You have assigned the variable "index" to represent the index of the $.each loop. So field[index] will access the items in the field array. Caution: if elements.length is more than field.length, you are going to get an error.
function populateSelect(id_select, function_name, field) {
if ($('#'+id_select).size() == 1) {
$.post('php/db_functions.php', 'function='+function_name, function(data, textStatus) {
elements = data.items;
$.each(elements, function(index, elements) {
$('#'+id_select).append('<option value="' + elements.id + '">' +field[index] + '</option>'); // I would like to use in this row the field parameter
});
$('#'+id_select).trigger('change');
}, "json");
}
}

Open in new window

0
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
LVL 1

Author Comment

by:ltpitt
ID: 40563953
I'm sorry but I see I was unclear...

All I wanto to do is populate 3 select in my html page.

Each select's array comes from a different table from the database.

From the sake of learning and writing better code I'd like to write a single populateSelect function instead of writing 3 specific function (one for each select to populate).

For this reason the "field" parameter would be the column name i'd need to get from the db...

I hope I was clearer this time: sorry for wasting your time :(
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40564099
You can pass whatever data you need through the post using name:value pairs.

$.post('php/db_functions.php', {  function_name : field }, function(data, textStatus) {

    ...

)};

Then test for the names in the $_POST array on the php side. Let's say the function name passed is 'getData'. It's value will be the field name you want to pass to the php function.

<?php

if (isset($_POST['getData'])) {
       //call the actual getData() function
        getData($_POST['getData']);
}

function getData(field) {
}

?>
0
 
LVL 1

Author Closing Comment

by:ltpitt
ID: 40565960
oh so you'd pass right from the php just the right data I need.

So simple I feel stupid: thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question