Home Forums ACF PRO Adding Repeater Rows on PHP POST


Adding Repeater Rows on PHP POST

  • I’m using jQuery to add items to a form, I’m then using the form to create a new post when it is submitted. I’ve been able to link ACF fields correctly except the repeater will only show the most recent item added.

    This is the code i’m using

      if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "new_post") {
        // Add the content of the form to $post as an array
        $title = date('m-d-Y_g:i:s');
        $new_post = array(
          'post_title'    => $title,
          'post_status'   => 'publish',
          'post_type'     => 'order'
        // Save the new post
        $pid = wp_insert_post($new_post);
        // Repeater
        $fieldSets = (isset($_GET['line-item']) ? $_GET['line-item'] : null);
        foreach((array)$fieldSets as $lineItem) {
          // Repeater
          $rid = 'field_586548ae64a78';
          $value = array(
      			"product" => $_POST['ID'],
            "ppu" => $_POST['ppu'],
            "container" => $_POST['container'],
            "quantity" => $_POST['quantity']
          update_field( $rid, $value, $pid );
        // Total
        $total = $_POST['ppu'] * $_POST['quantity'];
        update_field('field_586a9fa58fe98', $total, $pid);
        // Redirect
        wp_redirect(get_permalink($pid)); exit;

    And here is an example of a form that would be submitted, when it creates the new post, only the highest fieldset is included, rather than a repeater row per fieldset.

    <form id="new_post" name="new_post" method="post" action="">
        <input type="submit" value="Submit Order" tabindex="6" id="submit" name="submit" />
        <input type="hidden" name="action" value="new_post" /> <input type="hidden" id=
        "_wpnonce" name="_wpnonce" value="569eadeb3e" /><input type="hidden" name=
        "_wp_http_referer" value="/p/b/new-order/" />
        <fieldset name="line-item">
          <input type="text" id="59-ID" value="59" name="ID" /><input type="text" id=
          "3.52-PPU" value="3.52" name="ppu" /><input type="text" id="4-CONTAINER" value="4"
          name="container" /><input type="text" id="2-QUANTITY" value="2" name="quantity" />
        <fieldset name="line-item">
          <input type="text" id="44-ID" value="44" name="ID" /><input type="text" id=
          "3.02-PPU" value="3.02" name="ppu" /><input type="text" id="4-CONTAINER" value="4"
          name="container" /><input type="text" id="5-QUANTITY" value="5" name="quantity" />

    Any ideas?

  • Try add_row instead of update_field

    update field requires that you set all of the rows, if you only set one row then this replaces what was already there.

  • Thank you John, using add_row has the same result, one repeater row being added. I’m using the exact same examples as above except add_row instead of update_field I’m thinking it has something to do with my foreach loop trying to loop through the fieldsets.. but I can’t pin it down.

  • You have text fields that have the same name. When the form is input, only the last field with that field name will be submitted. You can see this if you print out the value of $_POST

    echo '<pre>'; print_r($_POST); echo '</pre>';

    If you want to have multiple fields with the same name then you need to set up arrays to handle them. For example:

    <input type="text" name='rows[0][ppu]' />
    <input type="text" name='rows[1][ppu]' />
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Adding Repeater Rows on PHP POST’ is closed to new replies.