update_post_meta using ajax

时间:2018-10-17 作者:rjm_wd

我有这个代码,但它不工作。我怎样才能让它工作呢谢谢!

HTML:

<form id="updateAmountOffered" class="amountOffered" method="post" action="">

    <label for="amount">Amount Offered:</label>
    <div class="flex">
        <span class="currency">$</span>
        <input id="amount" class="inpAmount" type="text" maxlength="15" />
        <input type="hidden" name="action" value="updateAmountOffered" />
        <button type="submit" class="btnSubmit">
        <span>
        <span>
        <i class="fa fa-check"></i>
        </span>
        <span>Accept</span>
        </span>
      </button>
    </div>
  </form>
PHP:

function updateAmountOffered(){

$amount = $_POST["inpAmount"];
$post_id = \'27030\';

update_post_meta($post_id, \'amount\', $amount);

die();
}
add_action(\'wp_ajax_updatemeta\', \'updateAmountOffered\');
add_action(\'wp_ajax_nopriv_updatemeta\', \'updateAmountOffered\');
JS公司:

<script type="text/javascript">
(function($, undefined) {

"use strict";

// When ready.
$(function() {

    var $form = $( "#updateAmountOffered" );
    var $input = $form.find( "input" );

    $input.on( "keyup", function( event ) {


        // When user select text in the document, also abort.
        var selection = window.getSelection().toString();
        if ( selection !== \'\' ) {
            return;
        }

        // When the arrow keys are pressed, abort.
        if ( $.inArray( event.keyCode, [38,40,37,39] ) !== -1 ) {
            return;
        }


        var $this = $( this );

        // Get the value.
        var input = $this.val();

        var input = input.replace(/[\\D\\s\\._\\-]+/g, "");
                input = input ? parseInt( input, 10 ) : 0;

                $this.val( function() {
                    return ( input === 0 ) ? "" : input.toLocaleString( "en-US" );
                } );
       } );

    /**
     * ==================================
     * When Form Submitted
     * ==================================
     */
    $form.on( "submit", function( event ) {

        var $this = $( this );
        var arr = $this.serializeArray();

        for (var i = 0; i < arr.length; i++) {
                arr[i].value = arr[i].value.replace(/[($)\\s\\._\\-]+/g, \'\'); // Sanitize the values.
        };

        $.ajax({
type: "POST",
url: "/wp-admin/wp-admin/admin-ajax.php",
data: updateAmountOffered,
error: function(jqXHR, textStatus, errorThrown){                                        
    console.error("The following error occured: " + textStatus, errorThrown);                                                       
},
success: function(data) {                                       
    console.log("Hooray, it worked!");                                                                  
}                              
 });

        console.log( arr );

        event.preventDefault();
    });

});
 })(jQuery);
 </script>

1 个回复
SO网友:jdp

您没有在id=“amount”的输入标记中设置name属性。将名称设置为“inpAmount”,然后重试。

<input id="amount" class="inpAmount" type="text" maxlength="15" />

应该是

<input id="amount" class="inpAmount" type="text" maxlength="15" name="inpAmount" />

如前所述,管理ajax的路径。php看起来也错了。

这个wp_ajax 命名法也不正确。actions后缀需要与name属性设置为“action”的输入值相匹配

应为:

add_action(\'wp_ajax_updateAmountOffered\', \'updateAmountOffered\');
add_action(\'wp_ajax_nopriv_updateAmountOffered\', \'updateAmountOffered\');

结束

相关推荐

如何用AJAX从unctions.php调用函数?

我想知道如何从函数中调用函数。php并在特定位置使用Ajax打印(woocommerce\\u before\\u add\\u to\\u cart\\u按钮)。我知道我可以像下面所做的那样简单地在add\\u操作中添加我的函数,但我认为ajax是可行的,因为我的函数输出得到了缓存(整页缓存),所以我认为ajax可以解决这个问题。<?php add_action( \'woocommerce_before_add_to_cart_button\', \'hello_world\', 1