【问题标题】:How to update post meta on wordpress with AJAX如何使用 AJAX 更新 wordpress 上的 post meta
【发布时间】: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);
}

【问题讨论】:

标签: php ajax wordpress


【解决方案1】:

编辑:由于您在评论中指定没有值输入,因此我进行了相应的编辑。

首先您要确保在前端定义了 wordpress 的 ajaxurl,因为您可以使用此代码。 (例如插入functions.php)

add_action('wp_head','my_ajaxurl');
function my_ajaxurl() {
$html = '<script type="text/javascript">';
$html .= 'var ajaxurl = "' . admin_url( 'admin-ajax.php' ) . '"';
$html .= '</script>';
echo $html;
}

其次,您应该创建 ajax 调用,为此在包含表单的页面中添加此脚本标记:

<script>
jQuery('input[type=submit]').click(function(e) {
e.preventDefault();
jQuery.ajax({
       type: "POST",
       url: ajaxurl,
       data: "action=newbid&id="+<?php echo $post->ID?>,  
       success: function(msg){
            jQuery('.vehicle-value-box').html(msg+",00€");
       }
   });
})
</script>

最后我们需要在wordpress中处理数据,为此我们应该在主题functions.php中使用这个动作:

add_action('wp_ajax_newbid', 'newbid_ajax');
function newbid_ajax() {
    $post_id = $_POST['id'];

    //Get current bid
    $mybid = get_post_meta($post_id, 'start_price', true);

    //Increase the bid, for example the amount here is 100€
    $mybid = $mybid + 100;

    //Update the database with the increased bid value
    update_post_meta($post_id,'start_price',$mybid);

    // In case you need to update another meta for the user, you 
    // can access the user ID with the get_current_user_id() function

    // Finally sending back the updated bid so the javascript can display it
    die($mybid);
}

【讨论】:

  • 我相信我遗漏了一些东西,因为它仍然无法正常工作。无论如何,我可能会补充说没有输入值。我想要的只是当用户点击“出价”按钮时,拍卖的价值会上升,例如 100 欧元,这样做的用户会被记录下来,并且带有价格的框也会更新。我确定现在已经定义了 ajaxurl。我尝试在“随心所欲”部分添加的最后一段代码如下: update_post_meta('45','start_price',$mybid);至于 ajax 调用,我唯一改变的是 mybidvalue 的硬编码值: var mybidvalue = 100;
  • 这说明了很多,在您的情况下,您不需要访问 POST 变量中的出价值,您只需在用户单击按钮时更新元键。我会相应地编辑我的答案。
  • 感谢您的帮助。有些事情仍然失败,但我想问题是我缺乏使用 AJAX 的经验。我在functions.php 中的代码与您在此处发布的完全一样。至于 AJAX 调用,我将它放在 wordpress 循环中,因为页面上的每个帖子都需要它。 Peraphs 如果您查看该网站,您将了解它应该如何工作。转到link 并查看“LICITAR”按钮。这就是用户点击的地方。上面的值是要更新的值,您可以使用箭头在拍卖之间导航。
  • 您遇到了两个问题,首先我在 javascript 中添加了 e.preventDefault() 以阻止页面提交并改用 ajax。第二个问题是 wp_ajax_newbid 函数没有从 wordpress 响应。您需要确保将函数和钩子添加到主题的 functions.php 中,它们的名称也必须完全相同,因为我们在 javascript 中指定了 action=newbid。所以请确保正确包含上一步的代码。
  • 终于出事了!当我点击“LICITAR”按钮时,它用 +100 更新了价值,并且两次拍卖都更新了“vehicle-value-box”,因为它们都有相同的类名。但这不是问题,我相信我可以使用
    和 jQuery('#vehicle-value-box".$post ->ID."').html(msg+',00€');问题是:当我在 Auction 1 上单击 LICITAR 时,它会更新 Auction 2 的 start_price 数据库值。当我在 Auction 2 上单击“LICITAR”时,e.preventDefault() 不起作用并重新加载页面。非常感谢您迄今为止的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多