close ad
Install the LAtest Updates to Work with CC 2017 and CC 2018
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Having the child feed the parent - help with Dynamic Dropdowns

Thread began 9/17/2015 11:50 am by Nathon Jones Web Design | Last modified 9/30/2015 1:51 pm by Ray Borduin | 1416 views | 12 replies |

Nathon Jones Web Design

Having the child feed the parent - help with Dynamic Dropdowns

We have a list of regions in one recordset and a list of countries in another.

Regions
regionID
regionname
countryID

Countries
countryID
countryname

We want users to be able to select a region and have it auto-complete the country menu, below it, depending on their selection.

Is this possible with WebAssist Dynamic Dropdowns? I've tried following the tutorial but I just can't get the country menu to change once the user has selected the region.

Oh, and I assume this doesn't yet work with MySQLi recordsets?

Thank you.
NJ

Sign in to reply to this post

Ray BorduinWebAssist

Would you still want them to be able to change the Country dropdown? Why show a list if it is already selected with the correct value?

Why not just show the country associated to that region in plain text in a div next to it? I don't think you need dynamic dropdowns in this case, since you are only going to have one option available for each selection.

I think you can probably add the country name to the original region list into another attribute like:

<option value="<?php echo($rs->getColumnVal['RegionID']); ?>" rel="<?php echo($rs->getColumnVal['Country']); ?>">"<?php echo($rs->getColumnVal['RegionName']); ?>"</option>

If you want them to be able to change the country and just have it pre-selected, then use the CountryID column in the rel attribute, if you only want it displayed and not changed, then I'd do an inner join in your Region recordset and put the country name in the rel attribute so you can populate the innerhtml of a span tag next to it.

Then you can add a simple javascript onchange of the region select list that will take the selected option and retrieve the associated rel attribute and use it to either display the country name or set the selection in the other list.

Sign in to reply to this post

Nathon Jones Web Design

Originally Said By: Ray Borduin
  Would you still want them to be able to change the Country dropdown? Why show a list if it is already selected with the correct value?  



D'oh!

I need the country name displayed, just for reassurance for the client, but it doesn't need a list/menu as you have so correctly highlighted. Thing is, I need to save the country ID to database as well as the region ID.


Originally Said By: Ray Borduin
  I think you can probably add the country name to the original region list into another attribute like:

<option value="<?php echo($rs->getColumnVal['RegionID']); ?>" rel="<?php echo($rs->getColumnVal['Country']); ?>">"<?php echo($rs->getColumnVal['RegionName']); ?>"</option>

If you want them to be able to change the country and just have it pre-selected, then use the CountryID column in the rel attribute, if you only want it displayed and not changed, then I'd do an inner join in your Region recordset and put the country name in the rel attribute so you can populate the innerhtml of a span tag next to it.

Then you can add a simple javascript onchange of the region select list that will take the selected option and retrieve the associated rel attribute and use it to either display the country name or set the selection in the other list.  



I think the best option is not to allow them to change the country. They've selected the region, which is more than sufficient info to populate the country information, and with any luck the countryID into a hidden field, thus saving them a bit of time.

Sorry, I'm not familiar with the rel attribute and searching Google I can only find information about using rel with an a href tag.

This is my regions drop down, which is taken from my regions recordset within which I've done an inner join so that I can drag the country name across in one recordset.

<select id="regionID" name="regionID" class="form-control">
<?php
while(!$rsREGIONS->atEnd()) {
?>
<option value="<?php echo($rsREGIONS->getColumnVal("regionID")); ?>" rel="<?php echo($rsREGIONS->getColumnVal['country']); ?>"><?php echo($rsREGIONS->getColumnVal("region")); ?></option>
<?php
$rsREGIONS->moveNext();
}
$rsREGIONS->moveFirst(); //return RS to first record
?>
</select>



Below this, I have set up a readonly input field because I'd still like the country to be shown in a form format/layout. I have included a hidden field for the countryID though.

