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.
Related
I have a page.php where I load a .csv file, read it and eventually upload the values into a database. What I want to do is, once the file is loaded in my form, and I've read it, first I check into my database if I still have space available, and if there is no space left, I print a message saying there is no space left, do you want to upload it anyway? I can get till this point, but I don't know how to send the file that has already been loaded into my form, to the same page through a post form, or any other method.
Here is how I read the file when I upload it
$file = $_FILES['file']['tmp_name'];
but if I try to send $file variable through a hidden field in the new POST form, it doesn't work.
You cannot send a file through hidden inputs due to security reasons. what I would recommend is to save the uploaded file on server when you read it the first time. Then if user clicks on anyway, then pass this saved file as the input for this 2nd iteration.
It can not repopulate field input.
So you can
1) use ajax to check if there are enough space and show message if no space left
or
2) store data from file in file on web server, and store data about it in hidden field in html form and after second submit use stored file.
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.
My client wants to have a 3 page form. The first page allows the user to enter data including a uploaded file. the second page confirms this data. and the third page submits the data to the database and directories.
Via post, I can keep saving the data to a hidden input fields, thats no problem. My problem is the uploaded file. how do I hold that document from page to page ? I am using Cakephp but any advice would help, thanks
You can always just create the illustion that the form is utilising three different pages. Use AJAX to accept and validate/request the user confirm their submitted data. If in this view they accept it initiate a POST to submit all that data.
You really don't need three physically different files to achieve this but you can still let it appear in three stages to keep your client happy.
You just upload the file to temp directory and keep the value in hidden variables just like other form data . If form successfully submitted then the image copy to desired location other wise delete the image
You can easily fake these 3 pages using CSS. Or even 2, as "third page" is actually a server script which has nothing to do with pages in the browser.
Just make your form, then put an event on the submit button which changes divs to whatever "confirmation page" he wants. and then actually send the form using a button on this page.
that's all
An uploaded file is always held temporarily. The server env var should tell you where it is. In Ruby's rack it is stored in the params var. So I guess there is a similar params var in php which has a hash with all the necessary information.
Since the file would be uploaded on the first step, one option is to put the file's location in a hidden input field along with the rest of the data (either there, or put it in the session). With CakePHP, if your file field looks somewhat like that:
<input type="file" name="data[User][image]" id="UserImage" />
Then you will be able to capture the location through
$location = $this->data['User']['image']['tmp_name'];
Which will correspond to something like /var/tmp/xxxxxx
On the last page, if the user confirms all the data, you just use move_uploaded_file() to put the file wherever you want on the server.
move_uploaded_file($location, '/new/location');
I have a PHP file containing a form with a few text fields (Title, description etc.)
One of the fields is Image, where I want the user to either type the address to an image (http://www.domain.com/image.jpg) or upload one.
If the user uploads an image, I store it on my server and want to populate the text field in the form with the address to the file on my server (http://www.mydomain.com/images/image.jpg)
I have tried to solve it by adding an upload button next to the Image text field, which opens a jQuery UI dialog that contains a form with a File field and a Submit button.
It works fine, uploads the image and adds a record in a database table, but I don't know how to populate the Image text field in the first form with the address of the uploaded file.
Can I do it with PHP, should I use jQuery or should I do it in a totally different way ?
Html input type image do not have a text or value property to set. It has a src property which is the image url which it renders. In your case instead of using image input element you can show a span with the updloaded image url as inner text or html once the upload is complete.
Check out move_uploaded_file, put the file wherever and just put the value where you need it. You can use jQuery to change the value of the text field like $('#myfield').val('new_value');.
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