【问题标题】:page refreshes but doesn't update the new content页面刷新但不更新新内容
【发布时间】:2016-08-09 18:40:06
【问题描述】:

下面的代码运行良好,它会在单击提交按钮时更新 db 中的排名,但随后它会刷新页面并自行加载旧内容,而不是使用新内容进行更新。我如何让它在刷新时加载新内容。

     <?php

        function update_ranks($newimgurl,$newrank){

        switch ($newrank)
        {
        case "1":
              $rank="1st";
              break;
        case "2":
              $rank="2nd";
              break;
        case "3":
              $rank="3rd";
              break;

        }


                $field_key = "field_582fdg46";  
                $value=array();
                $count=0;
                global $totalimgs;
                global $allimgurl;          
                        global $allimgranks;               

                while ($count < $totalimgs){

                if ($allimgurl[$count]==$newimgurl){
                $value[] = array("imglink" => $newimgurl,"rank" => $rank );

                }
                else{
                $value[] = array("imglink" => $allimgurl[$count],"rank" => $allimgranks[$count] );      
                }
                $count=$count+1;        
                update_field( $field_key, $value, $post->ID);       

                }

                unset($newrank,$newimgurl,$currentnewrank,$currentimgurl,$rank,$count); 
                unset($allimgurl,

    ,$allimgranks,$totalimgs,$value);   


    }
?>
<?php
        if(isset($_POST['rank1btn'])){
        $currentnewrank=$_POST['rank1'];
        $currentimgurl=$rank1img;
        update_ranks($currentimgurl,$currentnewrank);
        }
        if(isset($_POST['rank2btn'])){
        $currentnewrank=$_POST['rank2'];
        $currentimgurl=$rank2img;
        update_ranks($currentimgurl,$currentnewrank);
        }
        if(isset($_POST['rank3btn'])){
        $currentnewrank=$_POST['rank3'];
        $currentimgurl=$rank3img;
        update_ranks($currentimgurl,$currentnewrank);
        }


    ?>   

     <form id="myform" name="myform" action="" method="POST" >
        <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 ">

        <?php   
        global $post;

        $bookid=get_the_title($post->ID);;
        $args = get_posts(array('post_type' => 'bookgallery' ,'post_title' =>$bookid, 'posts_per_page' => -1
        ));
        ?>

        <?php
        foreach ( $args as $post ) :  setup_postdata($post);

        if (!empty($post))
            {  
            $allimg=$allrank=array();

            while( have_rows('imgs') ): the_row();
            $temprank=get_sub_field('rank',$post->ID);  
            $tempimg=get_sub_field('imglink',$post->ID);

            $allimg[]=$tempimg;
            $allrank[]=$temprank;

            if ($temprank=='1st') {
            $rank1img= $tempimg;
            $rank1='1st';
            } 
            if ($temprank=='2nd') {
            $rank2img= $tempimg;
            $rank2='2nd';
            } 
            if ($temprank=='3rd') {
            $rank3img=$tempimg;
            $rank3='3rd'
            } 

            endwhile;

            if (!empty($rank1img)){
            ?>
            <div >

            <img src="<?php echo $rank1img; ?>" alt="" >

            <div >1st
                <select name="rank1">
                    <option value="0"> </option>
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>    
                        </select>
            </div>   

            <div ><input type="submit" name="rank1btn" value="update rank" id="rank1btn" ></div>   
            </div>   
            <?php } 
            if (!empty($rank2img)){
            ?>
            <div >2nd

            <img src="<?php echo $rank2img; ?>" alt="" > 

            <div >
                <select name="rank2">
                    <option value="0"> </option>
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>
                        </select>
            </div>   

            <div ><input type="submit" name="rank2btn" value="update rank" id="rank2btn" ></div>     
            </div>
             <?php } 
            if (!empty($rank3img)){
            ?>   

             <div>3rd  

            <img src="<?php echo $rank3img; ?>" alt="" >   
            <div>
                <select name="rank3">
                    <option value="0"> </option>
                    <option value="1">1st</option>
                    <option value="2">2nd</option>
                    <option value="3">3rd</option>
                        </select>
            </div>   

            <div><input type="submit" name="rank3btn" value="update rank" id="rank3btn" ></div>     
            </div>
             <?php } 

            }
         endforeach; ?>

        </article> 
        </form>

我将 update_ranks 放在 isset 代码下方和表单下方...现在它甚至不更新数据库,而当我将 isset 代码放在用于更新数据库但页面刷新的表单下方时这会自动发生并没有显示新内容。你必须手动刷新页面才能显示新内容。我如何让它在更新数据库的同时自动加载新内容。

【问题讨论】:

标签: php


【解决方案1】:

我犯了一个完全新手错误。我有两个文件副本。我的编辑更改的一个。另一个我需要复制到nginx web目录/var/www/html/下。我未能复制我的更改文件,因此没有任何更新。

我知道这是与原始问题不同的根本原因,但恐怕这可能是某些人不会看到他们的更改得到反映的常见原因(因此,最终会出现在 stackoverflow 上)。

【讨论】:

    【解决方案2】:

    您正在更新页面底部的数据库,因此在执行更新查询之前已加载内容。这可以通过将更新查询放在您的 html 内容上方来解决。
    这样更新将执行,然后您将从数据库中加载新数据。

    请记住,所有代码将始终按照其在文件中的放置顺序执行,即使是 html 内容的呈现。

    【讨论】:

    • 是否仍然加载加载内容
    • 您仍在调用页面底部的update_ranks 函数。在呈现任何 html 或查询数据库之前,将其移至顶部。
    • 更改了代码(检查它)当我将 isset 移到表单上方时仍然无法正常工作,它现在甚至没有更新数据库
    猜你喜欢
    • 2020-07-28
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多