[英]Update post meta using front-end form / input box with ajax without page reload
I'm able to update post meta without ajax, with page reload using below code. 我可以不使用ajax来更新post meta,并使用以下代码重新加载页面。
global $post;
if( isset($_POST['submit_meta']) ) {
if( ! empty($_POST['change_meta']) ) {
update_post_meta($post->ID, 'shorturl', $_POST['change_meta']);
}
}
echo $_POST[ 'change_meta' ];
<form method="post" action="" id="shortform">
<input style="/*display:none;*/" id="shortUrlInfo2" type="text" name="change_meta" value="<?php echo get_post_meta( get_the_ID(), 'shorturl', true ); ?>">
<input id="btn_url" type="submit" name="submit_meta" value="Confirm" />
</form>
But how can i update the same post meta ( custom field ) with ajax? 但是我如何用ajax更新相同的post meta(自定义字段)? I tried my best to get it right using various sources, but can't able to find a solution. 我尽力使用各种资源来解决问题,但是找不到解决方案。
I even tried wordpress ajax in plugins, but can't able to understand really. 我什至在插件中尝试过wordpress ajax,但无法真正理解。
Here is the thing, i've tried. 这是我尝试过的东西。
add_action( 'wp_ajax_update_custom_fields', 'update_custom_fields' );
add_action( 'wp_ajax_nopriv_update_custom_fields', 'update_custom_fields' );
function update_custom_fields() {
$post_id = $_POST[ 'post_id' ];
$meta1 = $_POST[ 'meta1' ];
$meta2 = $_POST[ 'meta2' ];
update_post_meta( $post_id, 'meta1', $meta1 );
update_post_meta( $post_id, 'meta2', $meta2 );
die('Updated');
}
add_action('wp_footer', 'pp_script');
function pp_script() {
wp_enqueue_script('jquery');
?>
<script>
jQuery(document).ready( function($) {
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
var spinner = '<?php echo admin_url( 'images/spin.gif' ); ?>';
$('.pp input').click( function() {
var p = $(this).parent('p');
p.find('.response').html('<img src="'+spinner+'" />');
$.post(ajaxurl, {
'action': 'update_custom_fields',
'post_id': p.find('input[name="post_id"]').val(),
'meta1': p.find('input[name="meta1"]').val(),
'meta2': p.find('input[name="meta2"]').val()
}, function( response ) {
p.find('.response').html(response);
}, 'text');
});
});
</script><?php
}
So i need some light in the best direction to get it right. 因此,我需要一些最佳方向的光线以使其正确。 Also do we need to use form or button in html input boxes? 我们还需要在html输入框中使用表单或按钮吗? Or just input boxes? 还是只是输入框?
Is this html okay? 这个HTML可以吗?
<input style="/*display:none;*/" name="post_id" type='text' id='post_id' value='7' />
<input style="/*display:none;*/" name="meta1" type='text' id='meta1' value='' />
<input style="/*display:none;*/" name="meta2" type='text' id='meta2' value='' />
<input type="button" id="meta1submit" value="meta1submit" />
Debug your problem in steps: 分步调试问题:
Is your AJAX call is happening? 您的AJAX通话正在进行吗?
Instead of style="display:none;" 代替style =“ display:none;” to input elements, you can use type='hidden' . 要输入元素,可以使用type ='hidden' 。
You are using class names to access elements in jQuery, instead you should use IDs of the elements. 您正在使用类名来访问jQuery中的元素,而应该使用元素的ID。
And always use nonce in the form for security reason. 出于安全原因,请始终以该形式使用随机数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.