TOPIC:

Two dependent dropdown populated from database 7 months 4 weeks ago #272192

  • Topic Author
  • webciudadreal
  • Offline
  • Senior Breezer
  • Senior Breezer
  • Posts: 80
  • Thanks: 1
Hi!

I have two "select from list" field, city and state.

State field is populated from database, that's ok an working using this (that's perfect!):

crosstec.org/en/support/online-documenta...ct-list-from-db.html

Now, my goal is when user select a state in field, city field populates from database with cities of this state.

I've seen this old post:

crosstec.org/en/forums/3-breezingforms-f...ties-by-country.html

That's ok, but values are typed directly in code. It's possible to get from database? Is there any example I could check?

Thanks!

Please Log in or Create an account to join the conversation.

Two dependent dropdown populated from database 7 months 4 weeks ago #272193

  • Mirec's Avatar
  • Mirec
  • Offline
  • Ultimate Breezer
  • Ultimate Breezer
  • Posts: 3264
  • Karma: 2
  • Thanks: 160
Hi.

That's ok, but values are typed directly in code. It's possible to get from database? Is there any example I could check?


yes it is possible, you can populate one select list, depends on the selected value from another select list


I will provide you with one example, give me a little time

if you have more questions,m feel free to ask!


Regards,
Mirko

Please Log in or Create an account to join the conversation.

Two dependent dropdown populated from database 7 months 4 weeks ago #272195

  • Mirec's Avatar
  • Mirec
  • Offline
  • Ultimate Breezer
  • Ultimate Breezer
  • Posts: 3264
  • Karma: 2
  • Thanks: 160
Hi,


in the example, we have two select list:

select list 1 - name-> city
select list 2 - name -> state

in the first select list, in Advanced of element scroll down to the section Action script select type custom, Action Change, and insert this code:
function ff_city_action(element, action)
{
    switch (action) {
        case 'change':
            jQuery.ajax({
                type: "POST",
                
                url: "<?php return JURI::root(true ); ?>/city.php",
                data: { code: ff_getElementByName('city').value},    
                success: function(data) {
                var obj = JSON.parse(data);
                var result = [];

    var keys = Object.keys(obj);
    keys.forEach(function(key){
        result.push(obj[key]);

    });
                        jQuery('#ff_elem370').empty();
                       
                       jQuery('#ff_elem370').append(jQuery('<option>', {
                                value: '',
                                text: 'Choose option'
                            }));
                       for (var i = 0; i < result.length; i++) { 
                           console.log(result[i]);
                           jQuery('#ff_elem370').append(jQuery('<option>', {
                                value: result[i],
                                text: result[i]
                            })); 
                        }
                }      
            });
            break;
        default:;
    } // switch
} // ff_city_action


-function ff_city_action(element, action) -> Instead 'city' insert name 1st select list!


- data: { code: ff_getElementByName('city').value}, Instead 'city' insert name of 1st select list!

- jQuery('#ff_elem370') - Instead 'ff_elem370' insert ID of 2st select list (Id you can find using inpect of browser).




then create one PHP file and insert the PHP file in the Joomla root, give the name to the PHP file like 'city' and insert the code below:
<?php

define('_JEXEC', 1);
if (file_exists(__DIR__ . '/defines.php'))
{
    include_once _DIR_ . '/defines.php';
}
if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';


$db = JFactory::getDBO(); 

$code=$_POST['code']; // the value posted from the AJAX request above

$db = JFactory::getDBO();

$db->setQuery("Select state From jos_Projects Where city = '$code' ");


$row =$db->loadColumn();
 if(!$row){
 	echo "error";
} else {

	echo json_encode($row);
}

?>


please,
$db->setQuery("Select state From jos_Projects Where city = '$code' ");
adjust SQL query based on your needs!

In code '$code' is value of selected value form select list1 'city'!



If you get to stuck, please let me know and I will help you!



Regards,
Mirko
The following user(s) said Thank You: webciudadreal

Please Log in or Create an account to join the conversation.

Last edit: Post by Mirec.

Two dependent dropdown populated from database 7 months 4 weeks ago #272196

  • Topic Author
  • webciudadreal
  • Offline
  • Senior Breezer
  • Senior Breezer
  • Posts: 80
  • Thanks: 1
Many thanks Mirko, it's awesome!

But ... I'm using Wordpress, and I think your code for city.php is for Joomla. Do you know equivalent code to Wordpress, or maybe I could simply connect to database with mysql_connect php function?

Thanks!

Please Log in or Create an account to join the conversation.

Two dependent dropdown populated from database 7 months 4 weeks ago #272197

  • Mirec's Avatar
  • Mirec
  • Offline
  • Ultimate Breezer
  • Ultimate Breezer
  • Posts: 3264
  • Karma: 2
  • Thanks: 160
Hi,

oke, I didn't test this code for WordPress, but I do not see the reason why wouldn't work.

Did you test with WordPress?

regards,
Mirko

Please Log in or Create an account to join the conversation.

Two dependent dropdown populated from database 7 months 4 weeks ago #272198

  • Topic Author
  • webciudadreal
  • Offline
  • Senior Breezer
  • Senior Breezer
  • Posts: 80
  • Thanks: 1

Mirec wrote: Did you test with WordPress?


Yes ... files includes/framework.php, defines.php don't exists in Wordpress, JFactory is not defined, loadColum function ...

I usually work with Joomla, but in this case I need to use this form in Wordpress.

Don't worry about it, I'm working on it, changing code to php/mysqli "standard" functions. Do yo think this is correct?

Thanks!

Please Log in or Create an account to join the conversation.

Moderators: ForumSupport
Time to create page: 0.053 seconds

Quick Links

Downloads

BreezingForms

ContentBuilder

BreezingCommerce

Templates

Documentation

BreezingForms

ContentBuilder

BreezingCommerce

Apprendre BreezingForms (French Community)

Apprendre et maîtriser BreezingForms par des tutoriels et exemples, le tout en français

breezingforms.eddy-vh.com

Questions et réponses sur les forums de l'AFUJ

AFUJ

Special Offer

Sale! All subscriptions at a special price!

Includes prio support, all of our current and future Joomla!® extensions and Joomla!® templates for the duration of your membership.

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!