Wordpress - How refresh page after updating a DB

15.3k views Asked by At

I’m trying to make a plugin to a slider I already made an admin panel, and I can update the db, but I've a problem after I put the new data and press update - its updates the db, but it is not shown on the page. I need to refresh the admin page, and only after that, I can see the changes.

How can I automatically refresh the page?

  • I try to call js func etc.:window.location.reload()
  • I tried a lot but nothing seemed work
  • I have a little understanding of JavaScript and I don't know what the problem is and how to fix it

Code:

<form  action="" method="post">
<?php
for($i=1; $i<=5; $i++)
{
    $sliderows=getSliderData($i);
    echo '<div id="single-slide-wrap">
            <div id="slide-num">
                <p id="stitle"><strong>#'.$i.'</strong></p>
                <input name="slideradio" type="radio" value="'.$i.'" />
            </div><!--slide-num-->
            <div id="slide-pic">
                <img alt="" src="'.$sliderows['img'].'" style="width: 120px; height: 80px;" />
            </div><!--slide-pic-->
            <div id="slide-text">
                <strong>Img Url: </strong>'.$sliderows['img'].'<br />
                <strong>Title: </strong>'.$sliderows['title'].'<br />
                <strong>Url: </strong>'.$sliderows['url'].'<br />
            </div><!--slide-text-->
                
            </div><!--single-slide-wrap-->';
}
    ?>
    
<h3>update Slider</h3>

IMAGE URL: <input type="text" name="img" />
TEXT: <input type="text" name="text" />
URL: <input type="text" name="url" />
<input type='submit' name="submit" value='update'  /> 
</form>

<?php       

  if (isset($_POST['submit'])) {
    if (isset($_POST['slideradio'])) {

      switch ($_POST['slideradio']) {
        case '1':
          update_slider_db(1);
          break;
        case '2':
          update_slider_db(2);
          break;
        case '3':
          update_slider_db(3);
          break;
        case '4':
          update_slider_db(4);
          break;
        case '5':
          update_slider_db(5);
          break;
      }
    }
  }

  echo '</div>';
}
2

There are 2 answers

0
DUKEiLL On BEST ANSWER

i just found the answer - after a lot of digging:

i insert the code:
header("location: " . $_SERVER['REQUEST_URI']);
into update_slider_db function - after updating the db.
and then add:

//allow redirection, even if my theme starts to send output to the browser
add_action('init', 'do_output_buffer');
function do_output_buffer() {
    ob_start();

}

into function.php

1
arnorhs On

If you want to reload the page in javascript (the question was tagged with javascript) you can use the Location#reload() method:

document.location.reload(true);

Make sure to write your code so that you don't get it running in an infinite redirect loop.

If you want to reload the page in PHP, you can send the redirect headers and to be a good citizen you might want to send a 302 status header along with the request. You should probably also kill your script after that header is thrown to be sure to not execute any of the following code if there is any.

<?php
header("HTTP/1.1 302 Found");
header("Location: " + $_SERVER['REQUEST_URI']);
exit;
?>