Answer the question
In order to leave comments, you need to log in
Ajax image loading, how to write to the database?
I am creating a news download page. Pictures will be added to the news via ajax, i.е. before the news itself is loaded. As in this case, they usually do with a record in the database. More precisely, how to link an already loaded picture in the database with a news item that has not yet been loaded?
Answer the question
In order to leave comments, you need to log in
I do so.
For ad goods and other crap, we load the file into a temporary folder and give the file name that we insert on the page into a hidden input. After the submission, we create a record and get its id. we sort through the array of our inputs and pull out the necessary files from tmp and transfer them to the necessary directory while simultaneously writing the post id and the name of this image to the database.
For news, it is also better to create hidden inputs. we also put our pictures in tmp so that we can insert and see them while editing our news. Some convert them to base64 but this is very inconvenient when editing news in HTML mode. After the submission, we do the same as the posts, but at the same time we do a replay in the text from tmp to the name of the directory or server in which the files were placed
It depends on what kind of editor the news has. I somehow did it on CKEditor without preloading the image into the database. see ckeditor.com/addon/image. Can you do something similar?
And why give the image and the news itself separately? Just load the image into the container when choosing a file, and when submitting, give it all at once.
The picture is in the FS, information about it (path, mime, size) is in the database table.
If the pictures are embedded in the text of the articles, then it is enough to insert the tag into the text.
Example for CKEditor
function image_callback(response)
{
if (response.is_image)
{
var thumb_name = response.file;
var full_name = thumb_name.replace('_thumb','');
$(response.target_field).val(response.file);
var image = $(response.target_image);
if (image.length)
{
image.attr('src',response.file);
}
else
{
var image_id = response.target_image.replace('#','');
$(response.target_field).before('<img src="' + response.file + '" id="' + image_id + '"/>');
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question