ajax(iframe)無刷新提交表單、上傳文件

字號:


    要通過ajax保存表單,一切都OK,測試也通過,卻發(fā)現(xiàn)文件沒有上傳成功!然后就有了用 ajax是否可以上傳文件的疑問。
    找遍資料得出結果:不能
    不過同時也找到了解決辦法,就是用iframe的方式來提交表單,即實現(xiàn)無刷新提交表單又可以上傳文件!
    一、HTML代碼
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>無刷新提交表單</title>
    <script type="text/javascript">
    <!--
    function callback(str){
    alert(str);
    }
    // --></script>
    </head>
    <body>
    <form name="form1" method="POST" action="../post.php" target="post_frame" enctype="multipart/form-data">
    <iframe name='post_frame' id="post_frame" mce_style="display:none;"></iframe>
    <input type="file" name="img" />
    <input name="power[]" type="text"/>
    <input type="submit" value="完成以上修改" name="submit" />
    </form>
    </body>
    </html> 二、PHP代碼(處理代碼)
    這里的代碼就想怎么寫怎么寫了,不過這個里執(zhí)行完了剛才那個HMTL頁面應該要有所表現(xiàn),這時就要用到上個頁面中的 function callback()了
    echo ("<script type="text/javascript">parent.callback('操作成功 ! ')</ script>");