iframe嵌入页面提交表单后关闭,怎样提交后让iframe再次打开
1个回答
2015-05-29 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517196
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
给你个参考:
HTML页面:
<html>
<head>
<title>上传文件</title>
<script src="jquery.js"></script>
</head>
<body>
<input type="text" name="up" id="up"/><a href="javascript:void(0);">上传</a>
<div style="width:300px;height:400px;display:none;">
<iframe src="upload.php">
</iframe>
</div>
<script>
$(function(){
$("a").click(function(){
$("div").show();
$("iframe").contents().find("#upload").click();
$("iframe").contents().find("input[type!=submit]").val("");
});
});
</script>
</body>
</html>
PHP页面:
<?php
$newfile = "";
$sub = "";
if(!empty($_POST['submit'])=="submit"){
$sub = $_POST['submit'];
if($_FILES['upload']['error']>0){
//echo "ERROR:".$_FILES['upload']['error']."";
}else{
$upload_dir = "upload";
$filename = $_FILES['upload']['name'];
if(!is_dir($upload_dir)){
if(!mkdir($upload_dir)){
echo "<script>alert('创建目录失败');</script>";
exit;
}
}else{
$str = "";
for($i=0;$i<4;$i++){
$str .=mt_rand(0,9);
}
$farr = explode(".",$filename);
$hz = end($farr);
$newfile = $upload_dir."/".time()."_".$str.".".$hz;
move_uploaded_file($_FILES['upload']["tmp_name"],$newfile);
}
}
}
?>
<html>
<head>
<title>文件上传</title>
<script src="jquery.js"></script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upload" id="upload"/><input id="form_sub" type="submit" value="上传" name="submit"/>
</form>
<?php if($newfile!=""){ ?>
<input type="hidden" value="<?php echo $newfile; ?>" id="upfilename"/>
<input type="hidden" value="<?php echo $sub; ?>" id="upsub"/>
<?php } ?>
<script>
$(function(){
var upfilename = $("#upfilename").val();
var upsub = $("#upsub").val();
if((upsub !="")&&(upfilename!="")){
$(window.parent.document).find("#up").val(upfilename);
$(window.parent.document).find("div").hide();
}
$("#upload").change(function(){
var arr_hz = ["jpg",'gif','bmp','jpeg'];
var filename = $("#upload").val();
var hz = filename.split(".");
var hzlth = hz.length;
if(in_array(hz[hzlth-1],arr_hz)){
$("#form_sub").click();
}else{
alert("文件格式不正确!");
$("#upload").val("");
$(window.parent.document).find("#up").val(upfilename);
$(window.parent.document).find("div").hide();
}
});
function in_array(param,arr){
var lth = arr.length;
for(i=0;i<lth;i++){
if(arr[i].toLowerCase()==param.toLowerCase()){
return true;
}
}
return false;
}
})
</script>
</body>
</html>
HTML页面:
<html>
<head>
<title>上传文件</title>
<script src="jquery.js"></script>
</head>
<body>
<input type="text" name="up" id="up"/><a href="javascript:void(0);">上传</a>
<div style="width:300px;height:400px;display:none;">
<iframe src="upload.php">
</iframe>
</div>
<script>
$(function(){
$("a").click(function(){
$("div").show();
$("iframe").contents().find("#upload").click();
$("iframe").contents().find("input[type!=submit]").val("");
});
});
</script>
</body>
</html>
PHP页面:
<?php
$newfile = "";
$sub = "";
if(!empty($_POST['submit'])=="submit"){
$sub = $_POST['submit'];
if($_FILES['upload']['error']>0){
//echo "ERROR:".$_FILES['upload']['error']."";
}else{
$upload_dir = "upload";
$filename = $_FILES['upload']['name'];
if(!is_dir($upload_dir)){
if(!mkdir($upload_dir)){
echo "<script>alert('创建目录失败');</script>";
exit;
}
}else{
$str = "";
for($i=0;$i<4;$i++){
$str .=mt_rand(0,9);
}
$farr = explode(".",$filename);
$hz = end($farr);
$newfile = $upload_dir."/".time()."_".$str.".".$hz;
move_uploaded_file($_FILES['upload']["tmp_name"],$newfile);
}
}
}
?>
<html>
<head>
<title>文件上传</title>
<script src="jquery.js"></script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upload" id="upload"/><input id="form_sub" type="submit" value="上传" name="submit"/>
</form>
<?php if($newfile!=""){ ?>
<input type="hidden" value="<?php echo $newfile; ?>" id="upfilename"/>
<input type="hidden" value="<?php echo $sub; ?>" id="upsub"/>
<?php } ?>
<script>
$(function(){
var upfilename = $("#upfilename").val();
var upsub = $("#upsub").val();
if((upsub !="")&&(upfilename!="")){
$(window.parent.document).find("#up").val(upfilename);
$(window.parent.document).find("div").hide();
}
$("#upload").change(function(){
var arr_hz = ["jpg",'gif','bmp','jpeg'];
var filename = $("#upload").val();
var hz = filename.split(".");
var hzlth = hz.length;
if(in_array(hz[hzlth-1],arr_hz)){
$("#form_sub").click();
}else{
alert("文件格式不正确!");
$("#upload").val("");
$(window.parent.document).find("#up").val(upfilename);
$(window.parent.document).find("div").hide();
}
});
function in_array(param,arr){
var lth = arr.length;
for(i=0;i<lth;i++){
if(arr[i].toLowerCase()==param.toLowerCase()){
return true;
}
}
return false;
}
})
</script>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询