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
|
Show 9 more comments
1 Answer
Reset to default 1 +50Double 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
<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