Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Additional inputfields by certain values

    Hi all,

    I am building a form with multiple questions and some inputfields only need to be shown if a certain inputfield has a certain answer. Those new inputfields need to be shown when the answer is given by selecting a radiobutton. But i don't know how to build this in PHP. Does someone know how to get this right?

    For example:
    If question 1 of the type 'radio' with the name 'geslacht' has one of the values 'st_mv', 'st_mm' or 'st_vv' then the fieldset with the id 'persoon2' has to be shown. Otherwise it has to be hidden.

    This is what i have got so far:
    PHP Code:
    <?
                $geslachtarray
    =array
                (
                     array (
    'Man''m''geslacht_m'),
                     array (
    'Vrouw''v''geslacht_v'),
                     array (
    'Man/Vrouw-stel','st_mv','geslacht_mv'),
                     array (
    'Vrouw/Vrouw-stel','st_vv','geslacht_vv'),
                     array (
    'Man/Man-stel','st_mm','geslacht_mm')
                );
                for(
    $b=0$b<=4$b++)
                {
                    
    ?>
                    <div class="antw">
                    <input type='radio' name='geslacht' value='<? echo $geslachtarray[$b][1?>'
                    id='<? echo $geslachtarray[$b][2?>'
                    <?
                    
    if($geslachtarray[$b][1]==$geslacht)
                    {
                    
    ?>
                    checked
                    <?
                    
    }
                    
    ?>
                    > <label for='<? echo $geslachtarray[$b][2?>'><? echo $geslachtarray[$b][0?></label>
                    </div>
                    <?
                
    }
                
    ?>

    <fieldset class="fieldset_form" id="persoon2" hidden="true">
    Thanx in advance.

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    141
    Thanks
    3
    Thanked 20 Times in 20 Posts
    If you want to do this in real time (ie. no browser refresh) you need to employ Ajax. One of the easiest solutions for this would be Jquery a javascript framework. With this you can have the fields asynchronously load if a certain value is selected.

    An easier way to this would be to use Jquery to disable and hide the fields when they should not be in play. This would mean only one query to the DB server. Hopefully with this information you can go away and choose an option that best suits.

    Either way the difficulty of a UI like you are describing is more difficult, but your users will thank you for 'no refresh' solutions..

  • Users who have thanked itxtme for this post:

    lampie1978 (04-20-2013)

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,096
    Thanks
    23
    Thanked 594 Times in 593 Posts
    No. For such an easy thing ajax would just complicate everything.

    @lampie1978 you have a number of errors in your code ex.: change this line
    Code:
    if($geslachtarray[$b][1]==$geslacht)
    To:
    Code:
    if($geslachtarray[$b][1]=="geslacht")
    But you don't need php, you do need javascript:
    Is this anything close to what you want or at lest, can you use it to get what you want?
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function Man(){
    	document.getElementById('m').style.display='block';
    	document.getElementById('geslacht_v').style.display='none';
    }
    function Vrouw(){
    	document.getElementById('v').style.display='block';
    	document.getElementById('geslacht_m').style.display='none';
    }
    </script>
    </head>
    <body>
    
    <div id='geslacht_m'>
    <input type='radio' value='Man' onclick="Man();">Man</div> 
    <div id='geslacht_v'>
    <input type='radio' value='Vrouw' onclick="Vrouw();" >Vrouw</div> 
    
    
    <div id="m" style="display:none;"><br> 
    <input type='radio' name='geslacht_mv'>Man/Vrouw-stel<br>
    <input type='radio' name='geslacht_mm'>Man/Man-stel<br> 
    </div>
    
    <div id="v" style="display:none;"><br> 
    <input type='radio' name='geslacht_mv'>Man/Vrouw-stel<br> 
    <input type='radio' name='geslacht_vv'>Vrouw/Vrouw-stel<br> 
    </div>
    </body>
    </html>
    PS Do not use <? to start a php section of code use <?php.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    lampie1978 (04-20-2013)

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    @itxtme: oke that would be a challenge indeed. I never programmed anything in Ajax or Jquery so that would be a first.

    @sunfighter: i allready have this function in JavaScript, but i am trying to build the site WCAG1.0 compliant. One of the rules there is that when JavaScript is disabled that a form should function as if it still has JavaScript on. So i am putting the difficulty on myself and so JavaScript is not an option for this challenge.
    Another thing in WCAG is that 'refresh' isn't permitted also, so i am not going to use the refresh option either.

    So Ajax or Jquery is the best answer i got so far. Any other options? Otherwise i need to learn Jquery asap.

  • #5
    Regular Coder
    Join Date
    Jun 2009
    Posts
    141
    Thanks
    3
    Thanked 20 Times in 20 Posts
    @lampie

    Jquery is a javascript framework, so by using jquery you are using javascript, and your problem that it being turned off causes failure occurs.. The same is said for Ajax, (Asynchronous JavaScript and XML)

    Also I dont understand why a refresh would be non compliant. You cannot haver browser side processing occur with php, the data must be sent back to the server.. You should decide what you are trying to achieve, and if it MUST work without javascript then show all of the fields that then are ignored during processing, or have two pages /a refresh that shows the initial field and once hey are selected and submitted have the scrip put the next arguments. Both of these options are not ideal..

    Remember javascript use is approx 98% (as of 2010 - prob more now)

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi itxme & sunfighter,

    I am going to build the UI with JS / JQuery.
    Although i have to rebuild my entire CSS accordingly to have the things right and use id's for it.

    Thanx for your answers.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •