javascript - Wordpress - How refresh page after updating a DB - Stack Overflow

I’m trying to make a plugin to a sliderI already made an admin panel, and I can update the db,but I&#

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

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>';
}
Share Improve this question edited Dec 28, 2021 at 9:44 xXx 1,1611 gold badge11 silver badges24 bronze badges asked Dec 30, 2013 at 10:17 DUKEiLLDUKEiLL 3092 gold badges7 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744271564a4566126.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信