简体   繁体   中英

Submit form always copying over first row of ACF sub field

I've created a form which I'm trying to update the sub fields with the data using ACF's update_field function.

I have a repeater field with the sub fields - "time", "posted by" and "comment".

At the moment it's adding the data to the sub fields on submit fine... the problem is that it's always adding it to the first row. How can I make sure it adds a new row?

<form action="#" id="form" method="get">
  <input type="text" id="forum-comment" name="forum-comment">
  <input type="submit" name="submit" value="Submit">
</form>

<?php
// Check if form was submitted
$value = array();
if(isset($_GET['submit'])){
  $comment = $_GET['forum-comment'];
  $time = date('d/m/Y g:i a', time());

  global $current_user;
  get_currentuserinfo();

  // Comments
  $id = get_the_id();
  $field_key = "field_5c90d272c7ca9";

  $value[] = array(
    "time"          => $time,
    "posted_by"     => $current_user->ID,
    "comment"           => $comment,
  );

  update_field( $field_key, $value, $id );
}

If this helps anyone, pointed out by @04FS:

<?php
// Check if form was submitted
$value = array();
if( have_rows( 'comments' ) ):
  while( have_rows( 'comments' ) ): the_row();
    $time = get_sub_field( 'time' );
    $posted = get_sub_field( 'posted_by' );
    $comment = get_sub_field( 'comment' );

    $value[] = array(
      "time"        => $time,
      "posted_by"       => $posted,
      "comment"         => $comment,
    );

  endwhile;
endif;

if(isset($_GET['submit'])){
  $comment = $_GET['forum-comment'];
  $time = date('d/m/Y g:i a', time());

  global $current_user;
  get_currentuserinfo();

  // Comments
  $id = get_the_id();
  $field_key = "field_5c90d272c7ca9";

  $value[] = array(
    "time"          => $time,
    "posted_by"     => $current_user->ID,
    "comment"           => $comment,
  );

  update_field( $field_key, $value, $id );
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM