PHP is a simple example of uploading images using AJAx.

Source: Internet
Author: User

In the end, we use the FORM in HTML to implement the upload. All right, now I will write what I know and use it for learning. Of course, this is just my own study notes. If there is something wrong, I hope you can give me some advice.

First, let's make it clear that we still use <from… to upload images .... Enctype = "multipart/form-data">.

But how can I display the uploaded image without refreshing it? Here we need to understand <iframe> </iframe>.

Anyone who has used this tag knows that it can divide your page into several parts, and each part can have its own url, that is, display several different pages. Speaking of this, some friends may already know what this is about. That's right. This principle is used to upload images. It looks like on the same page, there is no jump, and in fact it has already jumped, but we didn't see it. Okay. Here is a simple example. I hope it will help you all. (Php + html)

The first is the index. php page. The code is as follows:

The code is as follows: Copy code

<Form id = "content" action = "hello. php" method = "post" target = "yframe" enctype = "multipart/form-data">
<Input type = "file" name = "tValue">
<Input type = "submit" value = "submit">
<Iframe name = "yframe" src = "hello. php" style = "border: none;"> </iframe>
</Form>
<Input type = "text">

Then there is the hello. php page, that is, the page for uploading images.

The code is as follows: Copy code

<? Php
// Echo $ _ FILES ["tValue"] ["tmp_name"];
If (move_uploaded_file ($ _ FILES ["tValue"] ["tmp_name"], "img/a.jpg "))
{
Echo " ";
}
?>

This can be said to have been done, and these lines of code can implement the results we need.

The following is an analysis:

First, there is a target in form, and its value is an iframe in it. This means that after we submit the target, the page will be displayed in this iframe,

According to the iframe definition, its content is hello. php. Therefore, after the form is submitted, it is directly included in the iframe, so the page will not jump, but we have finished

In. php: After the image is uploaded, it will display the image. php content is displayed in iframe, so the final result we see is that the page is not redirected, and we can make an effect similar to ajax implementation, the uploaded image can be displayed at the moment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.