logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$17
Dynamically Add More Fields To Form (Theme Options)

I have a theme options page, this one, and I have multiple fields for a single item/row with a mix of select and text fields.

Luis has given me a decent method below, however, it's not saving correctly. When I save the form, it will only save one set and also only save "Array" as the value. Here's the latest I have:

in my options.php

in my options-interface.php

attachment image View Attachment

Jon Stephen | 11/17/11 at 3:31pm Edit

Previous versions of this question: 11/17/11 at 10:05pm | 11/19/11 at 6:16pm | 11/22/11 at 5:31pm | 11/22/11 at 7:21pm | 11/22/11 at 7:21pm | 11/23/11 at 12:05am | 11/23/11 at 12:33am | 11/23/11 at 12:33am | 11/27/11 at 12:27am | 12/01/11 at 3:39pm | 12/03/11 at 2:42pm | 12/03/11 at 2:42pm | 12/03/11 at 2:42pm | 12/03/11 at 2:43pm | 12/05/11 at 12:20pm | 12/07/11 at 12:16pm | 12/07/11 at 5:23pm

The experts have suggested, on average, a prize of $20 for this question.

(4) Possible Answers Submitted...

Note: Jon Stephen felt their question was unanswered, so we granted them a refund.

Note: Jon Stephen requested a refund. They offered this explanation:

"not getting a working answer. if it doesn't work, it's not solved."

