上传图片(AJAX伪刷新)

思路:
  采用普通表单提交,然后表单中放一个target,指向一个iframe,提交后php执行完就返回到iframe中去,把iframe设置为display:none就看不出了,
  如果提交后还行执行什么,php中就输出一段js作为回调。
 
HTML:
 
<form method='post' enctype="multipart/form-data" action='./index.php' target='hidden_form'>
<input type='file' id='myfile' name='file1' >
<input type='submit' id='mybutton'  value='12312312312'>
</form>

<iframe src='about:blank' style='display:none' id='hidden_form' >im iframe</iframe>
 
JS:

<script type="text/javascript">
function callback(mes,code){
alert(mes);
}
</script>

PHP:

echo '<script>parent.callback("ok","1")</script>'; 
 
调用自己写的上传类:

$upload = new zz_upload();
$name = $_SESSION[ZZ_SESSION_IDENTITY];//随意
$array = array(
array('path'=>ZZ_USER_UPLOAD_LOCAL.DS.'120'.DS , 'size'=>'120' , 'name'=>$name),//path,size,pic name
array('path'=>ZZ_USER_UPLOAD_LOCAL.DS.'48'.DS , 'size'=>'48' , 'name'=>$name), //path,size,pic name
array('path'=>ZZ_USER_UPLOAD_LOCAL.DS.'24'.DS , 'size'=>'24' , 'name'=>$name) //path,size,pic name
);

$upload->setName('upload_head');
$result = $upload->up($array);
if ($result !== true) {
echo $result;    //this will output the result , true or some prompt
}

posted on 2014-10-14 14:43  allen__  阅读(175)  评论(0编辑  收藏  举报

导航