[1]

kesja plecha
Guest
January 15th, 2009 at 10:44 am

I want some form elements change depending on category which user will choose. So user would choose category first and he/she would see form for that category.

How can I do this? I believe it could be done via \’hack form\’ tab, but I\’m not sure how to do this. Any help would be appreciated.

(…and thanks for awesome plugin ;)

Permalink | Quote

[2]

Mark Cunningham
Guest
January 19th, 2009 at 2:47 pm

Do you want different forms for different categories or different elements in the form depending on the category? Either way it’s a bit tricky.

Permalink | Quote

[3]

Kesja Plecha
Guest
January 19th, 2009 at 3:30 pm

I think it doesn’t matter… but probably displaying whole new form would be a little easier (?) and more practical to modify later.

I’m thinking hardcoding some ajax would do but i never worked with ajax.

Permalink | Quote

[4]

Mark Cunningham
Guest
January 20th, 2009 at 11:20 am

I’m not sure you need “ajax” per say, but certainly javascript.

What I would have do is create a page template in your theme and in this theme, create all the forms you wish to display dynamically using the template tags (i.e. tdomf_the_form(id of form)). Then use a bit of jQuery magic to hide all of them except the one you’re interested in.

I did a small test and managed to get it to work using a page template. This is the code I added to switch dynamically between 4 different forms depending on a radio button:

<?php if(function_exists('tdomf_the_form')){ ?>

   <script type='text/javascript' src='<?php echo get_bloginfo('wpurl'); ?>/wp-includes/js/jquery/jquery.js'></script>

   <style>
      #tdomf_form1 { display: none; }
      #tdomf_form2 { display: none; }
      #tdomf_form3 { display: none; }
      #tdomf_form4 { display: none; }
   </style>

   <script type="text/javascript">
   //< ![CDATA[
   function show_form(form_id){
      switch(form_id) {
      case 1:
         jQuery('#tdomf_form2').css("display", "none");
         jQuery('#tdomf_form3').css("display", "none");
         jQuery('#tdomf_form4').css("display", "none");
         jQuery('#tdomf_form1').css("display", "block");
         break;
      case 2:
         jQuery('#tdomf_form1').css("display", "none");
         jQuery('#tdomf_form3').css("display", "none");
         jQuery('#tdomf_form4').css("display", "none");
         jQuery('#tdomf_form2').css("display", "block");
         break;
      case 3:
         jQuery('#tdomf_form1').css("display", "none");
         jQuery('#tdomf_form2').css("display", "none");
         jQuery('#tdomf_form4').css("display", "none");
         jQuery('#tdomf_form3').css("display", "block");
         break;
      case 4:
         jQuery('#tdomf_form1').css("display", "none");
         jQuery('#tdomf_form2').css("display", "none");
         jQuery('#tdomf_form3').css("display", "none");
         jQuery('#tdomf_form4').css("display", "block");
         break;
      default:
         alert("ERROR: show_form: Unknown Form ID");
         break;
      }
   }
   //-->
   </script>

   <?php if(tdomf_can_current_user_see_form(1)) { ?>
      <input type="radio" name="dynamic_forms" id="dynamic_forms_1" onChange="show_form(1);">
      <label for="dynamic_forms_1">Form 1</label><br/>
   <?php } ?>

   <?php if(tdomf_can_current_user_see_form(2)) { ?>
       <input type="radio" name="dynamic_forms" id="dynamic_forms_2" onChange="show_form(2);">
      <label for="dynamic_forms_2">Form 2</label><br/>
   <?php } ?>

   <?php if(tdomf_can_current_user_see_form(3)) { ?>
      <input type="radio" name="dynamic_forms" id="dynamic_forms_3" onChange="show_form(3);">
      <label for="dynamic_forms_3">Form 3</label><br/>
   <?php } ?>

   <?php if(tdomf_can_current_user_see_form(4)) { ?>
      <input type="radio" name="dynamic_forms" id="dynamic_forms_4" onChange="show_form(4);">
      <label for="dynamic_forms_4">Form 4</label><br/>
   <?php } ?>

   <?php if(tdomf_can_current_user_see_form(1)) tdomf_the_form(1); ?>
   <?php if(tdomf_can_current_user_see_form(2)) tdomf_the_form(2); ?>
   <?php if(tdomf_can_current_user_see_form(3)) tdomf_the_form(3); ?>
   <?php if(tdomf_can_current_user_see_form(4)) tdomf_the_form(4); ?>

<?php } ?>

You should be able to use that as a base to do more funky things. The builtin jQuery in Wordpress 2.7 allows you to do some nice stuff, like fading in and out the form. And it seems to work fine with a mix of forms using AJAX or non-AJAX too. :)

Permalink | Quote

[5]

Kesja Plecha
Guest
January 22nd, 2009 at 12:26 am

that’s *exactly* what i was thinking about. thanks xD

Permalink | Quote

[6]

Alex
Guest
September 14th, 2009 at 2:41 pm

Where I need to insert this code? in the hack code?

Permalink | Quote

[7]

Jay
Guest
October 16th, 2009 at 1:49 am

Hi! Is there a way to change the selection from radio to buttons? I have spend the better part of the day trying, but my coding skills are close to nil. Please help if you can. Thank you.

Permalink | Quote

Advertisments

Join the Discussion