JavaScript 函数在使用 header() 后不会运行,但如果不使用 header() 则运行(在 php 中)

标签 javascript php html

如果我的问题标题令人困惑,我深表歉意,这是我的第一篇文章,尽管阅读了 https://stackoverflow.com/help/on-topic我觉得我的写题能力可能还存在一些缺陷。

TL;DR: 如果我不使用 header("location: ProjectUserProfile.php?UploadSuccessful"),JavaScript 动画可以工作,但如果我使用 (我需要)。有什么原因或解决办法吗?

无论如何,

上下文: 我有一个嵌入在 php 文档中的 html 表单,用于上传图像、删除图像等。

主要代码发生在 ProjectUserProfile.php 上(并且工作正常),上传图像后,我使用 header("location: ProjectUserProfile.php?UploadSuccessful") 返回页面,并提示刷新。

问题: 如果我不使用 header("location: ProjectUserProfile.php?UploadSuccessful") ,图像不会改变等,所以我有必要使用它。但是,最近我实现了“幻灯片通知”(如果您愿意的话),它会显示成功和错误消息。这些正常工作正常,但如果我使用 header("location: ProjectUserProfile.php?UploadSuccessful") 返回页面,则无法显示。

<?php
// all the uploading etc that works occurs here
header("location: ProjectUserProfile.php?UploadSuccessful");
echo "<script> openMessage('Information','The duplicate files were successfully uploaded!') </script>";
?>

重定向到 ProjectUserProfile.php?UploadSuccessful 后,无法确认openMessage,因此没有任何反应。

然而,如果我没有使用 header("location: ProjectUserProfile.php?UploadSuccessful"),“通知”就会滑入并起作用。

有人有任何解决方案或建议吗?

JavaScript 函数“openMessage()”的相关代码如下:

function openMessage(Purpose, DisplayText){
	var notificationDiv = document.getElementById("slideinNotification");
	if(notificationDiv){
		alert("exists");
	}
	else{
		alert("does not exist");
	}


	document.addEventListener("DOMContentLoaded", function(event){
		if(Purpose == "Information"){
		document.getElementById("slideInNotification").style.backgroundColor = "#4CAF50";
		}
		else if(Purpose == "Warning"){
			document.getElementById("slideInNotification").style.backgroundColor = "#FF9800";
		}
		else if(Purpose == "Error"){
			document.getElementById("slideInNotification").style.backgroundColor = "#F44336";
		}
		document.getElementById("notificationMessage").innerHTML = DisplayText;
		moveElement();
	});
}
<?php
if($filesWereDeleted == true){
$connection = new mysqli("localhost", "root", "root", "project");
$result = $connection -> query("UPDATE UserProfileImage SET UploadStatus = 1 WHERE UserUniqueID = '$userProfileId'");
header("location: ProjectUserProfile.php?DeletionSuccessful");
echo "<script> openMessage('Information','The profile image was successfully deleted!') </script>";
}
?>



<div id = "slideInNotification" class = "slideNotification">
<p id = "notificationMessage" class = "notificationInfo"></p>
<a href = "javascript:void(0)" class = "closeButton" onclick = "closeMessage()">&times;</a>
</div>

最佳答案

首先,您的 UPDATE 查询暴露于 SQL 注入(inject),如果您从用户处获取 id,我希望注意,请阅读准备好的语句。

其次,关于您的问题,您在发送 Location header 的同一响应中回显了通知脚本,因此在浏览器加载您的 JavaScript 代码之前,它会将客户端重定向到新页面您的通知 JavaScript 代码未回显...

如果您的问题是用户更新了图像,但由于缓存而没有显示,您可以在图像 src 的获取查询中使用 uniqid() 或修改时间,更有效

关于JavaScript 函数在使用 header() 后不会运行,但如果不使用 header() 则运行(在 php 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707163/

相关文章:

javascript - Angular ng-click从jquery触发事件获取参数

php - 如何将数据库属性derby.database.sqlAuthorization更改为 'TRUE'

html - 在移动设备上当前视口(viewport)的中心显示 div

php - 使用array_search,但在找到结果后继续搜索(PHP)

php - 谷歌地图信息窗口看起来搞砸了

html - 如何在 flexbox 中垂直对齐文本?

javascript - 在 flickr 上将 CSS 选择器与 beautifulsoup 一起使用时遇到困难,我做错了什么吗?

javascript - 用于根据分配给表行的类过滤 HTML 表的 jQuery 按钮

javascript - ajax 发布数据不适用于共享主机

javascript - 是否可以创建类似于 typeof 工作方式的函数,例如使用空格而不是括号?