Form data not staying consistent, resetting after submit - php

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.

Related

Submit form but user's input remain

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).

Fetching result from another website using html

I have created a simple page with 3 text boxes and three check boxes in it. What I actually want to do is; Now consider that we have a student data page called "A" and the page I have created is "B".
Assume that I entered the first name and last name in "B"(which is the page we are going to create); the first name and last name should be taken to page "A"(site from where we want result. It should be auto-filled and once the result come(I actually mean when the next page opens). a screenshot of the page should be taken and saved in a particular location.
The location is like D:\REPORT\student 1
All the screenshots must be save here
The three check boxes are three different sites. I should be able to get the result from one or more of those three sites
This is the code that I have coded.
<html>
<center><font size="24" color="White"><b>
Experiment 1</center></font></b></i>
<body bgcolor="600000" bgproperties="fixed">
<br><br><b><font color="White">
First Name:<br>
<input type="text" name="fn"><br><br>
Last Name:<br>
<input type="text" name="ln"><br><br>
File Stored Location<br>
<input type="text" name="fsl"><br><br>
<form>
<input type="radio" name="site1" value="site1"> site1 <br>
<input type="radio" name="site2" value="site2"> site2<br>
<input type="radio" name="site3" value="site3"> site3<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Is this possible?
First of all, consider using CSS for your layout. It's much cleaner then what you are using now :).
Wrap everything in a form like so:
<html>
<center><font size="24" color="White"><b>
Experiment 1</center></font></b></i>
<body bgcolor="600000" bgproperties="fixed">
<br><br>
<form action="a.php" method="POST">
<b><font color="White">
First Name:<br>
<input type="text" name="fn"><br><br>
Last Name:<br>
<input type="text" name="ln"><br><br>
File Stored Location<br>
<input type="text" name="fsl"><br><br>
<input type="radio" name="site1" value="site1"> site1 <br>
<input type="radio" name="site2" value="site2"> site2<br>
<input type="radio" name="site3" value="site3"> site3<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Then you need php in your a.php to collect the post data from your form.

data forward to search page instead of form action page

I have a newsletter form which forward the data to newsletter.php but search.php page open when click on submit my form html code is:
<form action="../newsletter.php" method="post">
<input type="text" class="field" placeholder="Your Name" name="name" />
<input type="text" class="field" placeholder="Email" name="email" />
<div class="form-buttons"><input type="submit" value="Submit" class="submit-btn" />
</div> </form>
You can check if your entire file don't have another declaration. Maybe you have another form opened, and this newsletter form is inside of the other one. This will always submit the parent form.
Look for a
Check if there is a redirect to search.php on your newsletter.php page.

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']."!";

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