Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populating model dropdown
05-28-2012, 12:24 PM
Post: #1
Populating model dropdown
Quote:I have found a working script to populate the model dropdown from the make selection in my CMS. However, I don't want to re-invent the wheel! Does AdminPack already have this facility, perhaps? I've not tracked it down in the documentation, but that doesn't mean that you haven't thought of, or perhaps implemented it...

JiminSA - Got the above message, can't PM on the other site yet (not enough reputation).

When I need to do custom stuff, I use a little jQuery and inject it into 'htmldesc'. In your case, you probably want to run a AJAX query against the server when onchange/onkeyup happens on the 'Make' dropdown and fill out the 'Model' dropdown. I'd do something like:

PHP Code:
<?php
  ob_start
();
?>
function GetModels()
{
  $('input[type=model]').load('index.php', {
    'action' : 'getmodels',
    'make' : $('input[type=make]').val(),
    'sec_t' : <?php echo BB_CreateSecurityToken("getmodels"); ?>
  });
}

$(function() {
  $('input[type=make]').change(GetModels()).keyup(GetModels());
});
<?php
  $custom 
ob_get_contents();
  
ob_end_clean(); 

That loads up a variable named 'custom' with the Javascript content. That gets used for 'htmldesc' in $contentopts (or appended to a value).

The 'action' handler should output HTML 'option' tags and values so the 'select' field gets filled out properly.

Author of Barebones CMS

If you found my reply to be helpful, be sure to donate!
All funding goes toward future product development.
Find all posts by this user
Quote this message in a reply
05-29-2012, 06:19 AM (This post was last modified: 05-29-2012 07:00 AM by JiminSA.)
Post: #2
RE: Populating model dropdown
Thanks thruska, for such a quick response! This jQuery function I'm using, uses the form-field id values to identify field 1 (to select) and then field 2 (to populate) and I adjusted them to reflect the generated make and model form ids, accordingly (found by inspecting source). It the grabs a .txt file according to the first element selected and uses it to populate the second. viz:-
<script>
$(function()
{
$("#f1_make").change(function() {
$("#f2_model").load("textdata/" + $(this).val() + ".txt");
});
});
</script>
How will that scenario fit into the injected code?

I have altered the original ob_ injection slightly to (hopefully) incorporate the .txt file grab. viz:-

ob_start();
?>
function GetModels()
{
$('input[type=model]').load('index.php', {
'action' : 'getmodels',
'make' : $('input[type=make]').val(),
'sec_t' : <?php echo BB_CreateSecurityToken("getmodels"); ?>
});
}

$(function() {
$('input[type=make]').change(function() {
$('input[type=model]').load("textdata/" + $(this).val() + ".txt");
});
});
<?php
But she's not grafting??
P.S.
If you care to check, I have uploaded the amendments to the site and if you check bottom-left there's a working version outside of page generate...
P.P.S. I'm using xampp now - luverly!

If you don't squeak, you won't get oiled!Cool
Find all posts by this user
Quote this message in a reply
05-29-2012, 10:05 AM
Post: #3
RE: Populating model dropdown
You really do want to handle keyup events on the 'make' field. This is because if someone uses the keyboard to type the name or the arrow keys, the second field won't change until the 'make' field loses focus.

You should use the code I wrote as-is. In a separate 'action' in the PHP code called 'getmodels', load and output the correct HTML. You can load the data from whatever resource a lot more easily that way. If you want to use text files as a source (I personally disagree with the approach and would use a database, but whatever), just have the models listed without wrapping them with HTML 'option' tags - let the PHP code do that.

Personally, I think a freeform field for models would be easier for the end-user because otherwise you have to enter every make and model ever made into the system and then someone has to manually update the list whenever new makes/models come into existence. Freeform fields do have the downside of entering data inconsistently. But you have to ask how many entries you are going to display at a time and for how long? It takes a lot longer to hunt for an entry in a long list than it does to type it out even incorrectly. Based on what I've seen, your target audience is someone who is moving their product quickly. If a mistake is made in the listing, no one will care because it will be gone in a few days.

Author of Barebones CMS

If you found my reply to be helpful, be sure to donate!
All funding goes toward future product development.
Find all posts by this user
Quote this message in a reply
05-29-2012, 11:11 AM
Post: #4
RE: Populating model dropdown
You're right about model free form of course - I shall put it to my client and see what he says. Should he decide to go pro-forma I will adopt a get text file in the getmodels script. But at least I now understand the methodology exactly. Many thanks...

If you don't squeak, you won't get oiled!Cool
Find all posts by this user
Quote this message in a reply
05-30-2012, 12:23 AM
Post: #5
RE: Populating model dropdown
I did think that I had a handle on this but alas, my education is incomplete! How does the 'action' get actioned? By a call to a php function called 'getmodels' or an include to php program called getmodels.php, or have I lost the plot completely?Huh

If you don't squeak, you won't get oiled!Cool
Find all posts by this user
Quote this message in a reply
05-30-2012, 06:54 AM
Post: #6
RE: Populating model dropdown
The main PHP file has bunch of lines like this:

PHP Code:
if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "something")
{
...
}
else if (isset(
$_REQUEST["action"]) && $_REQUEST["action"] == "somethingelse")
{
...


Those are the 'action' handlers. Add another one to handle 'getmodels'. Process the data however you want (e.g. loading the file). Since this will be loaded into a select box already on the page, you just need to output HTML 'option' tags and values.

PHP Code:
...
else if (isset(
$_REQUEST["action"]) && $_REQUEST["action"] == "getmodels")
{
  
$fp = @fopen("models/" preg_replace('/[^a-z]/'""strtolower($_REQUEST["make"])) . ".txt""rb");
  if (
$fp !== false)
  {
    while ((
$line fgets($fp)) !== false)
    {
      
$line trim($line);
      if (
$line != "")  echo "<option value=\"" htmlspecialchars($line) . "\">" htmlspecialchars($line) . "</option>";
    }
    
fclose($fp);
  }
}
... 

Something like that.

Author of Barebones CMS

If you found my reply to be helpful, be sure to donate!
All funding goes toward future product development.
Find all posts by this user
Quote this message in a reply
06-01-2012, 03:56 AM
Post: #7
RE: Populating model dropdown
I'm assuming that the action handlers only operate after posting? Which would make any dynamic populating redundant, wouldn't it?

If you don't squeak, you won't get oiled!Cool
Find all posts by this user
Quote this message in a reply
06-01-2012, 06:15 AM
Post: #8
RE: Populating model dropdown
I'm not sure I understand. You let the server handle populating data fields when doing AJAX. That's how AJAX is supposed to be used.

Author of Barebones CMS

If you found my reply to be helpful, be sure to donate!
All funding goes toward future product development.
Find all posts by this user
Quote this message in a reply
06-04-2012, 02:54 AM
Post: #9
RE: Populating model dropdown
Of course! Please forgive my fogginess!Huh

If you don't squeak, you won't get oiled!Cool
Find all posts by this user
Quote this message in a reply
06-05-2012, 06:03 AM
Post: #10
RE: Populating model dropdown
I just realized that my code doesn't initially populate the models dropdown. If you want to do that, call GetModels() inside the jQuery 'onready' event handler - that is, inside $(function() { ... });

Author of Barebones CMS

If you found my reply to be helpful, be sure to donate!
All funding goes toward future product development.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

© CubicleSoft