0

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');
  • 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 Jan 7 at 18:38
  • Cloned field value won't save when I submit my post from frontend. Im using jQuery to clone input. – mems Jan 7 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 Jan 8 at 6:06
  • My data is serialized. I use foreach to fetch... – mems Jan 8 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 Jan 13 at 8:49
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

|improve this answer|||||
  • Thank you for your answer ! Everything working fine with my function except my form(html)... wrong closing form. That's why the cloned field won't save. – mems Jan 13 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.