Forums

Dynamic fields not saving to database/causing issues

missnene 18 Feb, 2011
I'm fairly new at this, so please be kind.🙂 I can't seem to get any of the following to save to the database table:

check1[]
child_more[]
age_more[]

I followed the post http://www.chronoengine.com/forums.html?cont=posts&f=5&t=15219&hilit=dynamically+add+new+field&start=15 and to get my email form to work properly I set "ChronoForms handle my posted arrays" to No.
Tried setting it back to yes to get the data to save, but to no avail. At one point it wouldn't save at all, can't remember the error I got, and once my template disappeared after pressing the submit button.

Maybe someone out there can see where I've gone terribly wrong.😢

Form HTML:
<div class="form_item">
  <div class="form_element cf_checkbox">
    <label class="cf_label" style="width: 150px;">Grade(s):</label>
    <div class="float_left">
      <input value="Pre-K" title="" class="radio validate-one-required" id="check10" name="check1[]" type="checkbox" />
      <label for="check10" class="check_label">Pre-K</label>
      <br />
      
<input value="K-2nd" title="" class="radio validate-one-required" id="check11" name="check1[]" type="checkbox" />
      <label for="check11" class="check_label">K-2nd</label>
      <br />
      
<input value="3rd-5th" title="" class="radio validate-one-required" id="check12" name="check1[]" type="checkbox" />
      <label for="check12" class="check_label">3rd-5th</label>
      <br />
      
<input value="6th-8th" title="" class="radio validate-one-required" id="check13" name="check1[]" type="checkbox" />
      <label for="check13" class="check_label">6th-8th</label>
      <br />
      
<input value="9th-12th" title="" class="radio validate-one-required" id="check14" name="check1[]" type="checkbox" />
      <label for="check14" class="check_label">9th-12th</label>
      <br />
      

    </div>
    
  </div>
  <div class="cfclear"> </div>
</div>

   <!--start first block (visible) -->

    <div class="form_item">
      <div class="form_element cf_textbox">
          <label class="cf_label" style="float:left;">Child's Name:</label>
          <input class="cf_inputbox required validate-alpha" style="float:left" maxlength="150" size="20" title="" name="child" id="child"  type="text" />
          <label class="cf_label" style="float:left;padding-left:40px;">Age:</label>
          <input class="cf_inputbox required validate-number" style="float:left" maxlength="150" size="20" title="" name="age" id="age"  type="text" />
       </div>
       <div class="cfclear"> </div>
    </div>

    <!--end first block (visible) -->

    <!-- start hidden block -->

    <div id="readroot" style="display: none;">
    <div class="form_item">
       <div class="form_element cf_textbox">
          <label class="cf_label" style="float:left;">Child's Name:</label>
          <input class="cf_inputbox" style="float:left" maxlength="150" size="20" id="child_more" name="child_more[]" type="text" />         
          <label class="cf_label" style="float:left;padding-left:40px;">Age:</label>
           <input class="cf_inputbox" style="float:left" maxlength="150" size="20" id="age_more" name="age_more[]" type="text" />
           <input class="img_x" title="Click to remove this child." type="button" style="color:#FF0000;font-size:14px;font-weight:bold;margin-left:40px;" value=" X " onclick="deleteFormBlock(this.parentNode.parentNode.parentNode.parentNode);" />
       </div>
       <div class="cfclear"> </div>
    </div>
    </div>

    <!--end of hidden block -->

    <!-- start placeholder for added blocks -->

    <span id="writeroot"></span>

    <!-- end placeholder for added blocks -->

    <div class="form_item">
       <div class="form_element">
          <input type="button"  title="Click to add a new child. *You MUST have Java Script enabled!*" style="color:#33CC00;font-size:14px;font-weight:bold;" value=" Add Child + "  onclick="addFormBlock()" />
       </div>
    </div>


Form JavaScript:
var counter = 0;

function addFormBlock() {
   counter++;
   var newBlockId ='newBlock' + counter;
   var newFields = $('readroot').clone();
   newFields.setProperty('id', 'newBlockId');
   newFields.setStyle('display', 'none');
   newFields.getChildren().each(function(newField){
      var theName = newField.getProperty('name');
      var theId = newField.getProperty('id');
      if (theName){
         newField.setProperty('name', theName + '[]');
         newField.setProperty('id', theId + counter);
      }
   });
   var insertHere = document.getElementById('writeroot');
   
   insertHere.parentNode.insertBefore(newFields,insertHere);   
   var addFormBlockSlider = new Fx.Slide( newFields, { duration: 500 } );
   addFormBlockSlider.hide();
   newFields.setStyle('display', 'block');
   addFormBlockSlider.slideIn();

}

function deleteFormBlock(obj) {
   

   var deleteFormBlockSlider = new Fx.Slide( obj, { duration: 500 } );
   
   deleteFormBlockSlider.slideOut().chain(function(){
      obj.remove(obj);
   });
   
}


Email template:
<div class="form_item">
  <div class="form_element cf_checkbox">
    <label class="cf_label" style="width: 150px;">Grade(s):</label>
    <div class="float_left">
      {check1}
      
      <br />
      

      
      <br />
      

      
      <br />
      

      
      <br />
      

      
      <br />
      

    </div>
    
  </div>
  <div class="cfclear"> </div>
</div>



    <!-- start placeholder for added blocks -->

    <span id="writeroot"></span>

    <!-- end placeholder for added blocks -->

	<!-- start results for Child -->

	<table style="border:0;">
	<?php // first print the static (visible) row ?>
	<tr>
	<td><b>Child Name: </b></td>
	<td style="padding-right:30px;">{child}</td>
	<td><b>Age: </b></td>
	<td>{age}</td>
	</tr>
	<?php

	//get the arrays from POST
	$child_more = JRequest::getVar('child_more', null, 'post');
	$age_more = JRequest::getVar('age_more', null, 'post');

	//loop through the player array and print it and the corresponding team
	foreach ($child_more as $key=>$value) {

   	// skip the hidden "source" row
   	if ($key != 0) {
      		// quick hack: add a divider
      		print "\n\n" . '<tr><td colspan="4" style="height:5px;border-bottom: 1px solid gray;margin-bottom: 5px;"> </td></tr>';
      		// print the info!
      		print "\n" . '<tr><td><b>Child Name:   </b></td><td style="padding-right:30px;">' . $value . '</td><td><b>Age:   </b></td><td>' . $age_more[$key] . '</td></tr>';
	   }

	}
	
	?>
	</table>

	<!-- end results for Child -->


Candace
GreyHead 18 Feb, 2011
Hi Candace,

Please turn debug on in the form General Tab, then submit the form and post the debug results here.

You'll usually need "ChronoForms handle my posted arrays" set to 'Yes' to save the results usefully in the database - otherwise you just get 'array' or nothing at all.

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