图片上传后 PHP 页面刷新不起作用


php page refresh not working after picture upload

用户上传完个人资料图片后,如何自动刷新页面?好吧,一旦用户刷新页面,图片就会更新,但我想强制刷新页面。

我正在上传文件并更新我的数据库,如下所示:

$query="UPDATE users set image='".$username_db."_n.gif' where user_name='$username_db'";
mysqli_query($con,$query);

在此之后,我想要一个刷新代码。

我已经尝试了几种方法:

echo "<script type='text/javascript'>$('.display_picture_image').attr('src',    '".$src."?".time()."');<scipt>";
exit;

其中.display_picture_image是我要在其中显示图片的图像标签。

echo "<meta http-equiv='refresh' content='0'>"
exit;

然后

header("Refresh:0");
exit();

然后

header("Location:".$_SERVER[REQUEST_URI]);
exit();

然后

header("Location:page_name.php");
exit();

然后

echo "<script type='text/javascript'>location.reload();</script>";

但没有任何效果。我做错了什么?

我有一个页面:索引.php。它包含是自引用形式的形式。

if(isset($_POST['submit'])
    include 'upload.php';

提交图片后,

来自
upload.php

被执行。然后上传图片,然后

echo '<script type="text/javascript">$(window).on("load",function(){window.top.window.showcrop("'.$target_file.'","'.$username_db.'","'.$imageFileType.'");});</script>';

调用函数 showcrop. 在标头中链接的 js 文件中。

然后在选择并提交裁剪区域后,将执行以下命令:

function crop_photo() {
   var x_ = $('#x').val();
   var y_ = $('#y').val();
   var w_ = $('#w').val();
   var h_ = $('#h').val();
   var photo_url_ = $('#photo_url').val();
   var username_ = $('#username').val();
   var fileTypeImage_ = $('#imageFileType').val();
   // hide thecrop  popup
   $('#popup_crop').hide();
   // display the loading texte
  // crop photo with a php file using ajax call
  $.ajax({
     url: 'crop_photo.php',
     type: 'POST',
     data: {x:x_, y:y_, w:w_, h:h_, photo_url:photo_url_, username:username_,     fileTypeImage:fileTypeImage_, targ_w:TARGET_W, targ_h:TARGET_H},
     success:function(data){
     // display the croped photo
   }
   });
 }
// updateCoords : updates hidden input values after every crop selection
function updateCoords(c) {
   $('#x').val(c.x);
   $('#y').val(c.y);
   $('#w').val(c.w);
   $('#h').val(c.h);
}

然后执行裁剪.php,上传裁剪后的图片并更新数据库。最后,刷新代码已编写,但不起作用。

服务器端:

注意:上传文件并更新数据库后输入以下代码:

  header("Refresh: 300;url='REDIRECTION URI'");

浏览器将在 300 秒后重定向。不过,它可以在浏览器的配置中禁用,但通常不禁用。

客户端 :

location.reload();
好的,

所以我自己想出了答案。在我的crop_photo.php文件中,

$.ajax({
 url: 'crop_photo.php',
 type: 'POST',
 data: {x:x_, y:y_, w:w_, h:h_, photo_url:photo_url_, username:username_,       fileTypeImage:fileTypeImage_, targ_w:TARGET_W, targ_h:TARGET_H},
 success:function(data){
 // display the croped photo
 }
});

好吧,在上面的代码中,我什么也没提到

success:function(data){
//display the cropped photo
}

虽然这就是php将要回应的东西的地方。所以我把它编辑成,

success:function(data){
//display the cropped photo
$('#image_div').html(data);
}

这样,我在 php 中回声的任何内容都被放入了 id image_div 的分区内。然后我附和了

echo '<script>window.location='redirectbackurl.php'</script>';

它重定向到重定向回网址.php重定向回上一个链接。我的重定向反向网址的代码.php

<?php
   header("Location:".$_SERVER['HTTP_REFERER']);
   exit();
?>

现在我明白了,我可以简单地使用 location.reload(),但由于表单重新提交问题,我这样做了。