如何使用Ajax的PHP页面发送输入文件数据的价值 [英] How to send input file data value using ajax to a php page
问题描述
每当用户选择的图片,然后单击按钮,将图像移动到特定的文件夹,并保存链接到数据库user_image列我想要做的就是。
我的问题是图像的不保存在数据库中列后,我点击提交按钮的实际名称。例如 OPPA /上传/
这就是保存在数据库中的值没有图片的文件名。
我认为该文件的价值didnt通过photo.php收到谁能帮助我解决这个问题。
<输入类型=文件ID =imageInputNAME =imageInput接受=图像/ */>
<按钮ID =changePictureNAME =changePicture>提交< /按钮>
脚本:
VAR数据= {};
。data.imageInput = $('#imageInput)VAL();
。data.email = $('#邮件)VAL();
$阿贾克斯({
键入:POST,
网址:哥/查看/ photo.php
数据:数据,
缓存:假的,
成功:函数(响应){
如果(数(响应)== 1)
{
$(#对话 - 确认 - changedImage)对话框(开放)。
}
}
});
返回false;
photo.php
< PHP
include_once('../ DBC / database.php中');
$ DB =新的连接();
$ DB = $ DB->数据库连接();
$ DB->的setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
$电子邮件=使用isset($ _ POST [电子邮件])? $ _ POST [电子邮件]:;
$图像=和addslashes(的file_get_contents($ _ FILES ['imageInput'] ['tmp_name的值']));
$ IMAGE_NAME =和addslashes($ _ FILES ['imageInput'] ['名称']);
$ IMAGE_SIZE =和getimagesize($ _ FILES ['imageInput'] ['tmp_name的值']);
move_uploaded_file($ _ FILES [imageInput] [tmp_name的值],哥/上传/$ _FILES [imageInput] [名称]);
$位置=OPPA /上传/。 $ _FILES [imageInput] [名称];
如果(!空($ _ POST [电子邮件])){
$ Q =UPDATE tbl_user SET user_image ='$位置'WHERE USER_EMAIL =:电子邮件;
$查询= $ DB-> prepare($ Q);
$查询 - > bindParam(':电子邮件,$电子邮件);
$结果= $查询 - >执行();
回声1;
}
?>
看看这个 HTTP: //malsup.com/jquery/form/#ajaxSubmit 。
包括插件 jquery.form.js
,然后尝试这个。
$('#FormID)ajaxSubmit会({// FormID - ID形式。
键入:POST,
网址:哥/查看/ photo.php
数据:$('#FormID')序列化()。
缓存:假的,
成功:函数(响应){
如果(数(响应)== 1)
{
$(#对话 - 确认 - changedImage)对话框(开放)。
}
}
});
这应该工作。我使用它为Ajax图片上传。
感谢。
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.
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.
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>
script:
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;
photo.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";
}
?>
Take a look at this http://malsup.com/jquery/form/#ajaxSubmit.
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");
}
}
});
This should work. I'm using it for ajax image upload.
Thanks.
这篇关于如何使用Ajax的PHP页面发送输入文件数据的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!