This article mainly introduces the relevant information about the nature of Ajax uploaded images. Friends who need it can refer to
1. The image is uploaded to the server.
2. The background transfers the image address to the html page and displays it in the form of an image.
3. Add the image address to the input form the day after tomorrow, and the form will be hidden.
4. The front-end deletes images and removes images and form data through js operations.
5. The image upload form is separate. Cannot be nested in the overall form.
6. Upload awesome pictures and process them. Compression, watermarking, etc.
7. Click btn to trigger image click. After the image is changed, image form submission is triggered.
Click the button
<p id="up_status" style="display:none">[站外图片上传中……(2)]</p> <p id="up_btn" class="carousel-btn"> <span>添加图片</span> </p> <p id="carousel-preview"></p>
##Upload form
<form id="imageform" class="hidden" method="post" enctype="multipart/form-data" action="{sh::U('Goods/carouselUpload')}"> <input id="carousel-photoimg" type="file" name="photoimg"> </form>
Trigger event
$("#up_btn").click(function () { $("#carousel-photoimg").click(); }); $('#carousel-photoimg').on('change', function(){ var status = $("#up_status"); var btn = $("#up_btn"); $("#imageform").ajaxForm({ target: '#carousel-preview', beforeSubmit:function(){ // 提交前,用加载的图片显示 status.show(); btn.hide(); }, success:function(){ // 提交后,加载的图片显示 status.hide(); btn.show(); }, error:function(){ status.hide(); btn.show(); } }).submit(); });
Backend verification and processing
function carouselUpload() { $path = "Uploads/Store/goods/".date("Ymd").'/'; if (!file_exists($path)) { mkdir($path, 0777, true); } $extArr = array("jpg", "png", "gif","jpeg"); if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ $name = $_FILES['photoimg']['name']; $size = $_FILES['photoimg']['size']; if(empty($name)){ echo '请选择要上传的图片'; exit; } $ext = $this->extend($name); if(!in_array($ext,$extArr)){ echo '图片格式错误!'; exit; } if($size>(2000*1024)){ echo '图片大小不能超过2M'; exit; } $image_name = time().rand(100,999).".".$ext; $tmp = $_FILES['photoimg']['tmp_name']; if(move_uploaded_file($tmp, $path.$image_name)){ echo '<p class="carousel-container" onclick="deleteImg(this);"> [站外图片上传中……(3)] <input name="goodsimg[]" value="'.$path.$image_name.'" type="hidden"> [站外图片上传中……(4)] </p>'; }else{ echo '上传出错了!'; } exit; } exit; } function extend($file_name){ $extend = pathinfo($file_name); $extend = strtolower($extend["extension"]); return $extend; }
Delete picture files
function deleteImg(obj) { $(obj).remove(); // ajax 删除图片文件 var src = $(obj).data('src'); $.ajax({ type: "POST", url: "{sh::U('Goods/deleteImg')}", data: "src="+src, success: function(msg){} }); } // 删除图片,避免造成空间不足 public function deleteImg() { if (IS_AJAX) { $src = $this->_post('src'); if(!unlink($src)) { echo "文件{$src}删除失败"; } else { echo "文件{$src}删除成功"; } } }That’s it The entire content of this article is hoped to be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website! Related recommendations:
AJAX uses post to send data in xml format to receive data
Automatically obtain the form through JS code in Ajax Element value
The above is the detailed content of Introduction to Ajax uploading images. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
