如何更新后的元上字preSS与AJAX元上字、preSS、AJAX

2023-09-10 18:28:54 作者:农村孩子别样帅@

我有以下的code:

 <形式方法='后'>
<输入类型=提交名称='出价'值='Licitar'>
 

当用户点击提交按钮,我想更新的话preSS元后,为了改变实际出价的价值。

我也想更新,无需重新加载页面,下面的DIV:

 < D​​IV CLASS ='车辆价值盒子'>中auctionplugin_get_latest_bid($后> ID)。,00€< / DIV>
 

我怎样才能做到既?我有麻烦了解如何捕捉$ _ POST值,并用它做上述的事情。我在哪里需要把PHP处理code,包括在Word preSS ajax的核心是什么?

编辑:

Adidas仅一双上榜 陈冠希罕见上脚的AJ排第9丨本月球鞋价格排行

现在我的code看起来像page.php(在循环中,它都被呼应用PHP):

 < D​​IV ID ='车辆价值中的$后> ID'>get_post_meta(get_the_ID(),start_price,真) ,00€< / DIV>
 (...)
< D​​IV CLASS ='车拍卖中'>
<脚本>
jQuery的('输入[类型=提交])。点击(函数(五){
    即preventDefault();
    jQuery.ajax({
         键入:POST,
         网址:ajaxurl,
         数据:';。$后> ID行动= newbid与放大器ID =',
         成功:函数(MSG){
    jQuery的('#车辆价值中的$后> ID)HTML(味精+',00€')。
    }
});
< / SCRIPT>
< D​​IV>
<形式方法='后'>
    <输入类型=提交名称='竞价$后> ID的价值='Licitar级='bidvalue'>
 

和我的functions.php:

  add_action('wp_ajax_newbid','newbid_ajax');
功能newbid_ajax(){
    $的post_id = $ _ POST ['身份证'];
    $ mybid = get_post_meta($的post_id,start_price,真正的);
    $ mybid = $ mybid + 100;
    update_post_meta($的post_id,start_price',$ mybid);
    死亡($ mybid);
}
 

解决方案

编辑:既然你在注释中规定的就没有值输入,我编辑相应

首先你要确保该ajaxurl字preSS在前端的定义,你可以使用这个code。 (插入的functions.php为前。)

  add_action('wp_head','my_ajaxurl');
功能my_ajaxurl(){
$的HTML ='<脚本类型=文/ JavaScript的>';
。$的HTML ='VAR ajaxurl ='ADMIN_URL(管理-ajax.php)。'';
$的HTML ='< / SCRIPT>。
回声$的HTML;
}
 

其次,你应该创建Ajax调用,对于包含表单的页面添加这个脚本标签:

 <脚本>
jQuery的('输入[类型=提交])。点击(函数(五){
即preventDefault();
jQuery.ajax({
       键入:POST,
       网址:ajaxurl,
       数据:行动= newbid和ID =+< PHP的echo $后> ID>中?
       成功:函数(MSG){
            。jQuery的('。车辆价值盒子)HTML(味精+,00€);
       }
   });
})
< / SCRIPT>
 

最后,我们需要处理的话preSS的数据,我们应该用在主题的functions.php这个动作:

  add_action('wp_ajax_newbid','newbid_ajax');
功能newbid_ajax(){
    $的post_id = $ _ POST ['身份证'];

    //获取当前出价
    $ mybid = get_post_meta($的post_id,start_price,真正的);

    //增加出价,例如这里的量是100€
    $ mybid = $ mybid + 100;

    //更新与增加的出价价值数据库
    update_post_meta($的post_id,start_price',$ mybid);

    //如果你需要更新其他元的用户,你
    //可以访问的用户ID与get_current_user_id()函数

    //最后发回的最新出价使JavaScript可以显示它
    死亡($ mybid);
}
 

I have the following code:

<form method='post'>
<input type='submit' name='bid' value='Licitar'>

When the user clicks the submit button, I want to update the WordPress post meta, in order to change the actual bid value.

I'd also like to update, without reloading the page, the following div:

<div class='vehicle-value-box'>".auctionplugin_get_latest_bid($post->ID).",00€</div>

How can I do both? I have troubles understanding how to capture the $_POST value and use it to do the things above mentioned. Where do I need to put the PHP processing code and include it in WordPress ajax core?

EDIT:

Now my code looks like on page.php (INSIDE THE LOOP, and it is all being "echoed" with 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'>

And my 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);
}

解决方案

Edit: since you specified in the comment there would be no value input, i edited accordingly.

First thing you would like to make sure that the ajaxurl for wordpress is defined in the front end, for that you can use this code. (insert in functions.php for ex.)

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;
}

Secondly, you should create the ajax call, for that add this script tag in the page containing the form :

<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>

Finally we need to handle the data in wordpress, for that we should use this action in the theme 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);
}

 
精彩推荐
图片推荐