How to POST form data from restored session? - php

I use Firefox add-on called "Session Manager" to save and restore sessions. I have simple php + html form:
<form id="form_id" enctype="multipart/form-data" method="post" action="upload.php">
<input id="name$key" type="text" placeholder="Name" name="name[]" value="$name">
<input type="file" name="fileToUpload[]" id="fileToUpload$key">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
When I restore form inputs data with "Session Manager" I can see all data I need. When I click the "Submit" button, data have empty $_POST.
What can I do to not lose this data?
Maybe to use some JQuery or session_start(); $_SESSION?

Firefox add-on "Session Manager" seems to work incorrect if html <form> setted with attribute enctype="multipart/form-data". If you want to send some files through POST use <form> attribute enctype="application/x-www-form-urlencoded" in conjunction with php copy(). That's not clean solution. Maybe there could be other solutions with enctype="multipart/form-data", maybe some expirements with form accept-charset could give you better results.

The Session Manager plugin in Firefox is not at all related to PHP sessions. Same word, entirely different meanings.
A Firefox session is your browser tabs and the websites they are accessing. A PHP session relates to a user session on a specific website.
Most likely the data you are seeing "saved" in the forms is just field data that is saved in Firefox only, for the sole purpose of making data re-entry faster. It is not yet actually "in" the form fields, but saved in Firefox (only, not on the website) in order to make easier the re-entry of frequently typed data.
When you lose a connection to a website, you lose the data typed in the fields. Refreshing the page loses the data typed in the fields. There is no work-around for this, it's just how it is.
If you have further questions, please ask in comments below this answer.
Edit:
Re-thinking, it may be possible to achieve some kind of solution using a javascript/jQuery (please, jQuery) solution that involves detected when fields are exited (blur()) and subsequent grabbing of the data and saving in localStorage.
References:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API
http://www.w3schools.com/html/html5_webstorage.asp
When is localStorage cleared?
What is the max size of localStorage values?

Related

php edit button better as a form or href link?

I'm just curious what is the better method for edit buttons in an admin, keeping in mind the there could possible be 100s of these edit buttons on a page,
a form to use the POST variables
<form action="" method="POST" enctype="multipart/form-data" target="_self">
<input type="hidden" name="ID" value="123" />
<input type="hidden" name="name" value="ABC" />
<button type="submit" name="action" value="Edit" class="edit></button>
or a href to use the GET variables
Edit
I'm just curious in regards to speed and usability.
Any opinions welcome.
Server Side
Edit button or form button do not impact server side processing at all.
If you are looking to reduce the html file size , the get variable based approach could help and reduce that less than 5% extra overhead (can vary based on type of application).
Client Side
The % impact on client side will also be negligible based on how browser handles the submit action.
Side Notes
href links work only as a replacement for GET forms and not for POST.
url based approach is useful in cases like edit button where there is no user modified input
Even better is using the HTTP PATCH method which meant to represent partial resource modification.
You should avoid using GET requests when the outcome alters a resource (because the request will be resent by hitting the back button).
However since many browsers cannot send native PATCH requests you need a bit of trickery. Ruby On Rails for example uses javascript together with a method POST parameter to fake PATCH, PUT and DELETE requests - even on links.

posting twice to the same page

I have a form to submit details to database, after processing the POST on the action page i have another form to upload a photo very closely related to the info provided on the previous form, in fact the image path is stored on the same record in the database, instead of having two pages / two steps process, is it possible to have them both on the same form?
i know that nesting forms is not possible, at the same time uploading the file requires a form.
Using anchors and GET method is not acceptable in my application for the info is too sensitive to be revealed in URL
is there a way to workaround this?
thanks in advance
You could use
either session variables (to temporarily store the first step of the form)
or javascript to cycle through steps without refreshing the page
How about using 2 fieldsets?
<form action="?">
<fieldset>
//input fields
<input type="button" value="Next" id="btnNext">
</fieldset>
<fieldset>
//foto input field
<input type="button" value="Submit">
</fieldset>
</form>
Then in JS (with jQuery):
$("fieldset").eq(1).hide();
$("#btnNext").click(function(e){
e.preventDefault();
$("fieldset").eq(0).hide();
$("fieldset").eq(1).show();
});

