如何使用ajax将输入文件数据值发送到php页面

How to send input file data value using ajax to a php page(如何使用ajax将输入文件数据值发送到php页面)
本文介绍了如何使用ajax将输入文件数据值发送到php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想要做的是每当用户选择图片并单击按钮时,它会将图像移动到特定文件夹并将链接保存到数据库 user_image 列.

What I want to do is whenever a user selects a picture and click the button it will move the image to a specific folder and save the link to the database user_image column.

我的问题是在我单击提交按钮后,图片的实际名称没有保存在数据库列中.例子 Oppa/upload/ 即保存在数据库中的值,无图片文件名.

My problem is the actual name of the picture is not save in the database column after i click the submit button. example Oppa/upload/ thats the value saved in the database no picture file name.

我认为 photo.php 没有收到文件的值,谁能帮我解决.

I think the value of the file didnt receive by photo.php can anyone help me solve it.

<input type='file' id="imageInput" name="imageInput" accept="image/*" />
<button  id="changePicture" name="changePicture">Submit</button>

脚本:

var data = {};
        data.imageInput = $('#imageInput').val();
        data.email = $('#email').val();

        $.ajax({
            type: "POST",
            url: "Oppa/view/photo.php",
            data: data,
            cache: false,
            success: function (response) {

            if (Number(response) == 1)
                {
                   $("#dialog-confirm-changedImage").dialog("open");
                }
            }
        });
            return false;

照片.php

<?php
include_once('../dbc/database.php');

$db = new Connection();
$db = $db->dbConnect();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


$email = isset($_POST['email']) ? $_POST['email'] : "";

$image = addslashes(file_get_contents($_FILES['imageInput']['tmp_name']));
$image_name = addslashes($_FILES['imageInput']['name']);
$image_size = getimagesize($_FILES['imageInput']['tmp_name']);

move_uploaded_file($_FILES["imageInput"]["tmp_name"], "Oppa/upload/" . $_FILES["imageInput"]["name"]);
$location = "Oppa/upload/" . $_FILES["imageInput"]["name"];


if(!empty($_POST['email'])) {

        $q = "UPDATE tbl_user SET user_image = '$location' WHERE user_email= :email ";
        $query = $db->prepare($q);
        $query->bindParam(':email', $email);
        $results = $query->execute();
        echo "1";
}

?>

推荐答案

看看这个 http://malsup.com/jquery/form/#ajaxSubmit.

包含该插件 jquery.form.js 然后试试这个.

Include that plugin jquery.form.js and then try this.

$('#FormID').ajaxSubmit({ //FormID - id of the form.
        type: "POST",
        url: "Oppa/view/photo.php",
        data: $('#FormID').serialize(),
        cache: false,
        success: function (response) {

        if (Number(response) == 1)
            {
               $("#dialog-confirm-changedImage").dialog("open");
            }
        }
    });

这应该有效.我用它来上传 ajax 图片.

This should work. I'm using it for ajax image upload.

谢谢.

这篇关于如何使用ajax将输入文件数据值发送到php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)