Inserting and retrieving id before form is submitted - php

Here is what i'm trying to do:
I have a form with few text fields and image upload via JavaScript(to have that loading animation).
here is the link http://netfecs.com/inprogress/phat_cat/admin_edit_car.php?id=4
I'm using row id as part of the image name in the database.
What is a good way to set the id and retrieve if before submitting the form because while user is filling out the form the images are being uploaded and they need an id.
Any Thoughts?

The best solution for you would be to have all the images uploaded stored in a temp location. Store that location in a session. Once the user submits the form, add the data into the database then move all the files in the session to a perm location and write to the database as needed.

I see two ways:
Make an AJAX/JSON call and create the record, returning the id
Create an empty record before displaying the form and use its id in the form

Related

How do I upload images in php after I have already saved data from the form1 and images are uploaded in form2

I have a html page, with a form and image upload,
I want to update the database with the images once uploaded, the two form work well individually, but I need to link the form-data to the uploaded images in the database.
kindly help with logic, I am considering using sessions
You can use session as you mentioned or you can updated the relevant record with the next form details.
Say for example, once first form is filled and submited by the user, add those details in database and get the ID of that record.
When the second form is submited by the user, update the same record which you have already added for form 1.
So in database you will have only one record for both the forms with all relavent detials.
You could save your images in the session if you don't want to save them in the database (which, by the way, might not be a good idea), this avoids putting stuff in the database that is not yet finally processed (as I understand: this happens when the other form is submitted?)
You could put them in a table for "temporary images" which are not yet processed finally and just keep track of the IDs through the session

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 there a way I can set the value of a file upload so it doesn't whipe on page refresh

Before my page is submitted, PHP is used to verify the integrity of all my data. I am currently using if(isset($_POST['bannerTitle'])){ echo htmlentities($_POST['bannerTitle']);} to keep the set values of my input data fields, but when the page is redrawn my uploaded file data is naturally reset to null.
Is there anything I can do to keep the selected users selected img file?
file uploads doesnt store any post data by default, but you can use $_FILE['fieldname']['name'] to retrieve its name and store at your database.

Yii prevent re-inserting data to database from uploaded file on refresh

I have an upload form, where I upload csv files and then insert all rows to the database. If I hit refresh, all content is inserted again and again into the database. What is the best prectice to prevent this?
Thanks a lot!
BR
The best way to prevent this behavior is to use a redirect to show the landing page. When a redirect is made the browser history records another url after the post action. in Yii you can achieve this by doing the following from the controller.
$this->redirect("../path/to/action");
You can add a hidden input element to the form which holds a unique ID:
<input type="hidden" name="random" value="4b3403665fea6">
If you store this value in the database you can make sure that no CSV file will be uploaded with the same unique ID (after refresh).
A unique ID can be generated with a built-in PHP function uniqid()

How to start uploading photo when the user clicks submit button?

I have one form on the page for addition and it is related with 2 tables in the database.
Eg. Say i have a table named event and one table named eventphotos. The uploadify plugin of jquery for eventphotos works just fine, however, event is the master table and eventphotos is the child table. Thus when i upload the photo due to asynchronous behaviour of uploadify the photo gets submitted on server but there is no EventId present for this photo which throws exception when i try to insert the photo into evenphotos. I hope you got the idea what the problem is.
I want that when the user clicks on the submit button on the form, the photo uploading process should start and it should only get inserted into database once the master record is inserted into table.
How do i make this happen?
Thanks in advance :)
One solution: Save the image somewhere in a temorary folder using a random generated name, then send this random name back and insert into your form as hidden field. When the form gets submitted you check if the file still exists and load it.
Another solution: Create a table called "temporary_photos" or similar, insert your picture. And send back the ID of the record and insert it as hidden field in your form. Don't forget timestamps so that you can delete images which have never been used. And delete records if you have saved the event!

Categories