How to display a message in textbox according to dropdown selection

hgaleano 19 Jun, 2015
Hi everyone

I have created a form with dynamic dropdowns with ajax and all works fine. Now, in other form, I need to show a message in a textbox, instead a second dropbox. I mean, according of the dropdown1 selection I have to fill a textbox. The problem is: I don't know what changes i have to do in the event setting(see image), and in addition how can i show the result value from dbread (in ajax event) in my textbox.

Thanks in advance for your answer

HugoG

PD. I have tried with DBREAD and with php custom code in ajax event. But nothing happens, this is my code
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('PAR_NAME1');
$query->from($db->quoteName('#__pweb_parameters'));
$query->where($db->quoteName('PAR_ID')." = ".$db->quote($form->data['cmbComm']));
$db->setQuery($query);
$db->execute();
$par = $db->loadResult();
echo $par;
GreyHead 22 Jun, 2015
Hi Hugo,

As far as I can see there is no built-in support for updating a textbox instead of setting drop-down options. What you can do is to select function in the Action drop-down, then add a function name e.g. getText() in the Element ID/fn()/Event box and add a custom function with that name in a Load JavaScript action in the form On Load event.

Here's a simple example of a function that gets a result back and adds it to a text input with the id text_input:
function getText() {
  jQuery.ajax({
    url: 'index.php?option=com_chronoforms5&chronoform=test_ajax_text_loader&event=ajax&tvout=ajax',
  })
  .done(function( data ) {
    jQuery('#text_input').val(data);
  });
}

Bob
Jojo 08 Jun, 2016
Hi Bob,

I have 5 text boxes where in user will be inputting values in first 3 textboxes. And I need to add/multiply those 3 textbox values and output the value to the 4th and 5th textboxes. Can you tell me how to echo the result to a textbox?
GreyHead 08 Jun, 2016
Hi Jojo,

You can do that with a few lines of custom JavaScript.

Bob
Jojo 12 Jun, 2016
Hi GreyHead,

I have tried with php. Following is the code: (here the values are inputted by user in 'deposit_amt', 'rate_int' and 'years' textboxes. And based on the values the fourth textbox 'cmpnd_quaterly' and fifth textbox 'cmpnd_anually' should show the final result after doing the specified calculations)
<?php
$form->data['cmpnd_quaterly'].val()=$form->data['deposit_amt'].val()(1+$form->data['rate_int'].val()/4)^(4*$form->data['years'].val())
$form->data['cmpnd_anually'].val()=$form->data['deposit_amt'].val()*($form->data['rate_int'].val()+1)^$form->data['years'].val()
?>


And have also tried one scenario with JavaScript: (Here how I want is if user enter e.g, 12 in textbox 'tenure_month', in another textbox 'years' it should show the months in years. in this e.g, 1 year)
jQuery(document).ready(function(jQ) {
  var year;
  year = jQ('#tenure_month').val()/12;
 jQ('#years').val()= year
}); 
Both the codes is not working. I tried removing .val() in php code. still no output after submission. Can you point out my mistake?
Jojo 14 Jun, 2016
Hi GreyHead,

Any input? Or any link referring to my case so that I can refer?

Thanks,
Pooja
GreyHead 15 Jun, 2016
Hi Jojo,

PHP is run on the server after the form is submitted so probably isn't what you need here. Also .val() isn't valid PHP.

JavaScript is probably what you need as that runs in the browser. However the code you have here will only run once when the page loads - you need to trigger it on a form event i,.e. when the user types in an input. See here for a tutorial.

Bob
Jojo 16 Jun, 2016
Hi GreyHead,

Thanks for your reply. As you said to use JS, here is the simplest thing I was trying. I have wherein if user enters 36 in text input ''tenure_month' another text input with id 'years' should spontaneously display '3' as number of years.

This is my form https://snag.gy/5dU6Vr.jpg

After seeing some forum topics I tried this simple JS Code:
window.addEvent('domready', function() {
$('tenure_month').addEvent('keyup', calc);
});
function calc() {
var yr = $('tenure_month').value/12;
$('years').value = yr;
});

I coded this in Load JS event and placed it with 'HTML Render Form' Event in 'On load' Event. While testing if I click the form the form just keeps on loading and does not open.
GreyHead 17 Jun, 2016
Hi JoJo,

You need that re-written using jQuery to work with CFv5 and Joomla! 3
jQuery(document).ready(function(jQ) {
  jQ('#tenure_months').keyup( calc );

  function calc() {
    var to_years;
    to_years = jQ('#tenure_months').val();
    to_years = parseFloat(to_years) / 12;
    to_years = to_years.toFixed(2);
    jQ('#years').val(to_years);
  };
});

Bob
This topic is locked and no more replies can be posted.