-1

I have dropdown selector in my registration form, for choose in which customer group customers want register. This is my code for dropdow:

   <form action="register.phtml" method="post"> 
    <div class="input-box">
        <label for="group_id"><?php echo $this->__('Odaberite razinu') ?><span class="required">*</span></label><br/>
        <select name="group_id" id="group_id" title="<?php echo $this->__('Group') ?>" class="validate-group required-entry input-text" />
            <?php $groups = Mage::helper('customer')->getGroups()->toOptionArray(); ?>
            <?php foreach($groups as $group){ ?>
            <option value="<?php print $group['value'] ?>"><?php print $group['value'] ?></option>
            <?php } ?>
        </select>
    </div>
   </form>  

Now I want show some extra fields in reg. form just for customer group with ID 4. I try something like this, but nothing happens. Extra fields don't show up when group with ID 4 is selected:

    <?php 

        if ( $_POST['group_id'] == '4')
        {
    ?>

            <my html code with extra fields>

             ?>

How I can check what customer group is selected and besides on that show some extra html fields for customer to insert? Is it my "if" correct and something is wrong or I something missing up in dropdown form?

  • did you manage to find a solution for your issue? – Sander Mangel Oct 17 '13 at 7:29
  • yes I did. please see my answer below. thank you for your answer and suggestion to use javascript. – srdan Oct 17 '13 at 19:36
1

The _POST variable is not filled until an actual post is done. I think you are looking for a dynamic javascript solution in this case.

I'm no javascript or prototype expert so please double check this Javascript but I think you're looking for something along the lines of this

<select name="group_id" id="group_id" ... >
   [...]
</select>
<input type="hidden" name="group_specific_attr" id="group_specific_attr" class="" value=""/>

<script>
$('group_id').observer('change', function(){
   if ($(this).value == 4)
   {
      $('group_id').setAttribute('type','text');
      $('group_id').setAttribute('class','required-entry');
   }
   else
   {
      $('group_id').setAttribute('type','hidden');
      $('group_id').setAttribute('class','');
   }
});
</script>
0

I found solution for that issue. Hope someone helps. I use simple javascript function:

         <script type="text/javascript">
             function izborgrupe(type)
                {
                   document.getElementById("4").style.display = "none";
                   document.getElementById(type).style.display = "";
                }
         </script>  

Before that in select tag use "onchange" to call function above:

<select name="group_id" id="group_id" title="<?php echo $this->__('Group') ?>" class="validate-group required-entry input-text" onchange="izborgrupe(this.value)">
                    <?php $groups = Mage::helper('customer')->getGroups()->toOptionArray(); ?>
                    <?php foreach($groups as $group){ ?>
                    <option value="<?php print $group['value'] ?>"><?php print $group['label'] ?></option>
                    <?php } ?>
</select>

And at the end I named span tag where is my extra filed with ID 4 and give style to display:none

    <span id="4" style="display:none">  

         <div class="fieldset">
            <ul class="form-list">
                <li>
                    <label for="school" class="required"><em>*</em><?php echo $this->__('Broj osnovnog osiguranja') ?></label>
                    <div class="input-box">
                        <input type="text" name="school" id="school" value="<?php echo $this->htmlEscape($this->getFormData()->getSchool()) ?>" title="<?php echo $this->__('school') ?>" class="input-text" />
                    </div>
                </li>
            </ul>
        </div>
     </span>

As result, extra field from span ID "4" will shows up only when value "4" from dropdown is selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.