I am trying to store values from multiple forms in the $_POST variable. The forms are on different pages and from what I can understand from the test I did, when I submit values from a form, they "overwrite" the values that where already in $_POST.
Here is an example of what I am trying to do :
Page 1 - form 1
<?php
session_start();
?>
<form method="post" action="page2.php">
First name : <input type="text" name="firstName" required ><br/><br/>
Last name : <input type="text" name="lastName" required ><br/><br/>
<input type="submit">
</form>
Page 2 - form 2
<?php
session_start();
?>
<form method="post" action="page3.php">
Age : <input type="text" name="age" required ><br/><br/>
City : <input type="text" name="city" required ><br/><br/>
<input type="submit">
</form>
Page 3 - results
<?php
session_start();
echo $_POST['firstName'].'<br/>';
echo $_POST['lastName'].'<br/>';
echo $_POST['age'].'<br/>';
echo $_POST['city'].'<br/>';
?>
The last page shows me only 'age' and 'city'. The values from the first form on page 1 are now undefined. Here is the an example of the result I get :
Notice: Undefined index: firstName on line 4
Notice: Undefined index: lastName on line 5
65
New York
in page2.php
put this
<input type="hidden" name="firstname" value="<?=$_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?=$_POST['lastname']?>">
You can also use session variables like this:
//Page 1
<?php
session_start();
$_SESSION['user'] = array();
$_SESSION['user']['firstName'] = $_POST['firstName'];
$_SESSION['user']['lastName'] = $_POST['lastName'];
?>
//Page 2
<?php
session_start();
$_SESSION['user']['age'] = $_POST['age'];
$_SESSION['user']['city'] = $_POST['city'];
?>
//Result
<?php
session_start();
echo $_SESSION['user']['firstName'];
echo $_SESSION['user']['lastName'];
echo $_SESSION['user']['city'];
echo $_SESSION['user']['age'];
?>
As I can see in the code above, you are trying to post the data to two different files.
<form method="post" action="page2.php">
This has action page as page2.php.
<form method="post" action="page3.php">
Here you are submitting data to page3.php
And you are trying to access
echo $_POST['firstName'].'<br/>';
echo $_POST['lastName'].'<br/>';
in page3.php which you have passed to page2.php. Definitely this won't work as you can access the values submitted through a form in the action page of the form only.
Related
I've been through 20 or so different posts and I can't seem to get this right piecing together questions and answers for different aspects of what I'm trying to accomplish.
I have a form the user can fill out. I want that upon submitting the form, the choices made will be used as variables throughout the rest of the website.
My code:
<form action="page1" method="POST">
<input type="text" name="var_1">
<input type="text" name="var_2">
<input type="submit" value="Submit" class="submit" name="submit">
</form>
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['var_1'] = $_POST['var_1'];
$_SESSION['var_2'] = $_POST['var_2'];
}
?>
Next Page:
<?php
session_start();
$var_1 = $_SESSION['var_1'];
$var_2 = $_SESSION['var_2'];
?>
<?php echo $var_1';?>
<?php echo $var_2';?>
This results in blank echos and a repeated error for each variable at the top of the page:
Notice: Undefined index: var_1 in page1.php on line #
Obviously my sessions aren't making it the second page, but I don't know why or what I've done wrong. This has been pieced together from posts about adding multiple sessions, posting sessions, getting sessions.
Either Remove the action from your <form> and give the redirect to page1 in the PHP code because the PHP code below wont work and get redirected to the next page.As a result of which the session will not be set
<form action="" method="POST">
<input type="text" name="var_1">
<input type="text" name="var_2">
<input type="submit" value="Submit" class="submit" name="submit">
</form>
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['var_1'] = $_POST['var_1'];
$_SESSION['var_2'] = $_POST['var_2'];
//redirect
header("Location: page1.php");
}
?>
Or set the session in the next page without the code below
<form action="page1" method="POST">
<input type="text" name="var_1">
<input type="text" name="var_2">
<input type="submit" value="Submit" class="submit" name="submit">
</form>
page1.php
<?php
session_start();
$_SESSION['var_1'] = $_POST['var_1'];
$_SESSION['var_2'] = $_POST['var_2'];
$var_1 = $_SESSION['var_1'];
$var_2 = $_SESSION['var_2'];
?>
<?php echo $var_1;?>
<?php echo $var_2;?>
On my first page: page1.php I have an input where you type your desired name which I then want to be carried over to more than one page, so far I can get it to page2.php but on page3.php the code fails here is my code
page1.php:
<form action="page2.php" method="post">
Name: <input type="text" name="username" />
<input type="submit" name="submit" value="Submit" />
\
page2.php: (after 5 seconds the page redirects to page3.php)
<?php
echo $_SESSION['username'] = $_POST['username'];
?>
<form action="page3.php" method="post">
<input type="hidden" name="username" />
page3.php:
<?php
echo $_SESSION['username'] = $_POST['username'];
?>
These lines work on page2.php but not here which is what I can't seem to fix
Instead of giving you a fish, I'll teach you to fish:
echo $_SESSION['username'] = $_POST['username'];
This statement is echoing the value ASSIGNED to $_SESSION['username']
= Assignment
== Comparison
=== Comparison (Identical)
On page3.php is not working because you pass no value. So:
Instead of:
<input type="hidden" name="username" />
Go with:
<input type="hidden" name="username" value="".$_SESSION['username']."">
Or:
<input type="hidden" name="username" value="".$_POST['username']."">
Now it passes the value and you should get the value on page3.php. One warning is that users can edit your value with DEV tools so I suggest to pass values differently.
I need to save my submit form data like:Name for two pages...
For some reason the $_POST only saves data for the "action" page, but cannot be retrived after the action page.
Here's my code:
HTML (form):
<html>
<body> <form name="input" action="staff.php" method="post">
Username: <input type="text" name="Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the next page after submiting and it works... (staff.php)
<html>
<?php
session_start();
echo "You have choosen". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>
</html>
Ok and after age submiting Name and Age stop working... (staff2.php)
Here's the code:
<?php
session_start();
echo "You have choosen".
$_POST['Name']; //it does't show Name.. Please help!
$_POST['Age']; // it doesnt't show this either..
?>
Obviously, there is nothing wrong on the first page. So don't change anything.
The second page. The post works. Then add a hidden input to preserve it and carry it on the next one:
<?php
echo "You have chosen: ". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="hidden" name="Name" value="<?php echo $_POST['Name']; ?>" /> <!-- this one -->
<input type="submit" value="Submit">
</form>
On the third and final page. Properly concatenate the variables:
echo 'You have chosen: <br/>';
echo $_POST['Name'] . '<br/>'; // this should carry the hidden input you set on the last page
echo $_POST['Age'];
//^^ you forgot the echo
as you have a session running pass them as session variables.
$_SESSION['name'] = $_POST['name'];
I am new to php and am not quite clear on what to do to carry my information from the first page to the next and then submit it to my email when they are done filling out contact information.
I need the script to work as follows:
Step1: User clicks the input check-boxes for the field they want that is stored in an array
ex:
< input type="checkbox" name="Sound[]" value="item1" > item1
and clicks a button i have written as
< input type="image" name="Submit" class="" alt="" src="images/contact1.png" border="0" >
Step2: The information from the check-boxes they have clicked needs to be carried over to the next page where they will fill out their contact info. Name email phone etc.
ex:
<tr>
<td valign="top">
<label for="telephone">Telephone Number *</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30" style="margin-bottom: 10px;">
</td>
</tr>
Step3. All of this information should be sent to my email upon button press for me to contact them :D
<tr>
<td colspan="2" style="text-align:center;">
<input type="image" name="Contact" class="contactbutton" alt="" src="images/contact.jpg"/>
</td>
</tr>
I can pull the information from my inputs but do not know how to carry to the next page!
Can I do it all in the same php script? or does each page need a different php script?
Please help!
Thanks Paul
you can do it with a form and send it to the page2.php. value will be stored in
$_POST['S'] for the checkbox
<form action="page2.php" method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" >
</form>
------------------
page2.php
echo($_POST['S']); // will be item1
$_SESSION array is better. to use it you need to put session_start(); at start of
every page that will use your $_SESSION variable i.e
session_start();
if(isset($_POST['S'])){
$_SESSION['h'] = $_POST['S'];
echo($_SESSION['h']); } //output value in checkbox
?>
<html><body>
<form method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" value="item1" >
Once this script is run you can accesS value in $_SESSION['h'] in other pages.
the data will be deleted when you close browser.
----------------------------------
page2.php
<?php
session_start();
if(isset($_SESSION['h'])){ //check if $_SESSION['h'] has been set a value
echo $_SESSION['h']; //output value stored in var
}
?>
You will ultimately still require the use of POST data to get the checkbox status from your page.
Page 1:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['Sound'])) {
$_SESSION['Sound'] = $_POST['Sound'];
header('Location: http://www.example.com/page2.php');
exit;
}
?>
<form method="post" action="page1.php">
Sound? <input type="checkbox" name="Sound" value="item1"><br>
<input type="submit">
</form>
Page 2:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['telephone']) && isset($_POST['email'])) {
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['email'] = $_POST['email'];
header('Location: http://www.example.com/page3.php');
exit;
}
?>
<form method="post" action="page2.php">
<!-- If you want to output the previously saved data in a disabled item -->
Sound? <input type="checkbox" name="Sound" value="item1" disabled="disabled" <?php if($_SESSION['Sound'] == 'Yes') echo('checked="checked"'); ?>>
Telephone: <input type="text" name="telephone" value=""><br>
Email: <input type="email" name="email" value=""><br>
<input type="submit">
</form>
And so on and so forth for your next pages
This does not include the code for generating the e-mail via PHP but is intend to show you how you can take the form input/checkbox selections and store there values to a SESSION ARRAY. Note that in this example: The form is submitting to itself by leaving the action="" blank, but normal would submit to a external PHP file for parsing/handling.
Also, im choosing to create a random number to represent the visitor to the form if not specifically set by $_POST['user']
<?php session_start();
if (!isset($_SESSION['user'])) {$_SESSION['user']=rand(10,700);}
if (isset($_POST['user'])) {$id=$_POST['user'];} else {$id=$_SESSION['user'];}
?>
<form action="" method="post">
Sound 1:<input name="cb1" type="checkbox" value="sound1"><br>
Sound 2:<input name="cb2" type="checkbox" value="sound2"><br>
Sound 3:<input name="cb3" type="checkbox" value="sound3"><br>
<input type="submit" name="submit" value="submit"><br><br>
<?php
if (isset($_POST['submit']) && $_POST!=="") {
foreach($_POST as $key => $value) {
$_SESSION['visitor']['sounds'][$id]=array(
'selects'=>$_POST['cb1'].",".$_POST['cb2'].",".$_POST['cb3']
);
};
echo "For user ID:".$id." We echo the comma delimited stored SESSION array: ".$_SESSION['visitor']['sounds'][$id] ['selects'];
echo "<br><br>";
// Option 2 Explodes the comma delimited ['selects'] field to handle each choice seperately
$choice = explode(",",$_SESSION['visitor']['sounds'][$id] ['selects']);
echo "For an alternative, we EXPLODE the stored 'selects' field of the SESSION ARRAY and can then echo each out seperately"."<br><br>";
echo "User ".$id." Option 1 value was: ".$choice[0]."<br>";
echo "User ".$id." Option 2 value was: ".$choice[1]."<br>";
echo "User ".$id." Option 3 value was: ".$choice[2]."<br>";
echo "<br><br>";
echo "A last example we loop through the EXPLODED values and echo only those that were selected (ie: had a value)"."<br>";
foreach ($choice as $key => $value ) { if ($value!=="") {echo "Selection: ".$value."<br>";} }
}
?>
i an new to php . please help me how to remember text input in PHP forms after go back to form ?
i have an rgister page and want to save text input in database , but if a text input existing in database go back to register page but remember text input .
please help me !
Save the data in your session.
Call
session_start();
in the form.php and your target.php. In target.php parse all your $_POST/$_GET Parameters and store them in the session.
$_SESSION['user_name'] = check_for_valid_name($_POST['user_name']);
in form.php just set input default to your session Variable:
<input name='user_name' value='<?= $_SESSION['user_name'] ?>' />
edit: In form.php you can also use:
value='<?php isset($_SESSION['user_name'])?(echo $_SESSION['user_name']):(echo "") ?>'
to get rid of warnings/notices of uknown index/variable.
Use PHP session
<?php
session_start();
$_SESSION['name'] = "YourSession";
// ...
?>
You can store the details in a session variable. You'll have to learn working with sessions though.
define your input field something like this.this will retain values after submission
<input id="user_firstname" type="text" name="user_firstname" value=<?= $_POST['user_firstmane'] ?> >
<form action="action.php" method="post">
<input type="text" name="inmputname" value="<?php echo $_POST['inmputname']?>">
<input type="submit">
</form>
Hope it will help you
Just try this one,
<?php
$name = NULL;
if(isset($_POST['submit'])){
$name=$_POST['inputname'];
}
?>
And in your form,
<form action="action.php" method="post">
<input type="text" name="inputname" value="<?php echo $name;?>">
<input type="submit">
</form>