loose my mind loose my mind - 3 months ago 12
Javascript Question

upload div image to database on submit

i have been trying to get an answer on how to upload an image that was created from a div. i have the code to create the div to an image now i need to know how i can upload that image to my database folder i need to upload it on submit. with a unique file name. the one that concerns me the most is how to upload on submit. will it be possible to upload the image of div using the same code to upload regular image files?



$(document).ready(function(){


var element = $("#firstshirt"); // global variable
var getCanvas; // global variable

$("#btn-Preview-Image").on('click', function () {
html2canvas(element, {
onrendered: function (canvas) {
$("#previewImage").append(canvas);
getCanvas = canvas;
}
});
});

$("#btn-Convert-Html2Image").on('click', function () {
var imgageData = getCanvas.toDataURL("image/png");
// Now browser starts downloading it instead of just showing it
var newData = imgageData.replace(/^data:image\/png/, "data:application/octet-stream");
$("#btn-Convert-Html2Image").attr("download", "your_pic_name.png").attr("href", newData);
});

});

<center><input id="btn-Preview-Image" type="button" value="Preview"/>
<button type="button" id="btn-Convert-Html2Image" href="#">Download</button>
<br/>

</center>
<center><input type="submit" name="submit" value="Next" /></center>




this is my upload image php


<?php
require_once("configur.php");


$query='UPDATE profile_table SET images="'.$_FILES['file']['name'].'"
WHERE email= "'.$_SESSION['email'].'"';

if ($mysqli->query($query) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$mysqli->close();
?>


<?php

include('configur.php');
if($_POST)
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.



if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";



}
else
{
// check if file already exit in "images" folder.
if (file_exists("upload/" . $_FILES["file"]["name"]))
{

}
else
{ //move_uploaded_file function will upload your image.
if(move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "insert into profile_table";

if(mysqli_query($link, $query_image))
{
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
else
{
echo'';
}
}
}




}
}


?>




Answer

"insert into profile_table" is not valid MySql (or at least wont change anything), I would move
$query='UPDATE profile_table SET images="'.$_FILES['file']['name'].'" WHERE email= "'.$_SESSION['email'].'"'; to the end under the file has uploaded successfully clause and store its path in the database (change images="'.$_FILES['file']['name'].'" to images="'upload/.$_FILES['file']['name'].'"'). To access the file, you can now query the database and return the stream through another php script.