沃梦达 / 编程问答 / php问题 / 正文

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

How to send input file data value using ajax to a php page(如何使用ajax将输入文件数据值发送到php页面)

本文介绍了如何使用ajax将输入文件数据值发送到php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是每当用户选择图片并单击按钮时,它会将图像移动到特定文件夹并将链接保存到数据库 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页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用ajax将输入文件数据值发送到php页面

基础教程推荐