how to prevent html form reset if submit is unsuccessful - php

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.

Related

Form submission preview with form validation (PHP)

This is going to sound like a dumb question, so I apologize upfront.
I have a PHP form, with its action posting to itself, so that I can do some validation to make sure text boxes, a few checkboxes and a couple radio buttons are selected. After I confirm there are no form errors, I need to let the user preview what they've entered before letting them submit the information to a database.
For previewing form submission details, logically page1.php would have <form action="page2.php"> and page2.php would allow the user to preview what they've submitted.
But for form validation, logically, the form should submit to itself so that it can validate all required fields are entered while on the same page.
So, is there a way that I can direct the user to a "next page" after the form has validated by submitting to itself, so that they can preview the information?
Note:
I am rewriting a classic ASP form, which does this:
if errorMsg = "" then
response.redirect "verify.asp"
else
session("errorMsg") = errorMsg
response.redirect "default.asp"
end if
I assume in the case of PHP, the "else" portion of a similar conditional would be unnecessary since it already posts to itself, it's only when the error message array is empty that it should redirect to the verify page.
You can use header to perform a redirect:
if(empty($errorMsg)) {
header("Location: verify.php");
exit;
}
Using Mark Heintz's answer and some googling, I figured out the best way to do it is with a combination of Javascript and a meta tag. The meta tag will account for when javascript is disabled.
Redirect in PHP without use of header method
Redirecting to a relative URL in JavaScript
There is also an AJAX way of doing it: Modify the URL without reloading the page

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.

Preserving data over multiple form submission

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" />

A form with 3 submits; how to keep data for final submit?

I have created a PHP form which requires the user to select a postcode from a list of postcode values.
The user presses submit two times:
- once to go to address select menu which will display a select drop-down with values
- second presses "ok" button to select the address corresponding to his postcode value
I need to keep the value of the selected postcode value for when the form gets submitted. I have tried setting up the postcode drop-down value chosen in a SESSION... but it gets lots when user presses form submit.
How can I keep all the form values even after refreshing the page when the user presses one of the submits?
"How can I keep all the form values even after refreshing the page when the user presses one of the submits?"
Reading your question I didn't understand if each of the form submits actually gets submitted to the server, but I'm going to assume so. I'm also assuming you're trying to use PHP sessions to accomplish this.
When the user submits the form, save the values server-side in a PHP session
//Start the session
session_start();
//Save the values
$_SESSION["foo"] = $_POST["bar"];
...
If, after choosing the address, the user gets redirected to the initial form and you want to populate that:
//Start the session
<?php
session_start();
?>
<!-- Populate HTML form based on previously submitted values -->
<input type="text" name="foo" value="<?php echo $_SESSION["foo"] ?>" />
...
After the final submit you should have all the submitted values saved in the $_SESSION array. Don't forget to always session_start() before trying to handle anything session related.
The short answer is I don't think you can track form data across multiple forms.
I haven't seen your project and so might not fully understand the requirements, but I would suggest you consider using AJAX. Check out the jQuery post() manual, it's really simple actually. This has the advantage of allowing you to update the page once your first form has been completed.
EDIT: Sorry I meant you can't access multiple form data in a single $_POST. Of course you could store it in $_SESSION (remember to start your session properly).

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