Forums

Dynamic added fields

NoName61 26 Jan, 2012
Hi there,

I have Chronoforms installed on Joomla. In my form, I manually added a "select" which calls a JS function, defined in "Form Javascript" section, like below. This function repeats a field, based on select value.

<select name="ngroups" id="nGroups" onchange="addGroups()">
  <option value=0>-choose-</option>
  <option value=1>1 Groups</option>
  <option value=2>2 Groups</option>
</select>


Js:

function addGroups(){
var x=jQuery("#nGroups").val();
if (x>0) {
  shtml='';
  for (i=0;i<x;i++) {					
    shtml+='<hr><div class="form_item"><div class="form_element cf_textbox"><label class="cf_label" style="width: 150px;">Name:</label><input class="cf_inputbox" maxlength="150" size="30" title="" id="text_22" name="name'+i+'" type="text" /></div><div class="cfclear"> </div></div><br />';
    }	
    jQuery('#target-div').html(shtml);
  }
}


This works fine. Fields are added as I choose select option.

The problem is when sending the email. With this changes, emails are not getting out.

Any clues?

Thanks in advance.

Regards,

Rui
NoName61 30 Jan, 2012
I need a answer as soon as possible, please!


Regards,

Rui
GreyHead 30 Jan, 2012
Hi Rui,

There isn't enough information here to give you any kind of useful answer. Assuming that your script works correctly then it shouldn't have any affect on the emails.

Please drag a Debugger action into the On Submit event, then submit the form and post the debug - including the 'dummy emails' results here. Note: if you are using the Easy Wizard you may need to switch to the Advanced Wizard to do this; if you want to continue to use the Easy Wizard please make a copy of your form first and add the Debugger action to the copy.

Bob
NoName61 31 Jan, 2012
Hi Bob,

First of all, thank you for your answer.

Below is the debug info, after filling and submiting the form. The email is sent, but the dynamically generated fields are not sent correctly:

Form passed first SPAM check OK
Form passed the submissions limit (if enabled) OK
Form passed the Image verification (if enabled) OK
Form passed the server side validation (if enabled) OK
$_POST Array: Array ( [data] => 25/01/2012 [horachegada] => 12:00 [horapartida] => 18:30 [NomeEscola] => Escola do Porto [localidade] => Porto [concelho] => Porto [distrito] => Porto [responsavel] => José [telef] => 123452 [tlm] => 919191919 [mail] => saladapt.russo@gmail.com [ngrupos] => 2 [educador0] => José [sala0] => 4º Ano [numcriancas0] => 25 crianças [educador1] => Antonio [sala1] => 3º Ano [numcriancas1] => 50 crianças [programa] => Visita 3 - Missão feijão - Pré [check0] => Array ( [0] => Tarde ) [criancasespeciais] => ads [radio1] => Não [radio2] => Sim [radio3] => Sim [obs] => ADSJA [button_20] => Submeter [b675109d2c302698f1c6f1fccb4f210d] => 1 [1cf1] => 06ab19308ccfb107ca360be6a2b13715 [chronoformname] => Ficha )
$_FILES Array: Array ( )
Form passed the plugins step (if enabled) OK
An email has been SENT successfully from (Utilizador)santos.ruimicael@gmail.com to santos.ruimicael@gmail.com
Debug End


[educador0] => José [sala0] => 4º Ano [numcriancas0] => 25 crianças [educador1] => Antonio [sala1] => 3º Ano [numcriancas1] => 50 crianças
These are the fields I'm talking about. In this case, I've choosen 2 groups in the select field, so it generated 2 groups of fields (XXX0 and XXX1).

Thanks in advance.

Regards,

Rui
GreyHead 31 Jan, 2012
Hi Rui,

Just confirming that this is ChronoForms 3.2?

Do you have {educador0}, {sala0}, {numcriancas0}, etc. in your Email template?

Bob
NoName61 02 Feb, 2012
Hi again.

Do you have {educador0}, {sala0}, {numcriancas0}, etc. in your Email template?



This fields are not in the email template. That's the problem. They are dinamically generated. So, if I choose 1 group, there are 3 fields ({educador0}, {sala0}, {numcriancas0}); If I choose 2 groups, there are 6 fields ({educador0}, {sala0}, {numcriancas0},{educador1}, {sala1}, {numcriancas1}); and so on...

So, I wonder if I can change email template (with PHP) so these fields, based on how much groups I've chosen, are dinamically generated.

Hope you have understood the idea..

Thanks in advance..

Rui
GreyHead 02 Feb, 2012
Hi Rui,

Well yes you have to do that. There's no other way that ChronoForms can know about dynamically generated inputs.

You don't have to use curly brackets though, you can write PHP to read the $form->data array and echo out the values that is finds there.

You can add PHP to the template provided that the rich text editor is off. Or you can put the PHP in a Custom Code action, build up a block of text/HTML to display these results, save them into the $form->data array as e.g. $form->data['dyn_results'] and then use {dyn_results} to show the whole block in the template.

Bob
NoName61 02 Feb, 2012
Thanks Bob.

If I change the email template, like this:
[#repeat]
{educadorX}
{salaX}
{numcriancasX}
[#repeat]


Then, before sending the email, when php is replacing the fields, if [#repeat] keyword is found, then I could make a change, based on the number of groups chosen (through the select input), insert a loop and repeat code, inserting fields and replacing X (above) with a counter.

To do this, I need to now where is this done, I mean, which file can I change. Can you tell me that, please?

Thanks in advance.

Regards,

Rui
GreyHead 02 Feb, 2012
Hi Rui,

You could do it by hacking the ChronoForms files . . . but it's a bad idea. Try the Custom code method from my post instead.

Bob
NoName61 02 Feb, 2012
Ok, understood.

When you say Custom Code, what do you mean? Where can I write that code, so it executes before template fields are prepared and email is sent? "Extra code", in Form Code tab, perhaps? Or, in the same tab, "onSubmit Events Code" section, "before sending email"?

Thanks.
GreyHead 03 Feb, 2012
Hi NoName61,

Sorry, I lost track of the version you are using. It would be the OnSubmit before Email box on the Form Code tab in CFv3.2

Or you can do it in the template itself if you turn the rich HTML editor off in the Email Setup Properties box.

Bob
NoName61 09 Feb, 2012
Hi Bob.

Finally, problem is solved. I added some php code before submiting the form, as you said.

Thank you for your help.

Best regards,

Rui
This topic is locked and no more replies can be posted.