如何在提交后重新加载包含 include 的 div,甚至刷新整个页面?
How to reload a div that has an include in it after submit, or even refresh the whole page?
抱歉解释得太长了,但在过去的两天里,我一直在努力解决这个问题,并尝试了各种方法来让我的动态 table 或页面刷新,但没有成功.这个问题包括 3 页,第一页包含在 Div 中加载 3 个不同的页面(这些页面每个都有一个 table 从目录加载特定文件列表) div通过下拉菜单获得 show/hide 在每一行中我都有一个删除按钮,它将删除 aws 存储桶目录中的文件并从 table 中删除该行。
问题是当我点击删除时它会从目录中删除文件,但不会删除行 (如果我刷新页面或再次单击“删除”,则该行将在实际页面上运行并且未通过 includediv 标签加载) 时有效。顺便说一句,上传工作完美无缺我有 tride .location,jquery,javascript,ajax,尝试在调试中单步执行代码。我已经把 ajax 等放在回声前后的 deletes3file.php 中,也在每个包含前后和每个 php 文件中。我应该提到的一件事是 deletes3file.php 有一个回声弹出所以一旦我点击确定就是我想从 table.
中删除行
第 1 页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="CSS/styles.css" />
<link rel="stylesheet" type="text/css" href="CSS/panels.css"/>
<body>
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
<ul>
<li>
<a href="index.php">Home</a>
<div class="cp-thumb">
<div class="cp-hover">
<?php
echo gethostname()
?>
</div>
</div>
</li>
</ul>
<div><a href="index.php" id="top-img"></a></div>
</div>
<!-- End Header -->
<!-- Begin Content -->
<div id="content">
<div id="dirlist">
<h2>Select Folder to upload to</h2>
<form action="uploads3.php" method="post" enctype="multipart/form-data" name="form1" id="form1"
<input type="hidden" name="method" value="post" />
<select id="bucketName" name="bucketName">
<option value="0">--Select Folder--</option>
<option value="Policy">Policy</option>
<option value="Program">Program</option>
<option value="Procedures">Procedures</option>
</select>
<input name="theFile" type="file" />
<input name="Submit" type="submit" value="Upload">
<script type="text/javascript">
document.getElementById('bucketName').addEventListener('change', function() {
var style = this.value == "Policy" ? 'block' : 'none';
document.getElementById('Policy').style.display = style;
var style = this.value == "Program" ? 'block' : 'none';
document.getElementById('Program').style.display = style;
var style = this.value == "Procedures" ? 'block' : 'none';
document.getElementById('Procedures').style.display = style;
});
</script>
</form>
</div><!-- End dirlist -->
<div id="Policy" style="display: none;">
<p><?php
include 'policy.php';
?></p>
</div>
<div id="Program" style="display: none;">
<p><?php
include 'program.php';
?></p>
</div>
<div id="Procedures" style="display: none;">
<p><?php
include 'procedures.php';
?></p>
</div>
</div><!-- End Content -->
</div><!-- End Wrapper -->
</body>
</html>
删除页面:
<?php
if(isset($_POST['bucketName'])){
$bucketName = $_POST['bucketName'];
}
$fname = $_POST['fname'];
include 'awss3con.php';
if($_POST){
if ($s3->deleteObject( $bucketName, $fname, S3::ACL_PUBLIC_READ)) {
echo "We successfully deleted your file $bucketName / $fname";
}else{
echo "Something went wrong while delteing your file. $fname sorry.";
}
}
?>
包含的页面:
<?php
$ignore = Array(".","..");
$count=1;
include 'awss3con.php';
// Get the contents of our bucket
echo '<form action="deletes3file.php" method="post" enctype="multipart/form-data">';
echo '<table id=db-table border=1>';
echo '<th colspan=3 style="text-align:center;">Policies</th>';
$contents = $s3->getBucket("MyBucket",'Policy');
foreach ($contents as $file){
//if(!in_array($file, $ignore));
if(!isset($flag_first)){
$flag_first=1;
continue;
}
$fname = $file['name'];
$furl = "https://s3.amazonaws.com/MyBucket/".$fname;
$newname = explode('/', $fname);
$fname = $newname['1'];
$bucketName = "MyBucket/Policy";
//output a link to the file
/*echo "<tr id='del$count'><td>$count</td><td><input type='submit' name=$furl index=$count value='delete' onclick='deleteFile(\"$dispimage\",$count,\"$directory\");> <a href=\"$furl\">$fname</a>
<input type='text' id=$count value=$furl name='furl' visible=''></td></tr>";*/
echo "<tr id='del$count'><td>$count</td><td>$fname</td><td><input type='button' id='delete$count' value='Delete' onclick='deleteFile(\"$fname\",$count,\"$bucketName\");'></td></tr>";
$count ++;
}
echo '</table>';
echo "</form>";
?>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function deleteFile(fname,rowid,bucketName)
{
$.ajax({ url: "deletes3file.php",
data: {"fname":fname,"bucketName":bucketName},
type: 'post',
success: function(output) {
alert(output);
$("#del"+rowid).remove();
}
});
}
</script>
而不是在 HTML 中附加事件处理程序并传递参数:
- 使用 HTML5
data
属性将数据与 HTML 个元素相关联。
- 在 javascript 中附加事件处理程序。
这是更好的做法,因为它将文档结构与动态功能分开,从而提高 HTML 和 javascript 的可读性。
因此,按如下方式构建行:
echo "<tr data-fname=\"$fname\" data-bucket-name=\"$bucketName\"><td>$count</td><td>$fname</td><td><input class=\"deleteFile\" type=\"button\" value=\"Delete\"></td></tr>";
注意:不需要任何 ID
并编写相应的脚本如下:
jQuery(function($) {
$(".deleteFile").on('click', function(event) {
event.preventDefault(); // probably not necessary but won't do any harm.
var $row = $(this).closest('tr'); // the clicked button's row.
$.ajax({
'url': 'deletes3file.php',
'data': $row.data(), // the data object is composed for you automatically
'type': 'post',
'success': function(output) {
$row.remove(); // here, you already have a reliable reference to the row that is to be deleted, so just remove() it.
}
});
})
});
抱歉解释得太长了,但在过去的两天里,我一直在努力解决这个问题,并尝试了各种方法来让我的动态 table 或页面刷新,但没有成功.这个问题包括 3 页,第一页包含在 Div 中加载 3 个不同的页面(这些页面每个都有一个 table 从目录加载特定文件列表) div通过下拉菜单获得 show/hide 在每一行中我都有一个删除按钮,它将删除 aws 存储桶目录中的文件并从 table 中删除该行。
问题是当我点击删除时它会从目录中删除文件,但不会删除行 (如果我刷新页面或再次单击“删除”,则该行将在实际页面上运行并且未通过 includediv 标签加载) 时有效。顺便说一句,上传工作完美无缺我有 tride .location,jquery,javascript,ajax,尝试在调试中单步执行代码。我已经把 ajax 等放在回声前后的 deletes3file.php 中,也在每个包含前后和每个 php 文件中。我应该提到的一件事是 deletes3file.php 有一个回声弹出所以一旦我点击确定就是我想从 table.
中删除行第 1 页:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="CSS/styles.css" />
<link rel="stylesheet" type="text/css" href="CSS/panels.css"/>
<body>
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
<ul>
<li>
<a href="index.php">Home</a>
<div class="cp-thumb">
<div class="cp-hover">
<?php
echo gethostname()
?>
</div>
</div>
</li>
</ul>
<div><a href="index.php" id="top-img"></a></div>
</div>
<!-- End Header -->
<!-- Begin Content -->
<div id="content">
<div id="dirlist">
<h2>Select Folder to upload to</h2>
<form action="uploads3.php" method="post" enctype="multipart/form-data" name="form1" id="form1"
<input type="hidden" name="method" value="post" />
<select id="bucketName" name="bucketName">
<option value="0">--Select Folder--</option>
<option value="Policy">Policy</option>
<option value="Program">Program</option>
<option value="Procedures">Procedures</option>
</select>
<input name="theFile" type="file" />
<input name="Submit" type="submit" value="Upload">
<script type="text/javascript">
document.getElementById('bucketName').addEventListener('change', function() {
var style = this.value == "Policy" ? 'block' : 'none';
document.getElementById('Policy').style.display = style;
var style = this.value == "Program" ? 'block' : 'none';
document.getElementById('Program').style.display = style;
var style = this.value == "Procedures" ? 'block' : 'none';
document.getElementById('Procedures').style.display = style;
});
</script>
</form>
</div><!-- End dirlist -->
<div id="Policy" style="display: none;">
<p><?php
include 'policy.php';
?></p>
</div>
<div id="Program" style="display: none;">
<p><?php
include 'program.php';
?></p>
</div>
<div id="Procedures" style="display: none;">
<p><?php
include 'procedures.php';
?></p>
</div>
</div><!-- End Content -->
</div><!-- End Wrapper -->
</body>
</html>
删除页面:
<?php
if(isset($_POST['bucketName'])){
$bucketName = $_POST['bucketName'];
}
$fname = $_POST['fname'];
include 'awss3con.php';
if($_POST){
if ($s3->deleteObject( $bucketName, $fname, S3::ACL_PUBLIC_READ)) {
echo "We successfully deleted your file $bucketName / $fname";
}else{
echo "Something went wrong while delteing your file. $fname sorry.";
}
}
?>
包含的页面:
<?php
$ignore = Array(".","..");
$count=1;
include 'awss3con.php';
// Get the contents of our bucket
echo '<form action="deletes3file.php" method="post" enctype="multipart/form-data">';
echo '<table id=db-table border=1>';
echo '<th colspan=3 style="text-align:center;">Policies</th>';
$contents = $s3->getBucket("MyBucket",'Policy');
foreach ($contents as $file){
//if(!in_array($file, $ignore));
if(!isset($flag_first)){
$flag_first=1;
continue;
}
$fname = $file['name'];
$furl = "https://s3.amazonaws.com/MyBucket/".$fname;
$newname = explode('/', $fname);
$fname = $newname['1'];
$bucketName = "MyBucket/Policy";
//output a link to the file
/*echo "<tr id='del$count'><td>$count</td><td><input type='submit' name=$furl index=$count value='delete' onclick='deleteFile(\"$dispimage\",$count,\"$directory\");> <a href=\"$furl\">$fname</a>
<input type='text' id=$count value=$furl name='furl' visible=''></td></tr>";*/
echo "<tr id='del$count'><td>$count</td><td>$fname</td><td><input type='button' id='delete$count' value='Delete' onclick='deleteFile(\"$fname\",$count,\"$bucketName\");'></td></tr>";
$count ++;
}
echo '</table>';
echo "</form>";
?>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function deleteFile(fname,rowid,bucketName)
{
$.ajax({ url: "deletes3file.php",
data: {"fname":fname,"bucketName":bucketName},
type: 'post',
success: function(output) {
alert(output);
$("#del"+rowid).remove();
}
});
}
</script>
而不是在 HTML 中附加事件处理程序并传递参数:
- 使用 HTML5
data
属性将数据与 HTML 个元素相关联。 - 在 javascript 中附加事件处理程序。
这是更好的做法,因为它将文档结构与动态功能分开,从而提高 HTML 和 javascript 的可读性。
因此,按如下方式构建行:
echo "<tr data-fname=\"$fname\" data-bucket-name=\"$bucketName\"><td>$count</td><td>$fname</td><td><input class=\"deleteFile\" type=\"button\" value=\"Delete\"></td></tr>";
注意:不需要任何 ID
并编写相应的脚本如下:
jQuery(function($) {
$(".deleteFile").on('click', function(event) {
event.preventDefault(); // probably not necessary but won't do any harm.
var $row = $(this).closest('tr'); // the clicked button's row.
$.ajax({
'url': 'deletes3file.php',
'data': $row.data(), // the data object is composed for you automatically
'type': 'post',
'success': function(output) {
$row.remove(); // here, you already have a reliable reference to the row that is to be deleted, so just remove() it.
}
});
})
});