How To Save variables from a form after the submit? PHP - php

I have an initial form that submits data to another form, and then that form submits data to itself. The problem is that once that form submits data to itself, all the data submitted from the original form is lost. Is there a way to make sure the initial data is kept after the second form is updated? I know this is possible using SESSION, but is there another way of doing this? I have read a few people saying that it can be done utilizing hidden fields, but I have no idea how to do this. All I need is a simple example to see how its done.

Let's suppose you have this tag inside a form:
<input type="text" name="foo">
You enter some value there and you will be able to reach it on server-side with $_POST["foo"]. Now, let's suppose you have another form of the response of the target page. You can include this value like this:
<input type="hidden" name="foo" value="<?php echo isset($_POST["foo"]) ? $_POST["foo"] : ""; ?>"
Here you check whether the given value was posted and if so, the posted value will be the value of the input. If not, the input's value will be empty string. When you post the form containing this hidden input, the value will be posted as well.
As you have already mentioned, another way of doing this is to store the value in $_SESSION and reuse it. Also, you may store the value in a database and load it.

Related

How to clear an HTML Form, even the form's initial values?

I have an HTML form that sends information via post to a PHP file.
On the user's second visit the page should remember the last search input. So if on their first visit they were looking for pencil then on their second visit, the form would already have prefilled the Product Name input with pencil. I'm doing this via a session variable that is shared between the two files.
For example this is what my code looks like:
<label for="minPrice">Minimum Price</label>
<input id="minPrice" type="text" value="<?php echo $_SESSION['minPrice'];?>" name="minPrice">
<input class="clearForm" type="reset" value="Clear Form">
As you can see, I'm setting the value of the input field using the session variable. Which means the initial value on the second visit of the input will be the value of $_SESSION['minPrice'], so the typical type="reset" for clearing forms doesn't work. Reset just resets the form to it's initial values.
My first thought was to unset the session variables, but that wouldn't change the current values in the input fields of the form.
There are 2 ways to make it happen
Using PHP session the correct way
Using Javascript local storage
Using PHP sessions
Make sure your .php file has session_start() at the top.
Now you need to request the server to save the value(s) you wanna use on "the next visit". This means, requesting the server without refreshing the page through an HTML form submit, using AJAX.
Following JS snippet will post a form to the server, you can modify what to post as easily as eating an apple pie.
fetch(url, {method: 'POST', body: new FormData(form)})
But you have to POST when the user types something so add an eventListener that triggers the fetch method.
document.getElementById('minPrice').addEventListener('keydown', () => {fetch...})
url is the name of the file or the url you wanna POST to,
form is the form you wanna submit, in case you wanna submit some input field(s) alone, replace new FormData(form) by {minPrice: document.getElementById('minPrice').value} and so on.
assign the fetch method to a variable and you can get the server's response using
variable.then(res => res.json()).then(response => //do whatever you want)
On the server side, get the value(s) using the superGlobal $_POST, such as $_POST['minPrice'] and you can save it in the $_SESSION['minPrice'] variable and whenever the user reloads or makes a second visit, the $_SESSION['minPrice '] will assign the last written minPrice to the input field.
Using Javascript local storage
localStorage is built-into javascript and is quite easier to use. Read more about localStorage on MDN docs. Use
localStorage.setItem('minPrice', document.getElementById('minPrice').value)
And assign the localStorage value to the field on every page load.
document.getElementById('minPrice').value = localStorage.getItem('minPrice')
That's it!
Take a look at this !
Make page to tell browser not to cache/preserve input values
Stop browser from filling textboxes with details
Alternatively, try adding this in Jquery :
$("form :input").attr("autocomplete", "off");
Use JavaScript to clear out the values of the form fields.
Something like:
<button onclick="() => {
document.querySelectorAll('input').value = '';
}" />
That way when you click the reset button, it sets all inputs value to empty string.
If you're never going to want the field autofilled by the browser it seems like you'd simply want to use the autocomplete="off" flag on the input field you desire to be dynamically filled by your php script.
You can read more about the specific of this on the MDN docs.
Basically though you'd take the input, store it as a session variable, load the next page and populate the search variable into the input field as a value and turn the autocomplete functionality off so that the browser cannot override the value you provide from the session value.
The support for for this seems fairly broad. and should in most cases prevent the browser from overriding whatever it has stored for the field.
If you're still running into issues with it filling you cvould maybe look to adding some javascript functionality with the reset() function. However depending on how this is fired it might actually end up overriding whatever you populate with the PHP function at the time the DOM is actuall rendered

