Auto populating text from database using value that was chosen from the list.

  • maurijkmakitalo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 5
  • Thank you received: 0

maurijkmakitalo created the topic: Auto populating text from database using value that was chosen from the list.

Hi,

So I have a DB where I have Customer table that contains name and address of the customers and I am auto populating list used in the form to show names from that DB table. What I'd like to to is to auto populate next textfield in the form to show Address of that customer user choosed in the first list. If anyone knows how to do it and can help me with it or knows if it has been asked and answered before and could link me to it. Liked picture that tries to explain what I need better.
#237092
Attachments:
  • Posts: 2463
  • Karma: 86
  • Thank you received: 333

mihaela replied the topic: Auto populating text from database using value that was chosen from the list.

Hello,

This crosstec.org/en/forums/1-forums/126483-b...avascipt.html#236904 is an example which is very close to what you need.

The difference is that in this example action will be triggered when the submit button is pressed and not after selection in the select list.

You just need to put the code in Actionscript for the first select list element and check "Change" as "Action".

Here is another similar example crosstec.org/en/forums/1-forums/125334-m...html?start=24#232442 , but it works a bit differently.

Let me know whether you have managed to apply this to your form.

Regards,

Mihaela
#237094
  • maurijkmakitalo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 5
  • Thank you received: 0

maurijkmakitalo replied the topic: Auto populating text from database using value that was chosen from the list.

Hello,

thanks to those links I did get some understanding about the issue but not completely. Right now I have some kind of query made in the actionscript in the select list that looks like this:

function ff_asiakkaann_action(element, action)
{
switch (action) {
case 'change':

$db = JFactory::getDBO();
$db->setQuery('Select `Address name` FROM asiakas Where name="asiakkaann"');
$result = $db->loadResult()+1;
break;
default:;
} // switch
} // ff_asiakkaann_action

And to clarify asiakkaann = name of select list
and asiakkaano is name of the text field where I want the result.

I at least think the query part should be fine but i am still missing that function that chances value to text field. I tried
ff_getElementByName('asiakkaano').value = ;

where I did try different kind of things after =, but could not get the result to show in the text field.
#237111
  • Posts: 2463
  • Karma: 86
  • Thank you received: 333

mihaela replied the topic: Auto populating text from database using value that was chosen from the list.

Hello,

This will not work.

In the Actionscript, only JavaScript code can be written and you have put there a PHP code.

That is the reason why an ajax call is needed so you can connect to a PHP file and write a query to the database as explained in the link.

1) The Actionscript for the first select list should look like this:
function ff_SELECTLISTNAME_action(element, action)
{
 jQuery.ajax({
        type: 'POST',
        url: 'http://localhost/value.php',
        data: ask: element.value,
        success: function(data){
           ff_getElementByName("ADDRESSFIELDNAME").value = data; 
    });
           
} // ff_SELECTLISTNAME_action
NOTE: Replace "http://localhost/value.php" with the correct URL to the new value.php file that you will create.
Also, replace SELECTLISTNAME and ADDRESSFIELDNAME with the corresponding values.

2) Then go to the Joomla root folder and create there file named value.php with the following content:
<?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';


$ask = JRequest::getVar('ask'); 
$db = JFactory::getDBO();
Select `Address name` FROM asiakas Where name="asiakkaann"'
$db->setQuery('SELECT `Address name` FROM asiakas WHERE `name` ='.$db->quote($ask)); 

$result =  $db->loadResult(); 
echo json_encode($result);
NOTE: Make sure to write the correct SQL query. `Address name` has to be the name of a column in database table as well as the `name` .

Let me know whetehr you have managed to set this for your form.

Regards,

Mihaela
#237119
  • maurijkmakitalo
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 5
  • Thank you received: 0

maurijkmakitalo replied the topic: Auto populating text from database using value that was chosen from the list.

Hi, thanks for reply.

Well it seems I did not understand it correctly first time.
Now I have made the changes according your instructions and actionscript looks like this:
function ff_asiakkaann_action(element, action)
{
jQuery.ajax({
type: 'POST',
url:'http://172.16.2.10/value.php',
data: ask: element.value,
success: function(data){
ff_getElementByName("asiakkaano").value = data;
});

} // ff_asiakkaann_action

I am not sure that can I use IP in the URL but that is what we use to connect to the site because it is only working locally.

And then I have made value.php that looks like this:
<?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';

$ask = JRequest::getVar('ask');
$db = JFactory::getDBO();
Select `Address name` From asiakas Where customer="asiakkaann"
$db->setQuery('SELECT `Address name` FROM asiakas WHERE customer ='.$db->quote($ask));

$result = $db->loadResult();
echo json_encode($result);

Query was actually wrong in the last message but this one should be correct, at least it was working in the SQL workbench when I tried manually searching Address with typing some of the customers after = symbol.

Still does not work on the form and I really don't know what I am missing anymore.

Lastly added pic of the value.php.
#237140
Attachments:
  • Posts: 2463
  • Karma: 86
  • Thank you received: 333

mihaela replied the topic: Auto populating text from database using value that was chosen from the list.

Hello again,

The scripts are now almost complete. I see now that when copying I did a mistake.
The following line should be deleted:
Select `Address name` From asiakas Where customer="asiakkaann"

I've tested this and realized that it should also be
data: { ask: element.value } ,
instead of:
data: ask: element.value,

So, the whole Actionscript should look like this:
function ff_asiakkaann_action(element, action)
{
jQuery.ajax({
type: 'POST',
url:'http://localhost/value.php',
data: { ask: element.value } ,
success: function(data){
ff_getElementByName("asiakkaano").value = data;
}
});

} // ff_asiakkaann_action

Furthermore, the value.php file should have this content:
<?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';


$ask = JRequest::getVar('ask'); 
$db = JFactory::getDBO();

$db->setQuery('SELECT `Address name` FROM asiakas WHERE `customer` ='.$db->quote($ask)); 

$result =  $db->loadResult(); 
echo json_encode($result);

A http://localhost/value.php should be the actual address where you can fetch the file.

Let me know whether this is working for you.

Regards,

Mihaela
#237143
The following user(s) said Thank You: maurijkmakitalo
Moderators: ForumSupporttomeperica
Time to create page: 0.171 seconds

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans, templates and icon packs!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

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!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here