Dynamically create content from SQL database - php

So basically all I want to do is a basic image uploading page. When the user fills out a form they submit a file either from their computer or from a URL then the image gets stored on the server.
I want to then display the images on a HTML page. I already have the code to dynamically create the divs that would hold the uploaded content.
function AddTiles() {
var mydiv = document.createElement("div");
mydiv.setAttribute("id", "mydiv");
mydiv.setAttribute("class", "tiles");
mainContent.appendChild(mydiv);
}
But whenever you upload the image the page refreshes, and then the divs would no longer be there to place the image in. So I want to save the image to a database instead, and then on page load, read through the database and generate the html content and then display the images.
So if there were 4 images in your database, then the program would create 4 div tags and put each image in its respective div tag.
Also, I just need the code to read from the database and create the html content on load. I can create the database and store the images with no problem.

If you are going to read and output information stored in a database, you might as well generate the HTML content on the server-side (PHP) without using JavaScript i.e. echo "<img src='$url' />".
You can also read PHP MysQL Select tutorial for more information on retrieving data from your database.

Related

How to retrieve html code as preview among text in sql php

I submitted some text to an SQL database like this:
"You can check my photo below (img src=..myimg.jpg..)and please also comment."
Now i want to retrieve it in PHP and containing image preview. The image code is HTML.
You need to escape data from DB before presenting it in page in order to what is stored doesn't mess with your page. But if you want also show the image, then you have to extract the img src part and put it back inside escaped output in proper html tag.

Need to update data from my database to a php variable

i'm facing an issue that i'm unable to solve. I've changed my coding approach 3 times in a row. It's basically like this:
A user uploads images, these images get uploaded to a local folder + their names in a database where i then fetch them and load them.
A user may edit his images, I've done this through checkbox selection and a button called "Delete selected images". When the button is pressed, an post request through ajax is sent and the new images list is updated into the database and in the folder too without refreshing the page.
My issue is here: Say i had in the database these images: 0.png,1.png,2.png and i deleted the third image 2.png. The database field will be updated to : 0.png,1.png but then if i delete one more image let's say the first image, the database field should now be: 1.png but instead it becomes 1.png, 2.png,this is happening because the variable containing the images is not being updated from the database after every delete. Let's say $row['images'] was fetched from the database but it is not getting updated.
I need help on how to update my php variables from my database after sending the post request with ajax.

generate dynamic html pages and store in mysql using php

im doing one simple project for an advertisement agent in php. in that when the admin upload the name,images and description of the product the data need to store in mysql(completed) and a html page need to create for that particular product in that html page the data which is inserted into the database of that particular product need to display. Is that possible? Any refference is there...?
The data you want to display to the user, you can do the task via PHP. Get data from mysql via PHP & display it to the user. Why creating the separate HTML page!
Yes, Possible.
Upload Product image on the server and store images link, name and description in MySQL Table.
In your php script make connection to the database and fetch the data in your php script. After display the data as you want.
PHP CRUD Tutorial (part 1)
File Upload and View With PHP and MySQL

Combining jQuery file upload with form to mysql

I have a form that stores text fields to a database. For image upload, I am using Blueimp jQuery File upload.
I'm looking for a good way of combining these, so I can store the image URL to a field in my database.
The jquery variable file.url gives me the URL of the uploaded file (after renaming if another image has same name as uploaded file.
var link = $('<a>')
.attr('target', '_blank')
.prop('href', file.url);
console.log(file.url); // gives: http://localhost/uploads/image.jpg
Can I somehow send this URL to a hidden input field in my form? I'm aware that the image has to be uploaded before I submit form, otherwise the jQuery variable is empty.
Any suggestions?
I suggest you do this in steps. First you ask the main info and then you ask the user for the picture.
If you can't (or simply don't want it to be this way) the easiest way is to set a hidden field with this value.
As an alternative, you can upload this file to a temporary folder or with a specific name that you can associate to the data you are about to send, like an ID or session ID of the user + some unique identifier.
After you send the image via the plugin you will not need to send the image url with the form data, since you will know where it will be and can rename, move and build the url before saving the data to the database.

is it possible to create inner(nested) forms in php

is it possible to create inner( nested ) forms in php
in our application i want to upload user details to mysql server including user recent work ( that is image ). image is stored in my uploads folder and the path of that image is stored in my database.
that's way i am using inner form to upload image to uploads folder and returns uploaded image path. when the mail form submitted then user details and image path will stored in mysql database this is my idea is it possible or not please give me suggestions...
It is not possible to create nested forms in HTML, the server side language is irrelevant.
You can have multiple submit buttons and use the value of the successful one (i.e. the one that was used to submit the form) to decide if you should accept an image upload and return to the form (repopulated it with the submitted data) or process the other submitted data.
FORMS are part of HTML and not part of PHP at all. And HTML doesn't allow you to have nested FORMS. The best bet, if you want to upload the image and preview before you submit the actual user data would be to use some JS and an IFRAME to simulate nested forms.
An example of what I explained is here : http://www.openjs.com/articles/ajax/ajax_file_upload/
NOTE This is NOT an Ajax file upload (noted in the article too). But this will help you do what you are looking for

Categories