Submit form but user's input remain - php

How to keep user's input after they submit a form?
For example:
<form action="a.php" method="POST">
First name:<br>
<input type="text" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if(isset($_POST['submit']){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
echo $firstname.$lastname;
?>
if user key in fname and lname, yes, it will echo out.. but i wan to keep their first name and last name at the input type, so that user dont need to refill again if the form is long.

You want to make your form sticky so that the user does not need to fill the entire form again which is a good practice.
You can add value to your form inputs for your form example like shown below:
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname'];?>" />
<input type="text" name="lastname" value="<?php if(isset($_POST['lastname'])) echo $_POST['lastname'];?>" />
In the similar way, you can make your entire form sticky (different for textarea and others but the similar idea).

Related

Form data not staying consistent, resetting after submit

This is hard to explain so bear with me.
I'm building a page that consists of multiple forms that are all submitted with one submit button at the bottom like so:
<form action="" method="post">
<input type="text" name="name1" value="<?$variable1;?>" placeholder="Type here...">
<form action="" method="post">
<input type="text" name="name2" value="<?$variable2;?>" placeholder="Type here...">
<form action="" method="post">
<input type="text" name="name3" value="<?$variable3;?>" placeholder="Type here...">
<form action="" method="post">
<input type="text" name="name4" value="<?$variable4;?>" placeholder="Type here...">
<input type="submit" name="submit" value="Submit">
</form>
Each of these forms asks the user for data, which then echos to a text box so that they can copy/paste the output.
Now, suppose the user fills out the data for one of these forms only and clicks on the submit button. Naturally, the data they entered would go to the text box. What I need however is that when the user then decides to fill out the rest of the forms, the data that was originally echoed is still in the text box, preferrably as well as the answer they put in the form (however not essential).
What I'm finding at the moment is that the form resets after clicking submit, so even though the user has submitted one part of the form, when they go to fill out the rest and click submit again, only the other 3 are echoed and the first one is omitted.
I hope this makes sense. Any idea on how one would go about this?
EDIT: Nearly got it with the following:
if (isset($_POST['submit'])) {
if (isset($_SESSION['test1'])) {
$_SESSION['test1']=$_SESSION['test1'];
}
else {
$_SESSION['test1'] = $_POST['test1'];
}
}
However the variable now doesn't change when we enter something new into the form field...
<form action="" method="post">
<input type="text" name="name1" value="<?$variable1;?>" placeholder="Type here...">
<input type="text" name="name2" value="<?$variable2;?>" placeholder="Type here...">
<input type="text" name="name3" value="<?$variable3;?>" placeholder="Type here...">
<input type="text" name="name4" value="<?$variable4;?>" placeholder="Type here...">
<input type="submit" name="submit" value="Submit">
</form>
Call only once. One submit button is for 1 form. Please try this. It should work
You need <?= $variable1; ?> etc. Otherwise it doesn't output anything.

PHP Form to redirect with form results

I have a form that when they submit it, it stores the info in the database. I need to be able to get the form data to come up on redirect page.
It does not need to fetch the database as I would love to do this PHP style. So lets say they enter there name and city. When they click submit it redirects them to a thank-you page with the results from the form on that page.
In a form, you have each element have a name, lets say name="username", in the php, yould get the value of this as either a get, or a post response, depending on the method of the form.
HTML Form
<form action="process.php" method="get">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
or
<form action="process.php" method="post">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
process.php
$someusername = $_GET['username'];
$someusername = $_POST['username'];
Form page:
<form action="thanks.php" method="post"><input type="text" name="myname" placeholder="Name" /><input type="text" name="mycity" placeholder="City" /><input type="submit" value="submit"></form>
PHP Page
print "Thanks, ".$_POST['myname']." who lives in ".$_POST['mycity']."!";

HTML forms with PHP script

I am trying to enter a record in the database. This record consists of the following :
1) Unique facebook id
2) name
3) Favorite car of the person
While the first parameter I get using the graph API from facebook, the other two are entered by the user using HTML forms using some code like this :
<form action="insert.php" method="post">
Name <input type="text" name="Name" /></br>
Fav car: <input type="text" name="car" /></br>
<input type="submit" />
</form>
Now the "insert.php" script will insert the record with the database. How can I pass the first parameter, i.e. the ID, which is something that insert.php needs, but it is not a user-entered parameter.
Use a hidden parameter as follows (I assume your PHP variable containing the ID is $facebookId):
<form action="insert.php" method="post">
<input type="hidden" name="facebook_id" id="facebook_id" value="<?php echo $facebookId?>"/>
Name <input type="text" name="Name" /></br>
Fav car: <input type="text" name="car" /></br>
<input type="submit" />
</form>
You could also use pure Javascript (I assume you stored the Facebook GraphID response in facebookResponse)
<script type="text/javascript">
$(document).ready(function() {
document.getElementById("facebook_id").value = facebookResponse["id"];
}
</script>
Pass it as a hidden input from within your form.
<form action="insert.php" method="post">
Name <input type="text" name="Name" /></br>
Fav car: <input type="text" name="car" /></br>
<input name="fbID" type="hidden" value="{value_to_pass_here}" />
<input type="submit" />
</form>
This way the fbID input won't be exposed in the front end of your website but will get POSTed to your server side script along with other input parameters, and you can access it in index.php at $_POST['fbID'];
The value you want to pass, in your case the FB Key, should be in the value attribute of this input field.

Showing form data after submit

I have a form with a submit button and a handler that stores data in the database. Problem is when the form is submitted, all data is cleared from the input fields. Is there a way to still show them after submit? What changes do I need to make to my form_submit function?
function mymodule_form_submit($form, &$form_state) {
//how to retain the input in the form
}
I'm looking for the most "drupalish" way to get this done?
As indicated by this previous StackOverflow question you can accomplish this with $form_state['storage'] and $form_state['rebuild'].
You can access the data using $_REQUEST['form_variable_name'] where form_variable_name is the name of the html input tag.
You then need to render the page back putting this value into the input tags value field.
<form method="POST" action="/account/contactdetails/">
<div>
<label>First name:</label>
<input type="text" name="firstname" value="<?php echo $_REQUEST['firstname']; ?>" />
</div>
<div>
<label>Last name:</label>
<input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
</div>
<input type="submit" value="Save" />
</form>

how to pass values from one page to another on jquery form submit

I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.

Categories