简体   繁体   English

如何从jQuery Ajax获取特定值?

[英]How to get specific value using from jquery ajax?

I am using jquery ajax function for getting some value and submitting some values, so far I have no issue for submitting value but when it comes to collect the value it is showing the old value not the new one. 我正在使用jquery ajax函数获取一些值并提交一些值,到目前为止,我没有提交值的问题,但是当涉及到收集值时,它显示的是旧值而不是新值。

I know a method where we can define json object and pick value from there, if you are working on the single same page. 我知道一个方法,如果您在单个页面上工作,则可以定义json对象并从那里选择值。

But I want to know is there any other way around we can use to get the specific value. 但是我想知道还有其他方法可以用来获取特定值。

(document).ready(function() {
    $("#bmup").click(function(){
        var upval = $.post(window.location,{rate:$("#bvup").val()},function(data){
            alert(data);
            $("#bmup").html(data);
        });
    });
    });

the data returning here is full html of the whole website, while I want only the data of rate, the updated value of rate variable that is defined in php. 返回的数据是整个网站的完整html,而我只需要rate数据,即php中定义的rate变量的更新值。

In case if it helps: the function is updating +1 in db and updating the value of rate after updating but that value is showing after the page refresh, so I thought to implement jquery & ajax now the problem is the returning data is full html of my whole website, not just the rate variable. 如果有帮助的话:该函数在db中更新+1并在更新后更新rate的值,但是该值在页面刷新后显示,所以我认为现在实现jquery&ajax的问题​​是返回的数据是完整的html整个网站,而不仅仅是费率变量。

Ok sorry this is the edited version I thought only jquery will enough but here we go with the full situation. 好的,很抱歉,这是我认为仅jquery就足够了的编辑版本,但是在这里,我们将介绍所有情况。

i am using this on single.php on wordpress So there are already many html marked ups and echo's before my $rate. 我在wordpress的single.php上使用了它,因此在$ rate之前已经有许多html标记了ups和echo。 when I am trying to get rate variable it is printing my whole page instead. 当我尝试获取速率变量时,它正在打印整个页面。

<?php 
 /**
 * Singular Content Template
 */

?>

<?php get_header(); ?>
<?php $cuser = wp_get_current_user(); ?>

<div class="main wrap cf">
    <div class="row">
        <div class="col-8 main-content">

            <?php while (have_posts()) : the_post(); ?>

                <?php 

                    $panels = get_post_meta(get_the_ID(), 'panels_data', true);

                    if (!empty($panels) && !empty($panels['grid'])):

                        get_template_part('content', 'builder');

                    else:

                        get_template_part('content', 'single');

                    endif; 
                ?>
                <div class="comments">
<div class="cntt">

<?php

//if(!isset($_COOKIE['rate'])) {

    if($_POST['rate']=='up') {
    $rate=get_post_meta($post->ID,'up',true);
    $rate=$rate+1;
    update_post_meta($post->ID,'up',$rate);
    setcookie(rate, 1, time() + (86400 * 30), "/"); // 86400 = 1 day
    }

    if($_POST['rate']=='down') {
    $rate=get_post_meta($post->ID,'down',true);
    $rate=$rate+1;
    update_post_meta($post->ID,'down',$rate);
    setcookie(rate, 1, time() + (86400 * 30), "/"); // 86400 = 1 day
    }

if(isset($_COOKIE['rate'])) {
    echo "<div class='errormainbtn'> Only one vote is allowed ! </div>";
    }

}


$totalup = get_post_meta($post->ID,'up',true);
$totaldn = get_post_meta($post->ID,'down',true);

?>

<input type='hidden' id='valval' value='<?php echo $rate; ?>' />

<div class="bothmainbtn">
<div class="btmainup">
<form method='POST' action="">
<input id="bvup" type=hidden name=rate value=down>
<input id="bmup2" class="tpbtn" type="submit" value= 
'<?php 

if ($totaldn != "" && $totaldn != 0) {
echo "Disagree:&nbsp;" . $totaldn;
}
else
{
echo "Disagree ?";
}
?>'
>
</form>
<div id="bmup">Click</div>
</div>



<div class="btmaindown">
<form method='POST' action="">
<input type=hidden name=rate value=up>
<input class="tpbtn" type=submit value=
'<?php 
if ($totalup != "" && $totalup != 0) {
echo "Agree:&nbsp;".$totalup;
}
else
{
echo "Agree ?";
}
?>'
>
</form>
</div>
</div>

<?php include 'leftsiderev.php';?>
<?php include 'rightsiderev.php';?>



<!-- Main working sheet -->







                </div> <!-- comment-->

            <?php endwhile; // end of the loop. ?>

        </div>

        <?php Bunyad::core()->theme_sidebar(); ?>

    </div> <!-- .row -->
</div> <!-- .main -->

<?php get_footer(); ?>

What i am trying to achieve is when the user click on vote up or down, the $totaldn will have the new value I want to show this updated value. 我想要实现的是,当用户单击上或下投票时,$ totaldn将具有我想要显示此更新值的新值。

as if I want to implement the json object I have to use array and then I will have to use it on top of the page, while this is quite difficult in word press as header's are already passed with navigation's. 就像我要实现json对象一样,我必须使用数组,然后我必须在页面顶部使用它,而在单词按中这是相当困难的,因为标题已经与导航一起传递了。

any solution ? 有什么办法吗?

Hope this helps you 希望这对您有帮助

<?php 
         $rate = getRate(); // grabs the rate
         if($_POST) {

          echo $rate;

         }else { // if it is not post show the html

        ?>

        <html>
         <title></title>
         <body>
           // content goes here
         </body>
        </html>
        <?php 
        }
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM