/    Sign up×
Community /Pin to ProfileBookmark

Populating dependent dropdown from MySql and using the result

I have adapted some Javascript code for an application, I need to have two dropdown selections, the second dropdown options being dependent on what the user selects in the first. I then need to take that option, and on an onclick event, use the information to perform a calculation.

Using a tutorial I found I have managed to get the dependent dropdowns working by fetching the info from a mysql db using jquery and php. However, I only seem to be able to get the ‘name’ column for the dropdown, I need the ‘price’ as well to perform the calculation. I can get the ‘price’ column, but it just adds it to the array and lists it as additional options.

This is my code:

[code=php]$area = $_GET[“area”];

$query = “SELECT proxies.name, proxies.price FROM proxies INNER JOIN areas ON proxies.area_id=areas.id WHERE areas.name LIKE ‘{$area}'”;
$data = mysqli_query($conn, $query);

$proxies = [];

while($row = mysqli_fetch_array($data)){
array_push($proxies, $row[“name”],$row[“price”]);
}

echo json_encode($proxies);[/code]

Could anybody help me please, or point me in the right direction? Also, I’m not experienced with Javascript, and this solution feels a little ‘hack-y’ to me, is there a better way of doing it? Thank you.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 16.2018 — How are you making the query to the server to get the data?

Are you using AJAX? When I say AJAX, I mean actual AJAX and not JQuery or is it via a web form submission like a search box?
Copy linkTweet thisAlerts:
@bevcauthorJan 17.2018 — How are you making the query to the server to get the data?

Are you using AJAX? When I say AJAX, I mean actual AJAX and not JQuery or is it via a web form submission like a search box?[/QUOTE]


I don't think I'm using AJAX, the JQuery file is included in the html, and my original use for this was as a web form that was submitted. My original inelegant solution was to use a mysql statement to look up the record that was selected when the form submitted, and then use that info.

I'm too much of a noob to know what's the best way to go about it - there are approx 200 records that need to be accessible, I don't know if having them as a MySql db is the best way to go, or to include them all somehow as Javascript objects (which would be fine, if that's a better solution).
×

Success!

Help @bevc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...