有人可以帮助我,因为我对编码很陌生。
我正在使用智能模板引擎,我有这个表单供用户更新,他们是封面图片或添加一个,但我的问题是,每当我或用户重新加载页面时,表单每次都会发布一个新图像甚至没有按下提交按钮。
这是表格。
<form method="post" enctype="multipart/form-data" id="cover_upload">
<input type="file" name="cover_file" id="file" class="cover_input" />
<input type="hidden" name="action" value="change_cover" />
<a class="submit_form_button" style="float:right" onclick="jQuery('#cover_upload').submit();" href="javascript:void(0);">Change</a>
</form>
这是php
if (isset($action) && $action=='change_cover' && isset($_SESSION['loggeduser_id'])){
if (isset($_FILES['cover_file']['name']) && $_FILES['cover_file']['name']){
if (($_FILES["cover_file"]["type"] == "image/gif") || ($_FILES["cover_file"]["type"] == "image/jpeg") || ($_FILES["cover_file"]["type"] == "image/pjpeg") || ($_FILES["cover_file"]["type"] == "image/png")){
$filename = $_SESSION['loggeduser_id']."_".date("YmdHis").".jpg";
if (move_uploaded_file($_FILES["cover_file"]["tmp_name"],"$basepath/thumbs/users/covers/" . $filename)){
$user = new User();
$user->update($_SESSION['loggeduser_id'],array("cover" => $filename));
$_SESSION['loggeduser_details']['cover'] = $filename;
}
}
}
}
有人可以出示或告诉我发生了什么以及如何解决此问题
答案 0 :(得分:1)
这是因为您没有遵循POST/REDIRECT/GET模式。如果POST请求成功,则应始终重定向。
示例:
$success = $obj->doSomething($_POST['var']);
if($success){
header('Location: page.php?m=success');
exit;
} else{
//Something went wrong...
}
答案 1 :(得分:0)
如果您在提交表单后按f5键(或点击重新加载按钮),它将重新提交表单..在Chrome中,您将收到确认消息..
答案 2 :(得分:0)
我已经解决了这个问题,而不需要重定向技术,因为现代AJAX JavaScript允许:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
$(".submit_form_button").click(function() {
$.ajax({
type: "POST",
url: "",
data: $("#cover_upload").serialize(),
success: function () {
//alert("OK");
}
});
//
// this is important not to trigger the link action:
//
return false;
});
});
</script>
</head>
<body>
<?php
//
//...
//
if (isset($action) && $action=='change_cover' && isset($_SESSION['loggeduser_id'])){
if (isset($_FILES['cover_file']['name']) && $_FILES['cover_file']['name']){
if (($_FILES["cover_file"]["type"] == "image/gif") || ($_FILES["cover_file"]["type"] == "image/jpeg") || ($_FILES["cover_file"]["type"] == "image/pjpeg") || ($_FILES["cover_file"]["type"] == "image/png")){
$filename = $_SESSION['loggeduser_id']."_".date("YmdHis").".jpg";
if (move_uploaded_file($_FILES["cover_file"]["tmp_name"],"$basepath/thumbs/users/covers/" . $filename)){
$user = new User();
$user->update($_SESSION['loggeduser_id'],array("cover" => $filename));
$_SESSION['loggeduser_details']['cover'] = $filename;
}
}
}
}
//
//...
//
?>
<form method="post" enctype="multipart/form-data" id="cover_upload">
<input type="file" name="cover_file" id="file" class="cover_input" />
<input type="hidden" name="action" value="change_cover" />
<a class="submit_form_button" style="float:right" href="">Change</a>
</form>
<?php
?>
</body>
</html>
我修改了这些项目: