Form fields in custom form

22 posts by 3 authors in: Forums > CMS Builder
Last Post: August 21, 2014   (RSS)

By design9 - June 26, 2014

I have a custom form and was wondering is there anyway to only show certain fields in form based on what category the user selects?

Here is my form : 

http://www.charlotteparent.com/directory/freeform.php

Ex: If the user picks Baby Fair from category list, then an additional field will pop up to input Babies ages. I just want to create one form instead of a separate form for each category. Just trying to figure out how I could have certain associated fields show up based on the category the user selects.

Thanks!

April

By design9 - June 30, 2014

I have a custom form and was wondering is there anyway to only show certain fields in form based on what category the user selects?

Here is my form : 

http://www.charlotteparent.com/directory/freeform.php

Ex: If the user picks Baby Fair from category list, then an additional field will pop up to input Babies ages. I just want to create one form instead of a separate form for each category. Just trying to figure out how I could have certain associated fields show up based on the category the user selects.

Thanks!

April

By Dave - July 3, 2014

Hi April, 

This is actually how our field editor in the CMS works.  As you select different field types it hides and shows the relevant related fields.

Usually how it's done is to create a form with all the possible fields, then use jquery to hide and show them in realtime as the user makes selections.  Here's some links to jquery:

If you're not familiar with jquery it can take a bit to learn, but it's very powerful.  http://learn.jquery.com/

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By design9 - July 10, 2014

Thanks! I created the query to show and hide fields based on category perfectly.

Here are the only issues I have now?

1)  One of my fields is a multi value list using checkboxes. I can get the checkboxes to display on the form but when I fill out the form and select more than one option, it only records one option in the database instead of both options that were checked by user on form. How can I fix that so it records all checked selections? See line 322 of my code.

2) How do I keep the selected fields for the chosen category (that are shown upon category selection using the query) to stay on page after user submits form? Of course the page is getting refreshed but I wasn't sure if I need to control that via the jquery or it is something different since the form uses the "preSaveTempId"?

Attachments:

freeform1.php 23K

By Dave - July 17, 2014

Hi April,

Sorry for the delay, this is getting more into custom programming but I'll try and give you some tips to point you in the right direction.

The only way to get PHP to accept multiple fields with the same name is to add [] after the fieldname, like this:

<input type="checkbox" name="services_provided[]" ...

This will save the value(s) to $_REQUEST['services_provided'] but as an array.
More details: http://stackoverflow.com/questions/7880619/multiple-inputs-with-same-name-through-post-in-php

Then to store them in MySQL you need to convert the multiple values into the same format CMSB uses internally, which is tab padded and separated.  (tab)like(tab)this(tab)

$services_provided_TSV = "\t" . implode("\t", $_REQUEST['services_provided'] ) . "\t";

Then save that to MySQL.

2) How do I keep the selected fields for the chosen category (that are shown upon category selection using the query) to stay on page after user submits form? Of course the page is getting refreshed but I wasn't sure if I need to control that via the jquery or it is something different since the form uses the "preSaveTempId"?

You need to check each option value in the category pulldown and add selected="selected" if it's been selected.  The code you have here should do it?  <?php selectedIf($value, @$_REQUEST['category']);?>

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By design9 - July 18, 2014 - edited: July 18, 2014

Thanks so much!

On this question: 2) How do I keep the selected fields for the chosen category (that are shown upon category selection using the query) to stay on page after user submits form? Of course the page is getting refreshed but I wasn't sure if I need to control that via the jquery or it is something different since the form uses the "preSaveTempId"?

I am not having a problem with the option selected. The issue is with the fields that I have hidden when the page first loads via the jquery.

Ex: I have a field called Ages that is hidden and only shows after the user selects the Camps category from drop down. Let's say the user forgets to fill out a required field. The error message will pop up but then the Ages field that was only shown with Camps category selection, no longer shows up since the page is refreshed. Same thing happens after the user submits, those fields no longer show up. The main issue is with them being hidden again when there is an error with a required field. Is there anyway to keep those fields populated when that happens?

By design9 - July 29, 2014

On this question: 2) How do I keep the selected fields for the chosen category (that are shown upon category selection using the query) to stay on page after user submits form? Of course the page is getting refreshed but I wasn't sure if I need to control that via the jquery or it is something different since the form uses the "preSaveTempId"?

I am not having a problem with the option selected. The issue is with the fields that I have hidden when the page first loads via the jquery.

Ex: I have a field called Ages that is hidden and only shows after the user selects the Camps category from drop down. Let's say the user forgets to fill out a required field. The error message will pop up but then the Ages field that was only shown with Camps category selection, no longer shows up since the page is refreshed. Same thing happens after the user submits, those fields no longer show up. The main issue is with them being hidden again when there is an error with a required field. Is there anyway to keep those fields populated when that happens?

By claire - July 30, 2014

Hi April

I'd do this by checking the variables in the $_REQUEST as the page loads, and then triggering the Javascript to hide/show different fields as needed if there's an error and the same form needs to be shown again. It can be easy to implement this, or very tricky, depending on how the Javascript is set up. There's no quick fix for it, unfortunately, but a basic version would take about an hour of programming time, I think.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By design9 - July 30, 2014

Thank you!

One last thing and I promise I will stop bothering you today!

I included the coding Dave provided above with my field names:

$days_TSV = "\t" . implode("\t", $_REQUEST['days'] ) . "\t";

It all works great when I select the category that it associated with. However, when I choose another category that is not associated with that field (days), I get an error undefined index because that fields days is being hidden by my query. 

Here is the error: Notice: Undefined index: days in C:\inetpub\charlotteparent\directory\freeform1.php on line 86 Warning: implode(): Invalid arguments passed in C:\inetpub\charlotteparent\directory\freeform1.php on line 86

Is there any other way to fix that error when I am selecting other categories?