【发布时间】:2014-02-11 19:20:49
【问题描述】:
我有以下代码:
<form method='post'>
<input type='submit' name='bid' value='Licitar'>
当用户点击提交按钮时,我想更新 WordPress 帖子元数据,以更改实际出价。
我还想在不重新加载页面的情况下更新以下 div:
<div class='vehicle-value-box'>".auctionplugin_get_latest_bid($post->ID).",00€</div>
我怎样才能做到这两点?我很难理解如何捕获 $_POST 值并使用它来执行上述操作。我需要将 PHP 处理代码放在哪里并将其包含在 WordPress ajax 核心中?
编辑:
现在我的代码看起来像 page.php 上的(在循环内部,它都被 PHP “回显”了):
<div id='vehicle-value-box".$post->ID."'>".get_post_meta(get_the_ID(),'start_price', true).",00€</div>
(...)
<div class='vehicle-auction-box'>
<script>
jQuery('input[type=submit]').click(function(e) {
e.preventDefault();
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: 'action=newbid&id=".$post->ID."',
success: function(msg){
jQuery('#vehicle-value-box".$post->ID."').html(msg+',00€');
}
});
</script>
<div>
<form method='post'>
<input type='submit' name='bid".$post->ID."' value='Licitar' class='bidvalue'>
还有我的functions.php:
add_action('wp_ajax_newbid', 'newbid_ajax');
function newbid_ajax() {
$post_id = $_POST['id'];
$mybid = get_post_meta($post_id, 'start_price', true);
$mybid = $mybid + 100;
update_post_meta($post_id,'start_price',$mybid);
die($mybid);
}
【问题讨论】:
-
你可能在Updating custom post meta with ajax有一个战利品