Posted on by

Showing a Record Edit Form Based on a Value in the Record

When using Participants Database to represent two or more classes of people or things, you sometimes need to customize the frontend edit form so that is only shows the fields that are relevant to the type of record that is getting edited. We can easily do this with a custom template.

If you’re not familiar with the use of custom templates, read this article: Using Participants Database Custom Templates

For our example, we are continuing the example used in “Multiple Databases” with Participants Database where you have a kid’s sports team and you have records for the players and the parents. We want to show a different record edit form for each of the two classes of people.

When someone wants to edit their record, they will typically use either a “private link” or a login page provided by the Particpant Login add-on. The way this works, when they go to their edit record page, we use a custom template to determine what kind of record edit form to show, and then the template invokes the record edit form with the desired parameters. It’s a template that uses shortcodes, as you will see.

As in the example, you have your fields organized into three groups: “contact”, “player” and “parent”. The “contact” group is used for all records and the other two are specific to the type of person. There is a field named “type” that tells us which type the record is for, and we use that to determine which set of field groups to show.

Here is a custom template that does that, it is named pdb-record-usertype.php

To use the template, use this shortcode on your record edit page:

No record was found.

That’s it! Of course, you’ll need to customize this to your particular situation, but that is the general technique.

43 thoughts on “Showing a Record Edit Form Based on a Value in the Record

  1. Hello, first I want to tell you that your plugin is excellent !!

    I have a query, I am making a map with tables and chairs in which the user can choose their table, for now I do it with [pdb_record fields = table] where a field appears to complete with the chosen table number. What I want to achieve is instead of the field to fill in to get a “sit here” button that adds the data without putting the number. This is possible?
    thank you very much in advance and
    pd: how beautiful is the place where you live, someday I will go!

    1. This sort of thing is possible, but it will certainly require some javascript skills to manage the user’s interaction with your graphical interface. You can easily use a Custom Template to set up the display of your graphical interface and also embed the needed javascript. The actual data should probably go into a hidden field. This field would be updated with the table number when the user clicks.

      Also, you may need some way to make some tables unavailable if they are full.

      1. there is a shortcode that changes a value, example [pdb_record set_field = "table = 2"] ?
        thanks for your answer!

        1. No, you would need to do something like that in a custom template.

  2. Hi,

    I used the code example from your article and edited it. This is my version:

    participant_values[‘form_type’];
    /*
     * now, show the record edit form for that type
     */
    switch ( $form_type ) {
       case ‘won’:
          echo do_shortcode(‘[pdb_record groups="main,wot1,wot2,wot3,wot4,wot5,wot6,wot7,wot8,wot9,wot10,wot11"]‘);
          break;
       case ‘tes’:
          echo do_shortcode(‘[pdb_record groups="main,testimonies"]‘);
          break;
       case ‘pdb_images’:
          echo do_shortcode(‘[pdb_record groups="main,image_uploading"]‘);
          break;
    }

    I’m not sure what else I need to do. I am assuming that $form_type refers to the database field name that holds that information, which is form_type. The values in that field are either ‘won’, ‘tes’ or ‘pdb_images’.

    Can you see any errors in how I have edited it? I’m no coder, so I am dabbling blindly here!

    1. You’re right, the first line of the code in the example (you got the syntax wrong there) is to get a specific value from the current record. The word in quotes is the name of the field you want to get the value from. Using your code, the name of the field you’re looking at is “form_type” so the line should be:

      $form_type = $this->participant_values[‘form_type’];
  3. HI Roland,

    Would this work if I had set up my database to work with WordPress users?

    I have followed the instructions and got so far, but I get the following error:

    Notice: Undefined variable: form_type in /var/www/new.kresy-siberia.org/public_html/wp-content/participants-database-templates/pdb-record-usertype.php on line 11

    Notice: Undefined variable: form_type in /var/www/new.kresy-siberia.org/public_html/wp-content/participants-database-templates/pdb-record-usertype.php on line 14

    Notice: Undefined variable: form_type in /var/www/new.kresy-siberia.org/public_html/wp-content/participants-database-templates/pdb-record-usertype.php on line 17

    Those errors relate to the page names that are set as the form type. Checking the database I can see that the values are correct in there.

    Reading on one of the previous comments here, I see that you previously stated “The page must be accessed with a URL that includes the private ID.”. My page is accessed via the record slug. Could this be an issue?

    Shelley

    1. I have another article that goes into detail about working with WordPress users: Participants Database and WordPress Users

      I can’t comment on the issues you’re having without seeing the code, but it looks like you are not assigning the $form_type variable correctly.

