Home >Backend Development >PHP Tutorial >iframe no refresh file upload implementation program_PHP tutorial
Iframe non-refresh file upload is actually to open the upload program page on the current page, which is a bit like ajax partial refresh, except that we put it into the iframe page for upload, and at the same time we hide the iframe page.
The most original and simple iframe upload example:
In this example, the iframe name is used, so when the form is submitted, the link will be opened in the iframe (that is, there is no refresh, to be precise, it should be
It feels like there is no refresh)
When the form is submitted, the startUpload method is called, which of course is defined by JS.
The front-end upload page index.html is mainly a form and a js callback function. When uploading files, the method and enctype attributes of the form must be the same as the following code. Then set the target value to the name of the iframe, so that files can be uploaded without refreshing.
The code is as follows | Copy code | ||||||||
}
|
代码如下 | 复制代码 |
if($result) |
The code is as follows | Copy code |
set_time_limit(0); if($_SERVER['REQUEST_METHOD']=='POST') { move_uploaded_file($_FILES["test_file"]["tmp_name"], dirname($_SERVER['SCRIPT_FILENAME'])."/UploadTemp/" . $_FILES["test_file"]["name"]); echo "<script>window.parent.CallbackFunction();</script>"; } ?> |
The code is as follows | Copy code |
if($result) { echo "Upload successful! The file path is: ".$file2; echo ''; echo'<script>parent.document.form1.img.value=document.getElementById("img").value;</script>'; } |
After the file is uploaded successfully, write the file path into a hidden field (img), and then use the DOM principle to send the VALUE in the hidden field to the img text box of form1 in the parent frame:
OK, let’s take a look at Tamsui.
Tamsui editor uses tinyMCE, so it is a little different.
My form page:
The code is as follows
|
Copy code
|
||||
|
// General options
The code is as follows | Copy code |
$pasteJS = "n"; echo $pasteJS; The uploaded file then goes to the editing box of tinyMCE. The uploaded file path will also be temporarily stored through userdata until it is written to the database. The path saving is also in the PHP processing file submitted to the iframe, so I won’t write much here. |