If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    11/17/11
    3:50pm
    Luis Abarca says:

    Yep, i done something similar before

    Check this answer amigo

    http://wpquestions.com/question/showLoggedIn/id/3170

    And here is the code

    Form and JS
    http://pastebin.com/CTmPyGRc

    In this case the save_post hook, but you can get the idea of how to get the values and store in the DB.
    http://pastebin.com/cVwGt77L

    Previous versions of this answer: 11/17/11 at 3:50pm

    • 11/17/11 3:56pm

      Luis Abarca says:

      As my amigo Jevusi said, you can use and adjust the code to your needs

    • 11/17/11 4:07pm

      Jon Stephen says:

      But how do I make it work with that exact options panel. I need to have also both a SELECT and TEXT field like my image. I also can't figure out how to make it save. So, if I have...

      array( "name" => "Item Name",
      "desc" => "blah blah blah",
      "id" => $shortname."_itemname",
      "type" => "multifield",
      "std" => ""),

      and then
      "case: multifield"


      How does that work?

    • 11/17/11 4:18pm

      Luis Abarca says:

      In the multifield you need to add something like this



      case 'multifiled':
      ?>
      <div class="rm_input rm_text">
      <label for="<?php echo $value['id']; ?>"><?php echo $value['name']; ?></label>
      <input name="<?php echo $value['id']; ?>[]" id="<?php echo $value['id']; ?>[]" type="text" value="<?php if ( get_settings( $value['id'] ) != "") { echo stripslashes(get_settings( $value['id']) ); } else { echo $value['std']; } ?>" />
      <small><?php echo $value['desc']; ?></small><div class="clearfix"></div>
      </div>

      <div id="placeholder"></div>
      <?php
      break;



      The las DIV is a placeholder to add new items when "+" or "Add" button is cliked.

      Then add the js in the footer.

    • 11/23/11 12:11am

      Luis Abarca says:

      I just got 3 emails about updates in this questions, but i can't see any changes

    • 11/23/11 12:13am

      Jon Stephen says:

      hmm, this is the updated question...

      I have a theme options page, this one, and I have multiple fields for a single item/row with a mix of select and text fields. My issue is I'm trying to have an add more and delete item buttons so that I can just add more instantly. See my image example. Any ideas how to make it work with my code below?


      case 'filmcredit':	

      $film_stored = $val;



      $output .= '<select class="of-typography of-typography-size" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmyear]' ) . '" id="' . esc_attr( $value['id'] . '_filmyear' ) . '">';

      for ($i = 2011; $i > 1949; $i--) {

      $filmyear = $i;

      $output .= '<option value="' . esc_attr( $filmyear ) . '" ' . selected( $film_stored['filmyear'], $filmyear, false ) . '>' . esc_html( $filmyear ) . '</option>';

      }

      $output .= '</select>';

      $theroles_array = array("" => "Role","Actor/Actress" => "Actor/Actress","Reality Participant" => "Reality Participant","Dancer" => "Dancer","Model" => "Model","Singer" => "Singer");

      $output .= '<select class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmrole]' ) . '" id="' . esc_attr( $value['id'] . '_filmrole' ) . '">';

      foreach ($theroles_array as $key => $theroles ) {

      $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $film_stored['filmrole'], $key, false ) . '>'. $theroles .'</option>';

      }

      $output .= '</select>';

      $output .= '<input id="' . esc_attr( $value['id'] . '_filmproduction' ) . '" class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmproduction]' ) . '" type="text" value="' . esc_attr( $film_stored['filmproduction']) . '" />';



      break;

    • 11/23/11 12:22am

      Luis Abarca says:

      Can you post the full theme ? to replicate it in my dev server ?

      I think you should raise the award a little bit.

    • 11/23/11 12:32am

      Jon Stephen says:

      I'm just simply using the options framework I have in the link and it's not dependent on a particular theme yet. That's why I just included the case that I created above which I know works and saves the data in the fields. So I'm only looking for the working bit that dynamically duplicates those fields correctly. I will raise the reward amount.

    • 11/23/11 1:56am

      Luis Abarca says:

      This is what i did.

      options-interface.php


      case 'filmcredit':
      $film_stored = $val;
      $output .= '<div id="filmtpl" style="display: none"><div class="item" style="margin: 20px 5px">';
      $output .= '<select class="of-typography of-typography-size" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmyear]' ) . '" id="' . esc_attr( $value['id'] . '_filmyear' ) . '">';

      for ($i = 2011; $i > 1949; $i--) {
      $filmyear = $i;
      $output .= '<option value="' . esc_attr( $filmyear ) . '" ' . selected( $film_stored['filmyear'], $filmyear, false ) . '>' . esc_html( $filmyear ) . '</option>';
      }

      $output .= '</select>';

      $theroles_array = array("" => "Role","Actor/Actress" => "Actor/Actress","Reality Participant" => "Reality Participant","Dancer" => "Dancer","Model" => "Model","Singer" => "Singer");

      $output .= '<select style="width: 200px" class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmrole]' ) . '" id="' . esc_attr( $value['id'] . '_filmrole' ) . '">';

      foreach ($theroles_array as $key => $theroles ) {
      $output .= '<option value="' . esc_attr( $key ) . '" ' . selected( $film_stored['filmrole'], $key, false ) . '>'. $theroles .'</option>';
      }

      $output .= '</select>';
      $output .= '<input style="width: 290px" id="' . esc_attr( $value['id'] . '_filmproduction' ) . '" class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmproduction]' ) . '" type="text" value="' . esc_attr( $film_stored['filmproduction']) . '" />';
      $output .= '<div class="commands" style="display: inline"><a rel="delete" class="button">-</a> <a rel="add" class="button">+</a></div>';
      $output .= '</div></div>';
      $output .= '<div id="filmplaceholder"></div>';

      break;



      in options.php in your theme

      add_action('admin_footer-appearance_page_options-framework', 'add_admin_footer');

      function add_admin_footer()
      {
      ?>
      <script>
      (function($)
      {
      var lines = 0;
      var template = 0;

      function items_init()
      {
      <?php $data = ''; // get the stored data ?>

      <?php if ( empty($data) ) : ?>
      items_add();
      <?php else: ?>

      <?php
      // Show stored records
      foreach ($data as $item) : ?>
      items_add({
      title: '<?php echo $item['testflight_filmproduction'] ?>',
      });
      <?php endforeach ?>
      <?php endif ?>

      // Delete the "-" button in first row
      $('#filmplaceholder .item .commands a[rel="delete"]').remove();
      }

      function items_add()
      {
      $('#filmtpl .item').clone().appendTo('#filmplaceholder');

      lines++;

      if (arguments.length > 0) {
      options = arguments[0];

      $('.testflight_filmproduction', obj).val( options.title ); // to fill values
      }
      }

      $('#filmplaceholder').delegate('.commands a', 'click', function()
      {
      var action = $(this).attr('rel');
      var confirm_delete = true;

      // Add action
      if ('add' == action) {
      items_add();
      }

      // Delete action
      if ('delete' == action) {
      // La TR en la tabla
      var oTr = $(this).parent().parent();


      if ( !confirm('Are you sure you want to delete this item ?') ) {
      confirm_delete = false;
      }

      if (confirm_delete) {
      oTr.remove();
      lines--;
      }
      }
      });

      $(document).ready(function()
      {
      items_init();
      });

      })(jQuery);

      </script>
      <?php
      }


      function optionsframework_options() {

      // If using image radio buttons, define a directory path
      $imagepath = get_bloginfo('template_url') . '/images/';

      // Options array
      $options = array();

      $options[] = array( "name" => __('General Settings','portfoliopress'),
      "type" => "heading");

      $options[] = array( "name" => __('Custom multifield','portfoliopress'),
      "desc" => __('Test Flight.','portfoliopress'),
      "id" => "testflight",
      "type" => "filmcredit");

      ....
      } // end function


      Notice: i using the Portfolio Press 0.8.3 by Devin Price

    • 11/23/11 2:25pm

      Jon Stephen says:

      Thanks Luis. This is looking good. However it's only saving one of the new groups. How can it increment the ids and names to save them?

    • 11/24/11 3:10pm

      Luis Abarca says:

      You need to change this code


      $output .= '<input style="width: 290px" id="' . esc_attr( $value['id'] . '_filmproduction' ) . '" class="of-input" name="' . esc_attr( $option_name . '[' . $value['id'] . '][filmproduction]' ) . '" type="text" value="' . esc_attr( $film_stored['filmproduction']) . '" />';


      To something like this

      $id = esc_attr( $value['id'] . '_filmproduction[]' );
      $name = esc_attr( $option_name . '_' . $value['id'] . '_filmproduction[]' );

      $output .= '<input style="width: 290px" id="' . $id . '" class="of-input" name="' . $name . '" type="text" value="'" />';


      This way, you will recive the data in an array.

      When you have data already stored in your post meta, you can fill the fields with javascript with add_item()

    • 12/01/11 8:59pm

      Jon Stephen says:

      hmm, I just can't get it to work. Am I missing something?

  • avatar
    Last edited:
    11/17/11
    3:49pm
    jevusi says:

    Use the code i have BUY
    http://wpquestions.com/question/showLoggedIn/id/3170

  • avatar
    Last edited:
    11/17/11
    3:54pm
    John Cotton says:

    You could do it javascript...


    jQuery('#add-button').click( function() {
    var rows_count = $('#rows> div').size();

    $('#rows').append( 'WHATEVER HTML YOU WANT HERE' );

    });


    The key thing with the html you add is to have pseudo-array names for the input fields ie

    <input type="text" name="title[1]" />


    That way, when you post it back to the server, you will get an array in S_POST['title'] for each entry.

    You could figure out what's new and old from a hidden input value or (since you're giving them the option to delete as well) just take what you get each time and overwrite whatever is in the database.

  • avatar
    Last edited:
    12/02/11
    1:35am
    Julio Potier says:

    This can be done using a simple jQuery function, one like John Cotton did.
    And same thing for delete a row.
    I can do this with FTP account and source.

This question has expired.





Current status of this question: Refunded



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.