Store input field value in session without form submit

How can I store input field value in session without form submit?
<input type="text" size="3" name="quantity" id="quantity" value="<?php echo $product_quantity; ?>" />
PHP does not interact directly with the browser. You'll need to use javascript to obtain the value of the textbox and then append it to the url (GET OR POST).
I don't understand why you cannot use a form - they're there for the purpose of passing data to pages.
PHP is server side, as in once the page has been sent to the browser from the server, PHP is useless and has no control. PHP can process forms, use variables etc.
Javascript is the opposite. It only works once the page is sent and is entirely reliant on the browser. It can be used to auto fill forms and even submit forms for you. Because it is browser dependant, if your visitor has turned off javascript, is will not work at all.
AJAX is between these forms, so it can auto submit forms, process variables etc... It's
simply an extension of javascript, so if you know javascript, AJAX isn't that much more work.

Retaining the value in the <input type="file"> field when submit button is clicked

I have got a <input type="file" > field along with other text fields in a form, When i try to upload any files using browse button and then click submit button ,the value in the input type= "file" field disappears , I would like the browsed value to remain in the <input type="file" > field if errors are present in other fields , is there any way i can retain the value that is browsed and for it to remain in the <input type="file" > field when submit button is clicked ,
<form action="form.php" method="post" enctype= multipart/form-data>
<input type="file" value="$file" name="file"/>
<input type="text" value="$line" name="line">
<input type="submit" name="btnsubmit">
</form>
if($_POST['btnsubmit'])
{
$line =$_POST['line'];
$file =$_FILES['file'] ['name'];
if($line)
{
//do something
//conditions for file check here
}
else
//error
}
It is not possible to do this. Browser security prevents you from pre-populating the File input field, so that websites cannot steal private files of their will without the user authorizing it first (by clicking "Browse..." and choosing a file).
EDIT: It is not possible to do this natively - but you can attempt some CSS wizardry to display the previously chosen file name maybe beside the file input box to hint the user. If you want to try and be really cool, you can also attempt to overlay the browser's native file input text display area with another div that has the previous file name filled in it. But this will prevent clicking on the input area and so is user unfriendly. Too much work, little reward.
This not allowed to be set by any script for security purpose, implemented by browser vendors as file input as readonly.
There is one way to do this. Submit the form details for validation using an AJAX submission method so the user never really leaves the page or "submits" the form. That way you can validate the result server-side but the user still has all their values populated, including file inputs.
As mentioned, input[type=file] is readonly. By validating your input on the client side, you can prevent the submit to happen unless all fields are valid. ...and, in most cases, it provides a much better user experience!
Check out jquery.validation or some other validation plugin for your favourite framework, or write one yourself. Keep in mind that you should also validate on the server side.
By preventing the request, the file input will keep it's value until all fields are OK. If you need server side validation, you could also do this using ajax.

Form Textbox Caching Problem

I have a PHP form, with various input fields and textboxes. If you submit and go back, all of the data that was submitted in the input fields remains, however the textboxes are blank. How can I get the data entered in the textbox to cache like the regular text inputs?
This is a client-side issue. Although there's no way to force the browser to cache the textarea input you can send the data back yourself using cookies if you want. One easy way to do so would be to store the textarea input in cookies when the form is submitted and then to check for the cookies and insert the values into the page source on subsequent requests to the server.
Check out this page for information on setting the cookies and this one to learn how to access the information the next time your form is accessed.
I generally prefer the back button myself, but if you want to re-populate all your fields, an alternative is to have the form page submit to self and then do like this:
<form action="whatever" method="POST">
<input type="text" size="20" name="text_field" value="<?php echo $_POST['text_field']; ?>">
<textarea name="text_area"><?php echo $_POST['text_area']; ?></textarea>
<input type="submit" value="Submit">
</form>

Categories