Tom C. Tom C. - 3 months ago 6
MySQL Question

Dropdown menu populated from sql with url redirect

For the three dropdown menu I have the sql table as follows:

sql city table

I'm trying to use:

$(document).ready(function() {
$("#city_id").change(function(){
window.location=$("#city_url");
});


How do I acomplish my goal so when the city is selected it redirects to the corresponding url?

Answer

You have to add your URL in your option. It shoudl work if you put the url in the value but I suggest you to use a data-attributes

$("#city_id").append("<option data-url=\""+ value.city_url+"\" value=" + value.city_id +">"+value.city+"</option>"); 

it should render you something like

<option data-url="http://miami.com" value=1>Miami</option>

Then you can redirect to this attribute (according your select has a id="city_id")

$(document).ready(function() {
$("#city_id").change(function(){ 
window.location=$(this).data("url");
});

Code is not tested but you should understand the way to do it