Leave a Reply
You have to agree to the comment policy.

43 thoughts on “Showing a Record Edit Form Based on a Value in the Record

  1. Hello, first I want to tell you that your plugin is excellent !!

    I have a query, I am making a map with tables and chairs in which the user can choose their table, for now I do it with [pdb_record fields = table] where a field appears to complete with the chosen table number. What I want to achieve is instead of the field to fill in to get a “sit here” button that adds the data without putting the number. This is possible?
    thank you very much in advance and
    pd: how beautiful is the place where you live, someday I will go!

    1. This sort of thing is possible, but it will certainly require some javascript skills to manage the user’s interaction with your graphical interface. You can easily use a Custom Template to set up the display of your graphical interface and also embed the needed javascript. The actual data should probably go into a hidden field. This field would be updated with the table number when the user clicks.

      Also, you may need some way to make some tables unavailable if they are full.

      1. there is a shortcode that changes a value, example [pdb_record set_field = "table = 2"] ?
        thanks for your answer!

        1. No, you would need to do something like that in a custom template.

  2. Hi,

    I used the code example from your article and edited it. This is my version:

    participant_values[‘form_type’];
    /*
     * now, show the record edit form for that type
     */
    switch ( $form_type ) {
       case ‘won’:
          echo do_shortcode(‘[pdb_record groups="main,wot1,wot2,wot3,wot4,wot5,wot6,wot7,wot8,wot9,wot10,wot11"]‘);
          break;
       case ‘tes’:
          echo do_shortcode(‘[pdb_record groups="main,testimonies"]‘);
          break;
       case ‘pdb_images’:
          echo do_shortcode(‘[pdb_record groups="main,image_uploading"]‘);
          break;
    }

    I’m not sure what else I need to do. I am assuming that $form_type refers to the database field name that holds that information, which is form_type. The values in that field are either ‘won’, ‘tes’ or ‘pdb_images’.

    Can you see any errors in how I have edited it? I’m no coder, so I am dabbling blindly here!

    1. You’re right, the first line of the code in the example (you got the syntax wrong there) is to get a specific value from the current record. The word in quotes is the name of the field you want to get the value from. Using your code, the name of the field you’re looking at is “form_type” so the line should be:

      $form_type = $this->participant_values[‘form_type’];
  3. HI Roland,

    Would this work if I had set up my database to work with WordPress users?

    I have followed the instructions and got so far, but I get the following error:

    Notice: Undefined variable: form_type in /var/www/new.kresy-siberia.org/public_html/wp-content/participants-database-templates/pdb-record-usertype.php on line 11

    Notice: Undefined variable: form_type in /var/www/new.kresy-siberia.org/public_html/wp-content/participants-database-templates/pdb-record-usertype.php on line 14

    Notice: Undefined variable: form_type in /var/www/new.kresy-siberia.org/public_html/wp-content/participants-database-templates/pdb-record-usertype.php on line 17

    Those errors relate to the page names that are set as the form type. Checking the database I can see that the values are correct in there.

    Reading on one of the previous comments here, I see that you previously stated “The page must be accessed with a URL that includes the private ID.”. My page is accessed via the record slug. Could this be an issue?

    Shelley

    1. I have another article that goes into detail about working with WordPress users: Participants Database and WordPress Users

      I can’t comment on the issues you’re having without seeing the code, but it looks like you are not assigning the $form_type variable correctly.

Leave a Reply
You have to agree to the comment policy.