<input type="text" class="form-control" id="country" name="country" placeholder="Country..." readonly>
<input type="hidden" id="countryID" name="countryID" value="">



How do I use the rel attribute to populate the country, readonly, input field and, at the same time, populate the countryID hidden field with the unique country ID?

You mentioned "...populate the innerhtml of a span tag next to it." but don't we have to wait on the region selection by the user?

I understand I'll need an onchange event for the region select:
<select id="regionID" name="regionID" class="form-control" onchange="myFunction()">

I just don't have the knowledge to write the javascript function. I assume the function populates the country and countryID fields "onchange" of the region drop down.

Would appreciate any advice you could offer. Thank you Ray.
NJ

Sign in to reply to this post

Ray BorduinWebAssist

rel is just a random attribute... it could be anything. It sounds like you would want both the country name and ID in the option so that you could populate a hidden form element with the ID and the innerhtml of a span with the name.

So the code might look like:

php:
<script>

function getCountryData(list) {
  var listOption = list.options[list.selectedIndex];
  var countryID = listOption.getAttribute("data-country-id");
  var countryName = listOption.getAttribute("data-country");
 document.getElementById("countryID").value = countryID;
 document.getElementById("showCountry").innerHTML= countryID;
}
</script>

<select id="regionID" name="regionID" class="form-control" onchange="getCountryData(this)">
<?php
while(!$rsREGIONS->atEnd()) {
?>
<option value="<?php echo($rsREGIONS->getColumnVal("regionID")); ?>" data-country="<?php echo($rsREGIONS->getColumnVal('country')); ?>" data-country-id="<?php echo($rsREGIONS->getColumnVal('country')); ?>"><?php echo($rsREGIONS->getColumnVal("region")); ?></option>
<?php
  $rsREGIONS
->moveNext();
}
$rsREGIONS->moveFirst(); //return RS to first record
?>
</select>
<input type="hidden" id="countryID" name="countryID" value="">
<span id="showCountry"></span>
Sign in to reply to this post

Nathon Jones Web Design

I've changed that to this as I think I spotted some errors:

php:

<script>
function getCountryData(list) {
var listOption = list.options[list.selectedIndex];
var countryID = listOption.getAttribute("data-country-id");
var countryName = listOption.getAttribute("data-country");
document.getElementById("countryID").value = countryID;
document.getElementById("showCountry").innerHTML = countryName;
}
</script>

<select id="regionID" name="regionID" class="form-control" onchange="getCountryData(this)">
<?php
while(!$rsREGIONS->atEnd()) {
?>
<option value="<?php echo($rsREGIONS->getColumnVal("regionID")); ?>" data-country="<?php echo($rsREGIONS->getColumnVal['country']); ?>" data-country-id="<?php echo($rsREGIONS->getColumnVal['countryID']); ?>"><?php echo($rsREGIONS->getColumnVal("region")); ?></option>
<?php
$rsREGIONS->moveNext();
}
$rsREGIONS->moveFirst(); //return RS to first record
?>
</select>
<input type="hidden" id="countryID" name="countryID" value="">
<span id="showCountry"></span>



That isn't populating the hidden field or displaying the country name though?

Sign in to reply to this post

Ray BorduinWebAssist

Do you have a url where I can view the page where you have tried?

Sign in to reply to this post

Nathon Jones Web Design

Below, thank you.

Here's what I have on that page:

<script>
function getCountryData(list) {
var listOption = list.options[list.selectedIndex];
var countryID = listOption.getAttribute("data-country-id");
var countryName = listOption.getAttribute("data-country");
document.getElementById("tmsacountryID").value = countryID;
document.getElementById("showCountry").innerHTML = countryName;
}
</script>

