Populating read only fields from database table as selected by radio button

  • hambakwe
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 40
  • Thank you received: 0

hambakwe created the topic: Populating read only fields from database table as selected by radio button

I've looked at many examples and solutions on the forum and I have not found what I'm looking for. I'm sure it is a much simpler solution than many of the more complex and sophisticated examples that I've found and tried to modify so far. I'm sure I'm over complicating this.

Simply, I have a radio button selector with 5 choices, one set as default.

On selection I wish to populate 5 read only fields on the form, drawing the results from a database table.

I do not want a submit button, I want it to run when the radio button choice is made.

I imagine that the solution is a fairly simple Before Form script to pull up the data possibly by running an external x.php db query. Then a series short php lines in the form field value box.

I'd really appreciate any pointers or help.
#238204
  • Posts: 2734
  • Karma: 93
  • Thank you received: 369

mihaela replied the topic: Populating read only fields from database table as selected by radio button

Hello,

To achieve what you want, you have to write an Ajax query in the Actionscript of the radio group button.

Here is an example of a similar task crosstec.org/en/forums/15-usage/126531-a...he-list.html?start=0 .
Also, you can take a look at this example crosstec.org/en/forums/1-forums/125334-m...html?start=24#232442 .

The main difference is that you have to fetch a radio group selected value using this function:
jQuery('[name="ff_nm_radiogroupname[]"]:checked').val();
instead of with just:
element.value

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

Regards,

Mihaela
#238227
  • hambakwe
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 40
  • Thank you received: 0

hambakwe replied the topic: Populating read only fields from database table as selected by radio button

Dear Mihaela,

Thanks for your reply, I've read the links you sent and tried as below, but this does not run. I suspect I've missed something simple.
#238231
Attachments:
  • hambakwe
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 40
  • Thank you received: 0

hambakwe replied the topic: Populating read only fields from database table as selected by radio button

Dear Mihaela, Thanks for your reply and the links to examples. I have tried to adapt the examples to suit what I'm trying to do. Unfortunately it doesn't run but I'm sure I have just missed something obvious.

I will set out exactly what I"m trying to achieve and attach what I've got so far.

Firstly here is the form I'm trying to populate:



The Radio Button is called "currency"

The fields to be pulled from the database are "benbank" "benbankadd" "swift" "beneacct" "benenumber" "beneaddr" and "pmtdetail"

The database table is called "mexaccount" (prosite_mexaccount)

Firstly I added the Actionscript to the Radio Button

function ff_currency_action(element, action) //where currency is the name of the radio group{
switch (action) {
case 'change':
jQuery.ajax({
type: "POST",
url: "http://localhost/accountinfo.php",
data: { mexaccount: element.value },
success: function(data) {
var obj = JSON.parse(data);
var arr = obj;
ff_getElementByName(‘benbank‘).value = arr[0];
ff_getElementByName(‘benbankadd‘).value = arr[1];
ff_getElementByName(‘swift‘).value = arr[2];
ff_getElementByName(‘beneacct‘).value = arr[3];
ff_getElementByName(‘benenumber‘).value = arr[4];
ff_getElementByName(‘beneaddr‘).value = arr[5];
ff_getElementByName(‘pmtdetail‘).value = arr[6]; }
});
break;
default:;
} // switch
} // ff_currency_action

Then I created the PHP file to collect the data:

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );

$username = $_GET;
$database = &JFactory::getDBO();
$sql = "SELECT * FROM prosite_mexaccount WHERE currency = '$currency'";

$database->setQuery( $sql );
$result=$database->mexaccount();
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *'); // CORS Settings
echo json_encode($result);

?>

For reference here is an image of the DB table fields



As said, it is probably something obvious to someone who is familiar with the coding, I'd be very grateful if you could take a look over this.
#238233
Attachments:
  • Posts: 2734
  • Karma: 93
  • Thank you received: 369

mihaela replied the topic: Populating read only fields from database table as selected by radio button

Hello,

I see what you are trying to achieve.

At first glance, I see one error in your code.

In your code instead of this line
data: { mexaccount: element.value }
put the following:
data: { mexaccount: jQuery('[name="ff_nm_currency[]"]:checked').val() }

Also, I see that you have missed to fetch the data sent from the form in the PHP file.
You should have a line like this:
$currency= JRequest::getVar('mexaccount');

You should put this before this line:
$db = JFactory::getDBO();

Change the code from the query to this(so remove your existing code after the query):
$db->setQuery('SELECT `benbank`, `benbankadd`, `swift` FROM `prosite_mexaccount` WHERE currency ='.$db->quote($currency)); 
$result = $db->loadRow();
 if(!$result){
 	echo "error";
} else {
	echo json_encode($result);
}
Add to the query all the wanted fields.

Let me know whether after you do this changes your form starts working.

Regards,

Mihaela
#238245
  • hambakwe
  • Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 40
  • Thank you received: 0

hambakwe replied the topic: Populating read only fields from database table as selected by radio button

Hi Mihaela,

I've made the changes you suggested and at the moment it does not run - just to double check that I have all as you suggested here are the scripts:

Actionscript:

function ff_currency_action(element, action) //where currency is the name of the radio group
{
switch (action) {
case 'change':
jQuery.ajax({
type: "POST",
url: "http://localhost/accountinfo.php",
data: { mexaccount: jQuery('[name="ff_nm_currency[]"]:checked').val() },
success: function(data) {
var obj = JSON.parse(data);
var arr = obj;
ff_getElementByName(‘benbank‘).value = arr[0];
ff_getElementByName(‘benbankadd‘).value = arr[1];
ff_getElementByName(‘swift‘).value = arr[2];
ff_getElementByName(‘beneacct‘).value = arr[3];
ff_getElementByName(‘benenumber‘).value = arr[4];
ff_getElementByName(‘beneaddr‘).value = arr[5];
ff_getElementByName(‘pmtdetail‘).value = arr[6]; }
});
break;
default:;
} // switch
} // ff_currency_action

accountinfo.php:

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/' ));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );

$currency= JRequest::getVar('mexaccount');
$database = JFactory::getDBO();
$sql = "SELECT * FROM prosite_mexaccount WHERE currency = '$currency'";

$database->setQuery('SELECT `benbank`, `benbankadd`, `swift` `bebeacct` `benenumber` `beneaddr` `pmtdetail` FROM `prosite_mexaccount` WHERE currency ='.$database->quote($currency));
$result = $database->loadRow();
if(!$result){
echo "error";
} else {
echo json_encode($result);
}

?>
#238259
Moderators: ForumSupporttomeperica
Time to create page: 0.305 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