PHP Classes

Dependent fields

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  PHP Forms Class with HTML Generator and JavaScript Validation package blog  >  How to Show Google Ma...  >  All threads  >  Dependent fields  >  (Un) Subscribe thread alerts  
Subject:Dependent fields
Summary:How to display/hide a field depending on value in other field
Messages:6
Author:Mike Dev
Date:2010-02-21 09:42:17
Update:2010-02-22 03:06:34
 

  1. Dependent fields   Reply   Report abuse  
Picture of Mike Dev Mike Dev - 2010-02-21 09:42:17
I need a form with a select list, and depending on the value selected a text field is shown.

Example: Select programming language [PHP, Perl, Other]

If user selects "Other", then a text field should become visible in which he must specify the other programming language.

Is this possible with this library? I did not find it in the test files.

  2. Re: Dependent fields   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-02-21 10:04:40 - In reply to message 1 from Mike Dev
Currently there is no direct way to display or hide fields depending on other values.

Maybe if you can tell me about a situation you want to use that I can orient how to achieve it.

  3. Re: Dependent fields   Reply   Report abuse  
Picture of Mike Dev Mike Dev - 2010-02-21 10:36:56 - In reply to message 2 from Manuel Lemos
The situation is as described in the post above. When a user registers, I want to know what is his main programming language. He has a select list with the main languages, but the select list also has value "Other".

When he selects "Other", I want a field to appear that allows him to enter the other.

Another example is a form for returning an article to a store. User can choose for (select-list):
- Exchange article
- Refund

In case of a refund, I need to capture the bank account and account holder name.

  4. Re: Dependent fields   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-02-21 11:06:29 - In reply to message 3 from Mike Dev
I think the best way to do that is just use the ONCHANGE event to show or hide the relevant form sections using the display CSS property. You need to always add the form fields even if they are hidden initially.

To generate the necessary Javascript code to get the value of the input to check, you can use the forms class function GetJavascriptInputValue.

  5. Re: Dependent fields   Reply   Report abuse  
Picture of Mike Dev Mike Dev - 2010-02-21 11:47:10 - In reply to message 4 from Manuel Lemos
Manuel, I'm afraid I do not understand what you mean. Could you point me to an example? Or even provide this case an an extra example in the package? I think that would be really useful.
Thanks.

  6. Re: Dependent fields   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-02-22 03:06:34 - In reply to message 5 from Mike Dev
Imagine that you have input named "source" and as a <div id="group" style="display: none">some inputs here</div> that contains some inputs that you want to show only when input source has the value "special".

You can set the ONCHANGE event of the "source" input to mae the "group" section visible like this:

$source_javascript_value = $form->GetJavascriptInputValue('source');

$special_javascript_value = $form->EncodeJavascriptString('special');

$form->SetInputProperty(
'source',
'ONCHANGE',
'if('.$source_javascript_value.' == '.$special_javascript_value)
{
var group = document.getElementById("group").style.display = "block";
}'
);