Get checkbox == checked through xslt to C# function (contactform)

Topics: Troubleshooting, XSLT
Aug 9, 2011 at 12:01 PM
Edited Aug 9, 2011 at 1:41 PM

Hi,

I have a simple question (I think) but cannot find a working solution. I extended the contact form with a checkbox and added a boolean value to the ContactFormData, extended the send function in Functions.
My problem is how to store, set and read the checked value when posting data.

I extended SendContactFormMarkup variable in the contactforms.xsl (to be able to submit the value to the send C# function) but how do I get the checked value (i.e. true or false). All other fields use {c1:GetFormData('phonenumber')} etc..
but {c1:GetFormData('checkboxid')} always gives me false. 

thnx,

JamBo

UPDATE: Solved it for now. I think this should be easier, but then again, it works.
      First changed the field type in the ContactFormData and C# function to string so I could use {c1:GetFormData('checkboxid')} and value="{$SendActionData[@Fieldname = 'checkboxid']/@Value}"
      Added an onchange function on the input:  onchange="handleChange(this);"

function handleChange(cb)
{

            if(cb.checked)
            {
                 cb.value = "checked";
            }
            else
            {
                 cb.value = "";
            }
        }

 

      Right below the checkbox i added a little script tag:  <script>document.forms[0].checkboxid.checked = document.forms[0].checkboxid.value == "checked"</script>

Still would like to know if it could be done easier.
mzZzl,
JamBo 

Coordinator
Aug 9, 2011 at 2:15 PM
Edited Aug 9, 2011 at 2:15 PM

>> but {c1:GetFormData('checkboxid')} always gives me false. 

You can use FireBug to see what is actually posted. Usually if you have:

<input type="checkbox" id="xxx" name="yyy" />

it will post yyy = "on" if the checkbox is checked and nothing if otherwise. You can write something like

<input type="checkbox" id="xxx" name="yyy" value="checked" />

 

>> Still would like to know if it could be done easier.

I'm not sure from which package you get your sample, but try something like that

 

<input type="checkbox" id="checkboxid" name="checkboxid" value="true">

  <xsl:if test="$SendActionData[@Fieldname = 'checkboxid']/@Value = 'true'">

       <xsl:attribute name="checked">true</xsl:attribute>

   </xsl:if>

</input>

 

 

it should post "true", and also set "true" if the C# function you're using is sending "true"

Aug 9, 2011 at 2:31 PM

Thnx for the swift reply,

You are exactly right when i would be doing a postback, but i'm not. The package is 'http://docs.composite.net/C1/Packages/Community/Contact-Form.aspx', that passes the form values to an C# External function.

offtopic: Why can I edit my own created c# functions in the console but not the one(s) from composite. Is that the difference between external and inline c# functions?

thnx,

JamBo

Coordinator
Aug 9, 2011 at 3:12 PM

>> offtopic: Why can I edit my own created c# functions in the console but not the one(s) from composite. Is that the difference between external and inline c# functions?

Well, we have different kinds of C# functions:

1) Inline functions - the one, source of which you can edit in administration console

2) External functions - in console those are represented as a references to a C# class/method from a /bin/*.dll file or from App_Code. You can edit those if you have access to App_Code, or the dll it is defined in

3) "Build-in" functions. Those are defined through "function providers" in /App_Data/Composite/Composite.config, section "Composite.Functions.Plugins.FunctionProviderConfiguration"

You can edit those only buy making a custom build of Composite.dll, which is possible, but of course not recommended - this way you would have to merge your changes in the case of upgrading which is time consuming.

We have some other kinds of functions, but I guess that info should be enough to answer your question.

Aug 10, 2011 at 7:44 AM

Dmitry,

Thnx for clearing that up (probably could have figured it out myself I guess).
Any clarification on the issue at hand?

mzZzl,
JamBo 

Coordinator
Aug 10, 2011 at 2:21 PM

Hi,

>> My problem is how to store, set and read the checked value when posting data.

I reproduced your scenario and successfully extended Contact From with CheckBox field. Below are steps how to do this:

1. Add new Boolean field to the data type Composite.Community.ContactFormData, e.g named as Option.

2. Edit XSLT function Composite.Community.ContactForm and a) add checkbox field to the form:

 <label for="option">Option</label> 
 <input type="checkbox" id="option" name="option" value="true">
      <xsl:if test="$SendActionData[@Fieldname = 'option']/@Value = 'true'">
                <xsl:attribute name="checked">true</xsl:attribute>
      </xsl:if>
 </input>

b) extend variable named SendContactFormMarkup and add new parameter:

<f:param name="option" value="{c1:GetFormData('option')}" />

3. Edit C# file ~\App_Code\Composite\Community\ContactForm\Functions.cs, locate method Send() and add additional parameter option:

public static IEnumerable Send(..., bool option,..)

locate #region SubmittedData and make sure to include this parameter:

yield return new XElement("SubmittedData",
                            new XAttribute("Fieldname", "option"),
                            new XAttribute("Value", option));


locate #region Save data and send Email and make sure that this parameter value is stored:

newContactFormData.Option = option;

 
4.To include this new filed to the Email, locate Email template and edit its Content tab (Content => Website Items => Contact Form Email Template.)

 

Aug 11, 2011 at 11:47 AM

Inna,

Worked like a charm. When I first implemented it like that I got an error everytime on the rendering of the postback. That's why I reverted to the javascript solution above.
After your latest post saying you reproduced the scenario succesfully I knew something on my end had to be wrong.
So after debugging thoroughly the inner exception revealed the error was due to setting other attributes before (i.e. ...you cannot set an attribute when....).
Now I have implemented your solution with the input being a xsl:element which prevents the error. Not using javascript is much cleaner.
Thanx for that.

To be complete the code for the checkbox:

<xsl:element name="input">
   <xsl:attribute name="name">option</xsl:attribute>
   <xsl:attribute name="id">option</xsl:attribute>
   <xsl:attribute name="type">checkbox</xsl:attribute>
   <xsl:attribute name="title">The title</xsl:attribute>
   <xsl:attribute name="class">classname</xsl:attribute>
   <xsl:attribute name="value">true</xsl:attribute>
   <xsl:if test="$SendActionData[@Fieldname = 'option']/@Value = 'true'">
      <xsl:attribute name="checked">true</xsl:attribute>
   </xsl:if>
</xsl:element>

Again, thx alot.
mzZzl,
JamBo