custom field - frontend submit post jQuery clone row won't save

Why the cloned value won't save in DB ?It's working fine for the first row (not the cloned one) but when I ad

Why the cloned value won't save in DB ? It's working fine for the first row (not the cloned one) but when I add new value with jQuery(.clone), new added value won't save...

jQuery

$('#add-embed').on('click', function() {
    var row = $('.empty-row').clone(true);
        row.removeClass('empty-row').addClass('entries');
        row.insertBefore('#repeater > div:last');
    return false;
});

HTML

<div id="repeater">
    <div class="entries">
        <input type="text" name="al_ttl[]">
        <input type="text" name="al_tmp[]">
    </div>
    <div class="empty-row">
        <input type="text" name="al_ttl[]">
        <input type="text" name="al_tmp[]">
    </div>
</div>

Save

function frontend_submit() {
    global $current_user, $post;

    if ( 'POST' == $_SERVER['REQUEST_METHOD']
        && !empty( $_POST['action'] )
        && $_POST['action'] == 'post' ) {

        //Global Post
        $post_id        = $post->ID;
        $user_id        = $current_user->ID;
        $post_title     = $_POST['post_title'];


        $post_id = wp_insert_post(array(
            'post_author'   => $user_id,
            'post_title'    => $post_title,
            'post_status'   => 'publish',
            'post_type'     => 'test'
        ));


        //Data Rows Save
        $old = get_post_meta($post_id, 'data_rows', true);
        $new = array();
        $ah_ttl = $_POST['al_ttl'];
        $ah_tmp = $_POST['al_tmp'];
        $count = count( $ah_ttl );
        for ( $i = 0; $i < $count; $i++ ) {
            if ( $ah_ttl[$i] != '' ) :
                $new[$i]['al_ttl'] = stripslashes( strip_tags( $ah_ttl[$i] ) );
                $new[$i]['al_tmp'] = stripslashes( $ah_tmp[$i] );

            endif;
        }

        if ( !empty( $new ) && $new != $old )
            update_post_meta( $post_id, 'data_rows', $new );
        elseif ( empty($new) && $old )
            delete_post_meta( $post_id, 'data_rows', $old );

        }

}
do_action('wp_insert_post', 'frontend_submit');

Why the cloned value won't save in DB ? It's working fine for the first row (not the cloned one) but when I add new value with jQuery(.clone), new added value won't save...

jQuery

$('#add-embed').on('click', function() {
    var row = $('.empty-row').clone(true);
        row.removeClass('empty-row').addClass('entries');
        row.insertBefore('#repeater > div:last');
    return false;
});

HTML

<div id="repeater">
    <div class="entries">
        <input type="text" name="al_ttl[]">
        <input type="text" name="al_tmp[]">
    </div>
    <div class="empty-row">
        <input type="text" name="al_ttl[]">
        <input type="text" name="al_tmp[]">
    </div>
</div>

Save

function frontend_submit() {
    global $current_user, $post;

    if ( 'POST' == $_SERVER['REQUEST_METHOD']
        && !empty( $_POST['action'] )
        && $_POST['action'] == 'post' ) {

        //Global Post
        $post_id        = $post->ID;
        $user_id        = $current_user->ID;
        $post_title     = $_POST['post_title'];


        $post_id = wp_insert_post(array(
            'post_author'   => $user_id,
            'post_title'    => $post_title,
            'post_status'   => 'publish',
            'post_type'     => 'test'
        ));


        //Data Rows Save
        $old = get_post_meta($post_id, 'data_rows', true);
        $new = array();
        $ah_ttl = $_POST['al_ttl'];
        $ah_tmp = $_POST['al_tmp'];
        $count = count( $ah_ttl );
        for ( $i = 0; $i < $count; $i++ ) {
            if ( $ah_ttl[$i] != '' ) :
                $new[$i]['al_ttl'] = stripslashes( strip_tags( $ah_ttl[$i] ) );
                $new[$i]['al_tmp'] = stripslashes( $ah_tmp[$i] );

            endif;
        }

        if ( !empty( $new ) && $new != $old )
            update_post_meta( $post_id, 'data_rows', $new );
        elseif ( empty($new) && $old )
            delete_post_meta( $post_id, 'data_rows', $old );

        }

}
do_action('wp_insert_post', 'frontend_submit');
Share Improve this question edited Jan 7, 2020 at 20:14 mems asked Dec 31, 2019 at 20:15 memsmems 7312 bronze badges 14
  • I didn't read every line of code, but I'm pretty confused as to what is going on here. What are you trying to achieve? – jasonp Commented Jan 7, 2020 at 18:38
  • Cloned field value won't save when I submit my post from frontend. Im using jQuery to clone input. – mems Commented Jan 7, 2020 at 20:10
  • What code you are using to fetch the records? Have seen the data in database, how is it look in there? – BlueSuiter Commented Jan 8, 2020 at 6:06
  • My data is serialized. I use foreach to fetch... – mems Commented Jan 8, 2020 at 7:10
  • 1 is your <div id='repeater'> properly inside <form> tag of your page ( I assume you're in a form since your code does not provide an ajax function) – Andrea Somovigo Commented Jan 13, 2020 at 8:49
 |  Show 9 more comments

1 Answer 1

Reset to default 1 +50

Double check if your <div id='repeater'> is properly inside <form> tag of your page ( I assume you're in a form since your code does not provide an ajax function) and from what you outpup with var_dump($_POST['al_ttl']) it may be the reason of Undefined index: al_ttl.

Also note that you're overwriting the first initialization of $post_id with wp_insert_post and after that you're looking for a post_meta $old = get_post_meta($post_id, 'data_rows', true); which will always be empty being on the new post

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744840955a4596553.html

相关推荐

  • custom field - frontend submit post jQuery clone row won&#39;t save

    Why the cloned value won't save in DB ?It's working fine for the first row (not the cloned one) but when I ad

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信