Solution to form insertion dilemma - php

I'm building a virtual tour application and have hit a bit of a stumbling block during the setup process. Currently, the administrator is the one responsible for determining which stop belongs to which panorama as well as the choices for the next move. In order to complete this, I need to be able to insert the photo name into the database along with the other information of the current stop.
My form solution was to add the photo names of a certain directory into an array, count the number of images in the folder, and create the corresponding number of mini-forms on the page. Each form has its own save button that works via ajax, so all information is updated as the user works through the stops.
My issue has to do with adding the photo name via the submit process. The photo itself is not a user-defined field in the form, and I'm using POST to pass the variables to the insert function. Is there a way to include the filename in the $_POST array just before submission?

what you have to do its use a hidden input with the filename in the value attribute.
<input type="hidden" name="filename" value="the filename" />
also, you can add the filename as a parameter in the ajax call, but i think that the input its cleaner.

Related

How to prevent user edit form elements name by browser console

I have a form almost all form elements are generated by clicking a button dynamically, for example
<input type="file" id="upload_1_1" name="upload_1_1">
the form elements's name is useful to my webapps which represent the position (e.g row = 1, col = 2), and it will be saved to DB later.
I found that, if the user change the name to "upload_2_1", they can still submit the form, and the $_POST will contains "upload_2_1". I use laravel 5.2, are there any ways to protect the form elements from being edit in browser console?
(the only way is check the sequence of the name in backend program?)
You cannot do it on front end level. Rather you can place a check on server side, for example: if(isset($_POST['upload_1_1]));
Sof if you have dynamic names, then you could somehow dynamically check them as well.

Store detail data in JavaScript

I have a Parent/Child form. For example, the parent table may have the following field
Number of projects completed
Have you ever worked with our company? (Yes/No radio button)
If the second option have 'Yes' value, then I need to fill the details form.
For example: "The project name done with our company, copy of contract to be uploaded etc."
I have the following logic,
The deatils form may have a ADD button to add deatils records. I think I can store the details data in JavaScript arrays? Can I store the
<input type="file" name="file_to_upload" />
in arrays ?
Then on the submit button, send the parent and child deatils to the database.
Is there any other method to accomplish the task ?
Please see the screen snap
I can only add one detail records at a time. My question is, where should I temporarily store the deatil list ?
yes, you can store the details data in javascript but you have to put that details in the form somehow, unless you send it via Ajax, but I am not sure if you can hold the image with javascript and then send it.
I think that you can also try a html array form, if you want to add a detail then just add another input with javascript, I think it looks something like this.
<input name="detail[0]" type="text"></input>
<input name="detail[1]" type="text"></input>
you can read something more about this here: http://roshanbh.com.np/2008/08/handling-array-html-form-elements-javascript-php.html.

Transfering a uploaded file from page to page

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');

PHP - multiple file upload

I am building a simple CMS for a client. They need the ability to manage employee profiles.
Each profile needs an image.
Currently I have a form for adding employees, and associating an uploaded image with a single record is easy.
Below the record add form, I have a list of existing records (and a thumbnail of the image). These records are printed between form tags, I've got a checkbox next to each record (marking it will delete it when the form submits).
I want to use this form to also UPDATE records; deletions occur first, then $_POST data is parsed and records updated.
When a record has no image associated with it, instead of a thumbnail, a file input tag is printed. Because there is a variable number of records, the file tags are all named image[] so I can easily loop through them.
Question: how do I correlate $_FILES data with $_POST data? Do I have to name each file input to image_<?=$record_id?> to determine which record the file belongs to?
Your solution looks good, i would add record id as index, for ex:
image[<?=$record_id?>];
You can then correlate by array index
instead of
<input name="image[]">
use assocative array, for ex.:
<input name="image[id_<?=$record_id?>]">
Using the default fieldname[] notation, you can't control what indexes PHP assigns to the server-side representations in POST/GET/REQUEST/FILES. PHP'll just add them sequentially and if there's a gap in your form, it'll be gone once the data hits the server.
You can, hoever, FORCE indexes, so that fieldname[7] and newimage[7] all relate to the same fieldset in your form.
You will need to name each file input, and access it via $FILES['unique_name'].

Getting $_POST variable from table

I'm trying to build a sort of resource allocation form. I'd like to be able to print a table from a database, and then allow users to click on each cell that they would like to reserve. Also, being able to drag and select multiple cells. Then send all of this via $_POST to another php script.
Problem is, I have no idea where to start.
Any suggestions?
The first and most critical thing you're going to need from what you described is a bunch of hidden fields to store the information you're interested in. You would have to write javascript code on the client side to store the users interaction with your page into these hidden fields.
To receive data via POST, you will need <input type="hidden" name"some_field"> for every bit of data you wish to "know" about that was changed on your page. Table information is not transmitted in a POST operation if it's just text, so you can't see the layout of the modified table on post back to the server.
If you don't have to POST this data to another form, it is probably a better idea to make callbacks via XMLHTTPREQUEST as the user interacts with your page, but I don't know the requirements of what you're trying to do.
I wrote one for my school recently; the trick is to either use buttons/links or addEventListener the cells to JavaScript. If you want the source code to my app, download this zip file:
http://azabani.com/files/busbook.zip
Edit:
My system works in the following way:
addEventListener to cell clicks, calling book()
book() then sets location to book.php
book.php does the database work
book.php sets the location header to immediately go back to the viewer
The system knows which week view to go back to based on session variables.

Categories