Solved

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

Posted on 2015-01-19
6
107 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

730 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