Preserving data over multiple form submission - php

I am building a web service in which a user can login to a backend, submit a form and then other users will be able to see this information on the front-end of the web service.
The data is submitted to the PostgreSQL database fine BUT on the second submission of the form (users can go back to the form and update their information if neccessary) if there is a blank field in the form, the existing data in the form is overwritten with blank data.
How can I avoid this? I think it would be good to populate the form fields with data from the database and allow the user to edit the form like that but I am unsure how to implement this using PHP. At the moment a user would have to fill out the form in full over and over to prevent this kind of data loss.
Could someone give me an idea or point me in the right direction?

pre-fill the form fields with earlier entries from the DB
<input type="text" value="<?php echo $fname;?>" name="first_name" id="first_name" />

Related

Auto fill form data after autocomplete="off" and value=""

I have a simple log in form in php, and I have two forms each on in separated web-page.
when I fill the log in information and go to another form in another web-page, the new form is filled automatically with the same data of the log in form.
And when I go back to log in page after logging out, the credential fields are filled the the old data as well.
I tried value="" for each input and autocomplete="off" and the autocomplete wasn't disabled. and I don't want to cache the data on the forms.
How to prevent one form to use the data from another?
and how to prevent caching the data and autofilling?
have you tried to apply the autocomplete="off" on the form tag and not in the input tag?
<form action="" method="" autocomplete="off">
</form>
Have you tried:
autocomplete="nope"
(or any other random string value for autocomplete, as suggested by Mozilla)?
See: https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion

Passing post data from one form to second form in CodeIgniter

I have a simple lead collection form that asks for the user's zip code on the first page, and then based on which state that zip code is in it sends them to the second page of my form with the zip code already populated, or to another site. In my pre-CodeIgniter days this was a very simple thing to do, but I'm not sure how to send the zip code data from my routing script to the second page of my form and not have it mess with CI's form validation. The second page of my form currently looks like this:
<input type="text" name="zip"value="<?php echo set_value('zip'); ?>" />
So if the user submits invalid data on the second page, the form reloads with all the data pre-populated (using CI's set_value function). But what needs to happen is something like...
IF the user comes from page 1 of the form, populate the zip code field using the data from form 1. ELSE, re-populate it after a failed form submit.
Is there some way for set_value to intelligently work in both situations? If not, do I need to use a different function? I'm also not sure how to pass the zip code data from my routing script to the controller for page 2 of the form.
I'd really appreciate any help. THANK YOU!!

Build a form to send data to another

I need to build a form with a just a few fields: username, password and submit button.
Why do I need this form? In my website there is a link to let some users log into another website. In other words, I want to display a login form inside my site theme to enter login data and submit.
My question: Is there a way to send my built in form values to that form (another website form) then submit automatically?
Well, there are a few ways you can approach this.
You can have the form fields on your site but the form itself points to the desired website. E.g
<form action="http://www.other-site.com/form.php">
<input type="text" name="username" />
// and so on
Or you can grab the form data on server side and use CURL to send the data.

Form 2 sql gets fields from Form 1

I have two forms form 1 and form 2.
The form 1 has 20 fields of data and it's action is to update.
In form 2 I have another action, but I want to get the 20 fields of data from form 1.
Is there any possible way to do this and how? Also form 1 and form 2 are in the same page.
I have already a solution which is putting 20 hidden fields in form 2 so form two can
get it from there which is tedious. But can I ask you guys is there a much easier idea?
You can save all the input from form 1 in a session variable and restore it in form 2. For that purpose, write a function which stores all $_GET and $_POST variables in an element of the $_SESSION array (using the parameter name as the key).
The "problem" is something that ever PHP developer has at some point solved.
The best way to do this would be to save data from previous form in $_SESSION. But this will be a valid solution, if you are filling out the form, while in the same session.
If you are building something like a e-commerce site, where user has to fill out several payment forms, or some large test-application, where students fill out multiform tests, it might be better to store data from each form in DB and associate it to UserId. This way the user gains ability to continue filling out the form from different location.
P.S. If you are using AJAX instead, you will mostly just add another layer of complexity on top of existing application. Especially if your skills in JavaScript are limited.
I think your solution isn't bad, simple and Swift !
But the only better way, is ajax.
In ajax you are not limited to a form, you can get every fields from every forms and then send them manually.
If you are not familier with ajax, and you want to use your non-ajax solution, you can use one form and put all fields of two last forms in this new single form, and use one action file to response to this form.In server side you can check the value of a field which is unique.
client side :
<form action="test.php">
<!-- fields of form 1 -->
<input type="text" name="text1"/>
<input type="text" name="text2"/>
.
.
.
<input type="text" name="text20"/>
<!-- fields of form 2 -->
<input type="text" name="text21"/>
<input type="text" name="text22"/>
.
.
.
<input type="text" name="text40"/>
<input type="submit" value="send"/>
</form>
server side:
<?php
if ( isset($_GET["text1"]) )
//do related jobs to form 1 here, and you can use all of filled fields
else
//do related jobs to form 2 here, and you can use all of filled fields
?>

how to prevent html form reset if submit is unsuccessful

I am using a form to register the user on my website and i have a captcha security on it. Everything is working well and good but the only problem that i am facing is that if i enter a wrong captcha or somehow the page refreshes , all the data entered by the user is wiped out.
what i wish to achieve is that even if the captcha entered is wrong and the form is submitted , the form should have all the fields intact as the user filled in excluding the captcha field.
How can this be done? My form is html and the processing page is php
You can populate the value attribute of your form inputs;
<input type="text" name="username" value="<?php
if (!empty($_POST['username'])) {
echo htmlspecialchars($_POST['username']);
}?>"
/>
You need to set the form action to the current page. As long as there are errors, the same script will get called and this script may fill in the form values as described in the other answers. Only on success you will redirect the user to another page.

Categories