Page refreshes but doesn't update the new content

the code below works fine it updates the rank in db when submit button is clicked, but then it refreshes the page and loads the old content itself instead of updating with new content. how do i get it to load the new content on refresh.

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

In the update_ranks function add an echo at the start:

echo 'I have been run';

That will confirm that the update function is being hit. If it is, do a var_dump() on what it’s being sent to confirm that it’s what you expect it to have been sent

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.