問題描述
我想要做的是每當用戶選擇圖片并單擊按鈕時,它會將圖像移動到特定文件夾并將鏈接保存到數據庫 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頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!