How to edit a submission using the original form

This tutorial will provide an example where, based on the logged in user, it pre-fills last submitted data in case the user wants to adjust certain information. You can download the sample form on the right for a finished example which can be restored using the built-in RSForm!Pro Backup/Restore feature (the sample form does not include the submission removal script - this can be simply added as instructed below).

Download Sample Form
Intro

Although normally you could use the Manage Directories feature, this doesn't provide a full form's functionality. Basically, if you have a simpler form without any custom added validations for example, you should use the Manage Directories feature instead to provide a frontend listing (that can be set even based on the logged in user, where users can adjust their submitted data).

The following steps explain what the form will do:

  • It firstly checks if a user is logged in, otherwise a message is displayed.
  • Queries are then performed to grab information about the form including submitted data on the logged in user.
  • A script then includes previous submitted data within the $val array from the "Script called before form is generated" area, that is designed to pre-fill fields.
  • Eventually, when the user re-submits the form, a new submission is made and the old one is removed through scripting (optional step).

Script

The following script is added within "Script called before form is generated" area (backend > Components > RSForm!Pro > Manage Forms > your form > Form Properties > PHP Scripts - Pre-Processing).

Most of the code is commented for a better understanding on what exactly is going on.

if(JFactory::getUser()->id == 0){
//if user is not logged in, the following message is displayed
  $formLayout = 'Please login in order to access the form.';
}else{
  $db = JFactory::getDBO();
  $db->setQuery("SELECT `SubmissionId` FROM #__rsform_submissions WHERE `UserId`='".JFactory::getUser()->id."' AND `FormId`='".$formId."' ORDER BY `SubmissionId` DESC LIMIT 1");
  $SubmissionId = $db->loadResult();
  //grabs previous user submission, if any.

  if(!empty($SubmissionId)){
    //this basically triggers if the user had submitted the form before.

    $exclude = implode("','",array(7,8,9,10,11,12,13,14,15,21,22,23,24,27,28,30,31,32,33,34,41,212,499,500,2424));
    /* certain form elements are EXCLUDED as follows:
    7 button
    8 captcha
    9 File Upload
    10 freeText
    11 hidden field
    12 image button
    13 submit button
    14 password
    15 support ticket
    21 single product
    22 multiple product
    23 total
    24 recaptcha
    27 choose payment
    28 donation
    30 RSEvents!Pro name
    31 RSEvents!Pro email
    32 RSEvents!Pro tickets
    33 RSEvents!Pro payments
    34 RSEvents!Pro coupon
    41 Page Break
    212 Google Map
    499 offline payment
    500 PayPal payment
    2424 reCaptcha V2
    */

    $db->setQuery("SELECT ct.`ComponentTypeName`, p.`PropertyValue`, sv.`FieldValue` FROM #__rsform_component_types AS ct LEFT JOIN #__rsform_components AS c ON ct.`ComponentTypeId` = c.`ComponentTypeId` LEFT JOIN #__rsform_properties AS p ON c.`ComponentId` = p.`ComponentId` LEFT JOIN #__rsform_submission_values AS sv ON  p.`PropertyValue` = sv.`FieldName` WHERE c.`FormId`='".$formId."' AND c.`ComponentTypeId` NOT IN ('".$exclude."') AND p.`PropertyName`='NAME' AND sv.`SubmissionId` = '".$SubmissionId."'");

    $results = $db->loadObjectList();
    //to keep things sort, the above query grabs previously submitted data of your form fields based on the logged in user.


    foreach($results as $result){
    //there are four main cases when you have to specify a particular syntax to include data within the $val variable that will eventually pre-fill form fields

      if($result->ComponentTypeName == 'selectList' || $result->ComponentTypeName == 'checkboxGroup'){
      //for dropdowns and checkbox groups
        if(strstr($result->FieldValue,"\n") !== false){
        //for multi selections
          $multiVals = explode("\n",$result->FieldValue);
          foreach($multiVals as $multiVal){
            $val[$result->PropertyValue][] = $multiVal;
          }
        }else{
        //for single selections
          $val[$result->PropertyValue] = $result->FieldValue;
        }
      }elseif($result->ComponentTypeName == 'calendar'){
      //for the calendar field
        $val[$result->PropertyValue] = $result->FieldValue;
        //if you're using other calendar formats than d-m-Y, you may need to use something like the following instead (adjust according to your date format):
        //$calDate = explode("/",$result->FieldValue);
        //$val[$result->PropertyValue] = $calDate[2].'/'.$calDate[0].'/'.$calDate[1];
      }elseif($result->ComponentTypeName == 'birthDay'){
      //for the birthday field
        $bDate = str_replace(' ','',$result->FieldValue);
        $bDate = explode("/",$bDate);
        $val[$result->PropertyValue]['d'] = $bDate[0];
        $val[$result->PropertyValue]['m'] = $bDate[1];
        $val[$result->PropertyValue]['y'] = $bDate[2];
      }else{
      //for other fields such as textboxes, radio groups and so on
        $val[$result->PropertyValue] = $result->FieldValue;
      }
    }
  }
}
Optional - removing the previous submission

The next script is added within "Scripts Called after form has been Processed" area (backend > Components > RSForm!Pro > Manage Forms > your form > Properties > PHP Scripts). This basically removes the previous submission after the new one is already saved and there's no need for any further adjustment.

This script is optional, unless you do want the previous submission to be deleted (this is permanent).

$db = JFactory::getDBO();
$db->setQuery("SELECT `SubmissionId` FROM #__rsform_submissions WHERE `UserId`='".JFactory::getUser()->id."' AND `FormId`='".$formId."' ORDER BY `SubmissionId` DESC LIMIT 2");
$oldSubId = '';
if(count($db->loadColumn()) == 2){
  $oldSubId = $db->loadColumn()[1];
}
if(!empty($oldSubId)){
  $db->setQuery("DELETE rs,rsv FROM #__rsform_submissions AS rs ,#__rsform_submission_values AS rsv WHERE
  rs.`SubmissionId`=rsv.`SubmissionId` AND rs.`SubmissionId`='".$oldSubId."'");
  $db->execute();
}

52 persons found this article helpful.


Was this article helpful?

Yes No
Sorry about that