• Resolved sondo

    (@sondo)


    Dear Xnau,
    First of all, thank you for a great plugin!
    I want to use this plugin for my website that collects infos from applicants (for scholarship).

    There is a field (named Avg) that its value is the average of 3 other fields (that an applicant fill in from [pdb-record] form).

    I want Avg be calculated right after an applicant press submit button and the value of Avg will display on [pfb-list]. the List page will rank applicants base on Avg field.

    As I understand from the forum, I should copy pdb-record-default.php from the template to my theme and then modify it. However, I don’t know where I have to put my code in (I’m just an “amateur” PHP coder). Could you please help me to solve this? I’m using PDB plugin Version 1.4.9.1 on WordPress 3.5.1.

    Thank you very much!
    Son

    https://www.ads-software.com/extend/plugins/participants-database/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author xnau webdesign

    (@xnau)

    Son,

    This kind of thing can be tricky because you have to calculate the new value before the data gets submitted to the server…but PHP is running on the server, so you actually have to do it before the form gets submitted to the server. The solution is to use Javascript.

    I can’t give you the exact script to use, but you attach a function to the “submit” event and change the value of you “avg” field before the form data goes to the server. Make sure you have the “avg” field set up so you have somewhere to put your value.

    Put something like this (you will need to modify it for it to work on your site) into the custom template for your [pdb_record] shortcode. This is pretty simple javascript, you should be able to just change the names in the selectors for it to work for you. It will give you a chance to start learning some Javascript…

    <script type="text/javascript">
    // this is the standard wrappper for jQuery functions in WP
    jQuery(document).ready(function($) {
       // attach a function to the submit button click
       $('.pdb-record form .pdb-submit').click(function ()
       {
          // calculate the average of three fields
          var average = (parseInt($('input[name=val1]').val()) + parseInt($('input[name=val2]').val()) + parseInt($('input[name=val3]').val())) / 3;
          // set the value of the avg field
          $('input[name=avg]').val(average);
          // now submit the data
          $('.pdb-record form').submit();
          // this is so the browser won't submit it also.
          return false;
       }
    });
    </script>
    Thread Starter sondo

    (@sondo)

    Dear Xnau,
    Thank you very much for your prompt reply!
    I tried to insert the above code (with some modifications) to my template. However, there is an error: “Uncaught ReferenceError: jQuery is not defined”.

    It seems I put it in wrong place. Where should I insert this script in the template file? I did try to insert it right before the section of $this->print_submit_button() and also at the end or the start of the page.

    I’m sorry for my stupid question!
    Thanks a lot!

    Plugin Author xnau webdesign

    (@xnau)

    No, not a stupid question…but puzzling. It seems your WP installation is not loading jQuery, which is a little unusual. We can fix that, though.

    Also– I notice I left an error in the code I posted. Here’s the code with the error fixed…it goes right after the first “?>” in the template at line 8 so you won’t have to wonder where it goes.

    <script type="text/javascript">
    // this is the standard wrappper for jQuery functions in WP
    jQuery(document).ready(function($) {
       // attach a function to the submit button click
       $('.pdb-record form .pdb-submit').click(function ()
       {
          // calculate the average of three fields
          var average = (parseInt($('input[name=val1]').val()) + parseInt($('input[name=val2]').val()) + parseInt($('input[name=val3]').val())) / 3;
          // set the value of the avg field
          $('input[name=avg]').val(average);
          // now submit the data
          $('.pdb-record form').submit();
          // this is so the browser won't submit it also.
          return false;
       });
    });
    </script>

    Now, to solve the bigger issue, you will have to edit one of the plugin files. On line 427 of the file “participants-database.php” is a function named “add_scripts()” There is a blank line on line 430. Put this in that blank line so that jQuery will be loaded:

    wp_enqueue_script('jquery');

    The function will now look like this:

    public function add_scripts() {
    
        if (false !== self::$shortcode_present) {
          wp_enqueue_script('jquery');
          wp_enqueue_script('pdb-shortcode');
          wp_enqueue_script('jq-placeholder');
        }
      }

    You’ll have to modify the javascript as before.

    Thread Starter sondo

    (@sondo)

    Dear Xnau,
    It works flawlessly!
    Actually, it doesn’t work when I inserted wp_enqueue_script(jquery); into the line 430.
    However, it works when I uncommented this command under the function include_script(), line 419
    Thank you very much for your support!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Update only one field’ is closed to new replies.