How to bring first input box value to second input box with refreshing and without saving into database?

How to bring the first input box value to second input box after clicks the confirm button and refreshing the page.
But I dont want to save the value in the database, I just want to bring the value from first input box to the second input box after clicks the confirm button and refreshing the page. The first input box value will show into the second input box.
After that, How should I pass the second input box value to another page?
You have also a js API for cookies, cache and local/session storage, as well as IndexedDB, so you can use JS.
But why you need to refresh the page?? You could just put this value in a second field with JS without refreshing the page.
If you need badly to refresh the page after form validation, you have all form data sent by POST or GET, use the corresponding variables $_POST or $_GET in PHP and then echo wherever you want.
Or another option, if you need this value to be accessible all over your site/project and you use Sessions, you could save this value in the $_SESSION variable.
There are a lot of possible solutions, everything depends on your needs and goals.
Put this into top of the file
$first_value = isset($_POST['first_input']) ? $_POST['first_input'] : "";
and this into the form
<form action="_white.php" method="POST">
<input type="text" name="first_input">
<button>Confirm</button>
<input type="text" name="second_input" value="<?php echo $first_value; ?>">
</form>
It works

Unable to pre populate form with url

I am trying to pre-populate a set of form fields by passing info via parameters in the URL. I have been able to do this with html forms before by simply adding the parameters to the URL, for example ?name=John. The variable I enter usually appears in the form field.
I am finding that this approach is not working on the latest form. I have been able to identify the parameter names but when I add them to the end of the URL they are not populated in to the form.
For example using website.co.uk/admin/usersearch.php?email=test#test.com I would expect the email field to be populated with test#test.com but the page refreshes and the form is still blank.
Is this because it is a .php form? Is there anyway round this? I only have the options to use the URL or javascript.
Thanks
Give your field value as <?php echo $_GET['email'];?>
Like this :
<input type="text" name="email" value="<?php echo $_GET['email'];?>" />
There is no such default procedure for pre-populating form fields built in to any web server. So, I'm not sure how you got it working earlier. Maybe the developer had actually coded it such that the form pre-population occurred.
For the new form, you could do as Prasanth suggested. However, since you require only JavaScript or HTML, refer to this prior question for further assistance: How to retrieve GET parameters from javascript?
Basically, what you'll be doing is getting the value of the field from the url and setting the field's value to it in the form using JavaScript.

Saving multiple $_POST from a single text form

I have a page with a single form, and a submit button.
What I'm trying to achieve is when some text is entered it's saved to the $_POST array and outputted below. However, what I then want to do is use the same form to then perform the same task (albeit different text), but ensure both/multiple values are saved.
I'm assuming the best way to achieve this would be to save them to an array as the page is reloaded; but i'm not sure where to start.
Thank you.
What you could do is make some hidden fields and init them with the data from the form,
wich has been edited the first time.
Then when it is posted for the second time you could use the values from the hidden fields and the new information from the normal fields
<input type="hidden" name="hiddenFieldName" value="<?php $_POST['normalFieldName'] ?>"

sending a $id variable through hidden input tag

I have an input tag inside a form as below
I don't know if I should place the variable $id through the value property of the input tag
i don't remember but I need that value to the URL so I can pick it up after form submission.
<input type="hidden" id="parent_comment" name="comments" value="'.$id.'" />
That's how you would embed a value in a form. If you want the value to be tamper-proof, then consider encrypting it somehow (http://php.net/mcrypt for one), so that it's meaningless garbage on the client, but easily transformed back to a useful value on the server.
You could store it in the session on the server, but if the user has multiple copies of the form open, the stored value would get stomped on and submitting form copy A would probably get copy B's id value.
As for retrieving the value, it'd be in $_GET['comments'] or $_POST['comments'], depending on how you submit the form.

Categories