<div class="form-group">
<label for="tmsaregionID" class="col-sm-2 control-label">Region:</label>
<div class="col-sm-6">
<select id="tmsaregionID" name="tmsaregionID" class="form-control" onchange="getCountryData(this)">
<option value="0">Please select</option>
<?php
while(!$rsREGIONS->atEnd()) {
?>
<option value="<?php echo($rsREGIONS->getColumnVal("tmsaregionID")); ?>" data-country-id="<?php echo($rsREGIONS->getColumnVal['tmsacountryID']); ?>" data-country="<?php echo($rsREGIONS->getColumnVal['tmsacountry']); ?>"><?php echo($rsREGIONS->getColumnVal("tmsaregion")); ?></option>
<?php
$rsREGIONS->moveNext();
}
$rsREGIONS->moveFirst(); //return RS to first record
?>
</select>
</div>
</div><!-- end form-group -->

<div class="form-group">
<label for="tmsacountryID" class="col-sm-2 control-label">Country:</label>
<div class="col-sm-6">
<span id="showCountry"></span>
<input type="text" class="form-control" id="tmsacountry" name="tmsacountry" readonly>
<input type="text" id="tmsacountryID" name="tmsacountryID" value="">
</div>
</div><!-- end form-group -->



I changed this in your example:
document.getElementById("countryID").value = countryID;

To this...
document.getElementById("tmsacountryID").value = countryID;

..as I felt that must match the id of the form field. I've un-hidden them as well just so I can check they're working correctly.
Also, whilst I understand that <span id="showCountry"></span> will (should) display the country name, how do I get that value into my readonly form field?

Really appreciate the help, thank you. Was messing about with dynamic dropdowns for aaages before you pointed out the obvious!
NJ

Sign in to reply to this post

Ray BorduinWebAssist

It looks like the data isn't getting added to the options in the select list to begin with. Are you sure the columns tmsacountryID and tmsacountry are in the rsREGIONS recordset? Maybe you have to update your select statement to make sure those columns are available.

Sign in to reply to this post

Nathon Jones Web Design

<?php
$rsREGIONS = new WA_MySQLi_RS("rsREGIONS",$csdbmysqli,0);
$rsREGIONS->setQuery("SELECT TMSAregions.tmsaregionID, TMSAregions.tmsaregion, TMSAregions.tmsamapsectionID, TMSAregions.tmsacountryID, TMSAcountry.tmsacountry FROM TMSAregions INNER JOIN TMSAcountry ON TMSAregions.tmsacountryID = TMSAcountry.tmsacountryID ORDER BY TMSAregions.tmsacountryID ASC, TMSAregions.tmsaregion ASC");
$rsREGIONS->execute();
?>



When I test that recordset in DW it displays the countryID and the country.

Ray, I just spotted single quotes around those values:

Originally Said By: Ray Borduin
  
php:
<option value="<?php echo($rsREGIONS->getColumnVal("regionID")); ?>" data-country="<?php echo($rsREGIONS->getColumnVal('country')); ?>" data-country-id="<?php echo($rsREGIONS->getColumnVal('country')); ?>"><?php echo($rsREGIONS->getColumnVal("region")); ?></option>
  



Should have been:
<option value="<?php echo($rsREGIONS->getColumnVal("regionID")); ?>" data-country="<?php echo($rsREGIONS->getColumnVal("tmsacountry")); ?>" data-country-id="<?php echo($rsREGIONS->getColumnVal("tmsacountryID")); ?>"><?php echo($rsREGIONS->getColumnVal("region")); ?></option>

That will teach me to cut and paste!
How do I get the <span id="showCountry"></span> value into the value of a form field?

Sign in to reply to this post

Ray BorduinWebAssist

Odd... when I view source on the page I see the select element code:

<select id="tmsaregionID" name="tmsaregionID" class="form-control" onchange="getCountryData(this)">
<option value="0">Please select</option>
<option value="3" data-country-id="" data-country="">Aberdeenshire</option>
<option value="4" data-country-id="" data-country="">Angus</option>



You can see the country and country id are blank from the recordset. Is it possible the data isn't entered into the regions table properly? Do you see those field values when you test the recordset?

Maybe send me a copy of the page so I can take a look.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...