We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16633
    • 116 Posts
    Hey I am using Revo and formit2db trying to insert some info into my custom table. I have a row of form fields that are an array because they can be added to.

    <tr>
    <td valign="middle"><input type="text" name="attendee_salutation[]:required" value="" /></td>
    <td valign="middle"><input type="text" name="attendee_firstname[]:required" value="" /></td>
    <td valign="middle"><input type="text" name="attendee_lastname[]:required" value="" /></td>
    <td valign="middle"><input type="text" name="attendee_email[]:required:email" value="" /></td>
    <td valign="middle"><span class="remove">Remove</span></td>
    </tr>

    They are adding "attendees". So if they click to add one, this row is cloned. So each value is an array and I have to loop through them and insert them in the formit2db snippet. I cannot get this to work however. When simply having them not as an array and as static fields everything works fine. I have tried the follow code and it is not working.

    $len = count($_POST[’attendee_firstname’]);
    for ($i=0; $i < $len; $i++)
    {
    // Get values from form
    $attendee_salutation[] = $_POST[’attendee_salutation’][$i];
    $attendee_firstname[] = $_POST[’attendee_firstname’][$i];
    $attendee_lastname[] = $_POST[’attendee_lastname’][$i];
    $attendee_email[] = $_POST[’attendee_email’][$i];

    // Insert attendee into db
    $attendee_dataobject->set(’attendee_salutation’, $attendee_salutation[$i]);
    $attendee_dataobject->set(’attendee_firstname’, $attendee_firstname[$i]);
    $attendee_dataobject->set(’attendee_lastname’, $attendee_lastname[$i]);
    $attendee_dataobject->set(’attendee_email’, $attendee_email[$i]);
    $attendee_dataobject->set(’event_id’, $event_id); // Taken from the ID of event resource
    $attendee_dataobject->set(’billing_id’, $billing_id); // Taken from the last inserted billing info.
    $attendee_dataobject->save();
    }

    also tried

    $allFormFields = $hook->getValues();
    foreach ($allFormFields as $field => $value)
    {
    if ($field !== ’spam’ && $field !== ’resource_id’)
    {
    $attendee_dataobject->set($field,$value);
    }
    }

    Any help would be great.
      --
      Landon Poburan
      Web Designer and Developer

      http://www.categorycode.ca
      http://www.landonp.com
      • 16633
      • 116 Posts
      I will add that with the for loop, it worked but say there were 4 records to insert (looping through 4 times) it would just insert the last record. But the foreach didn’t work at all.
        --
        Landon Poburan
        Web Designer and Developer

        http://www.categorycode.ca
        http://www.landonp.com
        • 4172
        • 5,888 Posts
        I don’t know if that is what you need and I didn’t try it but perhaps something like this will work:

        <?php
        
        $allFormFields = $hook->getValues(); 
        $arrayfields=array();
        $fields=array();   
        foreach ($allFormFields as $field=>$value){
            if ($field !== 'spam' && $field !== 'resource_id'){
                if (is_array($field)){
                	foreach ($field as $key=>$value){
                		$arrayfields[$key][$field]=$value;
                	}
                }
        	    else{
        	    	$fields[]=$value;
        	    }
            }       
        }
        
        //if posted fields were arrays
        if (count($arrayfields)>0){
        	foreach ($arrayfields as $afields){
        	    $dataobject = $modx->newObject($classname);
        		foreach ($afields as $field=>$value){
                    $dataobject->set($field,$value);
                }
        		$dataobject->save(); 
            }
        }
        //if posted fields were normal fields
        elseif(count($fields)>0){
            $dataobject = $modx->newObject($classname);
        	foreach ($fields as $field=>$value){
                 $dataobject->set($field,$value);
            }
        	$dataobject->save();	
        }
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
          • 3749
          • 24,544 Posts
          If you are creating new rows in a table, you definitely need $attendee_dataobject = $modx->newObject(’modSomething’) for each one.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 16633
            • 116 Posts
            Quote from: BobRay at Nov 03, 2010, 02:22 PM

            If you are creating new rows in a table, you definitely need $attendee_dataobject = $modx->newObject(’modSomething’) for each one.


            Thanks. That is actually the first thing I noticed looking at Bruno’s code. Then I took that and applied it to the code I had (for loop) and works like a charm. Issue now is if I say have 2 attendees so that form x 2. So there would be

            2 x attendee_firstname
            2 x attendee_lastname ... and so on

            Am I able to validate them on the front end? Because the code would be 2 of <input name="attendee_firstname[]" />
              --
              Landon Poburan
              Web Designer and Developer

              http://www.categorycode.ca
              http://www.landonp.com
              • 21025
              • 6 Posts
              Could any of you please clarify how you were able to get this working? I have a form made up of nothing but arrays that I’m desperately trying to save to a database. How/where exactly did you end up modifying the formit2db snippet? Thanks!!
                • 16633
                • 116 Posts
                Make sure as you loop through the array you take note of Bruno and BobRays comments related to:

                $dataobject = $modx->newObject($classname);

                You need this for each time you loop through the array to insert into the db.
                  --
                  Landon Poburan
                  Web Designer and Developer

                  http://www.categorycode.ca
                  http://www.landonp.com
                  • 21025
                  • 6 Posts
                  Thank you for the reply. Maybe I should have clarified than I’m PHP-impaired. Where exactly are you adding this code? Are you just adding it onto the end of the formit2db snippet, or replacing a part of it, or creating a new snippet..? huh
                    • 4172
                    • 5,888 Posts
                    your form?
                    your formit-call?
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 21025
                      • 6 Posts
                      Thank you so much for any help you can offer. Right now I’m using the same formit2db snippet as you originally posted. I tried modifying it with bits and pieces from this thread, but I’m not sure where to put everything. It’s successfully creating a folder under core/components, but not saving anything to the DB. I have the table and fields all created per your instructions.

                      formit2db code:
                      <?php
                      $prefix=$scriptProperties['prefix'];
                      $packageName = $scriptProperties['packageName'];
                      $tablename = $scriptProperties['tablename'];
                      
                      $packagepath = $modx->getOption('core_path') . 'components/'.$packageName.'/';
                      $modelpath = $packagepath.'model/';
                      
                      $modx->addPackage($packageName,$modelpath,$prefix);
                      $manager= $modx->getManager();
                      $generator= $manager->getGenerator();
                      $classname = $generator->getClassName($tablename);
                      
                      //$errorMsg = '<pre>'.print_r($scriptProperties['fields'],true).'</pre>';  
                      //$hook->addError('error_message',$errorMsg);  
                      
                      //$scriptProperties['hook']->formit->config['emailTo']='[email protected]';
                      $dataobject=$modx->getObject($classname,array('id'=>$hook->getValue('resource_id')));
                      
                      if (empty($dataobject)){
                          $dataobject = $modx->newObject($classname);
                          //$dataobject->set('createdby', $modx->user->get('id'));
                      }
                      else{
                          //$dataobject->set('editedby', $modx->user->get('id'));
                      }
                      if (!is_object($dataobject) || !($dataobject instanceof xPDOObject)) {
                          $errorMsg='Failed to create object of type: ' . $classname;
                          $hook->addError('error_message',$errorMsg); 
                          return false;
                      }
                      
                      $allFormFields = $hook->getValues();    
                      foreach ($allFormFields as $field=>$value){
                          
                          if ($field !== 'spam' && $field !== 'resource_id'){
                              $dataobject->set($field,$value);
                          }       
                      }
                      
                      $dataobject->save();
                      
                      $allFormFields = $hook->getValues(); 
                      $arrayfields=array();
                      $fields=array();   
                      foreach ($allFormFields as $field=>$value){
                          if ($field !== 'spam' && $field !== 'resource_id'){
                              if (is_array($field)){
                              	foreach ($field as $key=>$value){
                              		$arrayfields[$key][$field]=$value;
                              	}
                              }
                      	    else{
                      	    	$fields[]=$value;
                      	    }
                          }       
                      }
                      
                      //if posted fields were arrays
                      if (count($arrayfields)>0){
                      	foreach ($arrayfields as $afields){
                      	    $dataobject = $modx->newObject($classname);
                      		foreach ($afields as $field=>$value){
                                  $dataobject->set($field,$value);
                              }
                      		$dataobject->save(); 
                          }
                      }
                      //if posted fields were normal fields
                      elseif(count($fields)>0){
                          $dataobject = $modx->newObject($classname);
                      	foreach ($fields as $field=>$value){
                               $dataobject->set($field,$value);
                          }
                      	$dataobject->save();	
                      }
                      
                      return true;


                      Formit Call and HTML:
                      [[!FormIt? &hooks=`spam,formit2db,redirect` &preHooks=`db2formit` &prefix=`oasis_custom_` &packageName=`results` &tablename=`quiz` &redirectTo=`21` &validate=`one:required, two:required, three:required`]] 
                      <div class="quiz">
                      <form action="[[~[[*id]]]]" method="post">
                        <input name="resource_id" type="hidden" value="[[+fi.id]]">
                      <input name="nospam:blank" type="hidden">
                      <ul>
                      <li class="clearfix"><div><span class="num">1</span>How often do your eyes feel dry?</div>
                      <input type="hidden" name="one[]" value=""> 
                       <label class="label_radio"><input type="radio" id="1" name="one[]" value="Never" [[!+fi.one:FormItIsChecked=`Never`]]> Never</label>
                       <label class="label_radio"><input type="radio" id="2" name="one[]" value="Sometimes" [[!+fi.one:FormItIsChecked=`Sometimes`]]> Sometimes</label>
                       <label class="label_radio"><input type="radio" id="3" name="one[]" value="Mostly" [[!+fi.one:FormItIsChecked=`Mostly`]]> Most of the time</label>
                       <label class="label_radio"><input type="radio" id="4" name="one[]" value="Always" [[!+fi.one:FormItIsChecked=`Always`]]> Always</label>
                       <span class="error">[[!+fi.error.one]]</span>
                      </li>
                      
                      <li class="clearfix"><div><span class="num">2</span>How often do your eyes feel gritty or as if there is 'something' in your eye?</div>
                      <input type="hidden" name="two[]" value=""> 
                       <label class="label_radio" for="two[]"><input type="radio" id="5" name="two[]" value="Never" [[!+fi.two:FormItIsChecked=`Never`]]> Never</label>
                       <label class="label_radio" for="two[]"><input type="radio" id="6" name="two[]" value="Sometimes" [[!+fi.two:FormItIsChecked=`Sometimes`]]> Sometimes</label>
                       <label class="label_radio" for="two[]"><input type="radio" id="7" name="two[]" value="Mostly" [[!+fi.two:FormItIsChecked=`Mostly`]]> Most of the time</label>
                       <label class="label_radio" for="two[]"><input type="radio" id="8" name="two[]" value="Always" [[!+fi.two:FormItIsChecked=`Always`]]> Always</label>
                        <span class="error">[[!+fi.error.two]]</span>
                      </li>
                      
                      <li class="clearfix"><div><span class="num">3</span>How often do your eyes feel painful, sore, or burning?</div>
                      <input type="hidden" name="three[]" value=""> 
                       <label class="label_radio" for="three[]"><input type="radio" id="9" name="three[]" value="Never" [[!+fi.three:FormItIsChecked=`Never`]]> Never</label>
                       <label class="label_radio" for="three[]"><input type="radio" id="10" name="three[]" value="Sometimes" [[!+fi.three:FormItIsChecked=`Sometimes`]]> Sometimes</label>
                       <label class="label_radio" for="three[]"><input type="radio" id="11" name="three[]" value="Mostly" [[!+fi.three:FormItIsChecked=`Mostly`]]> Most of the time</label>
                       <label class="label_radio" for="three[]"><input type="radio" id="12" name="three[]" value="Always" [[!+fi.three:FormItIsChecked=`Always`]]> Always</label>
                       <span class="error"> [[!+fi.error.three]]</span>
                      </li>
                      <input type="submit" name="go" value="See Results" class="button right">
                      </ul>
                      </form>


                      Again, thank you for your help!