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;?>
Related
I am trying to create a form which, when submitted, redirects the user to a specific URL which contains the content of the submission at the end of the URL.
So, for example, a simple form like this:
<form method="post">
<input type="text" name="tracking">
<input type="submit" name="submit" value="submit">
</form>
When the user types "abc" as the tracking number and clicks 'submit' they would be redirected to:
https://www.specificurl.com/abc
My question is, is this possible and if so, how can it be done?
This is what I have so far...
On the form page:
<form action="redirect_form.php" id="#form" method="post" name="#form">
<label>Enter your tracking code:</label>
<input id="tracking" name="tracking" placeholder='Enter your tracking code' type='text'>
<input id='btn' name="submit" type='submit' value='Submit'>
<?php include "include/redirect.php"; ?>
</form>
included in the redirect.php file:
<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$name = $_POST['tracking'];
if($tracking)
{
// To redirect form on a particular page
header("Location:https://specificurl.com/$tracking");
}
else{
?><span><?php echo "Please enter tracking number.";?></span> <?php
}
}
?>
Probably JavaScript will be enough here:
<input type="text" id="tracking" name="tracking">
<input type="button" name="submit" value="submit" onclick="window.location.replace('https://www.specificurl.com/'+tracking.value);">
you should use the GET method for this.
With javascript you can then extract the text input and manipulate the url to which the user is redirected to.
Hope that helps you further.
I am learning PHP and have a situation where I want the method to be POST to pass variables to my php and
1) Connect to Server2) store results in a variable3) Display an HTML Table
However, I later on in my syntax want to use GET to "recall" that variable and output the results to a CSV file. This is what I have, but when I click the button Generate nothing happens. Is this possible? If not, how should I re-write the syntax to make ithappen?
<body>
<form method="POST">
End Date:<input type="date" name="end" value="<?= $_POST['end'] ?>">
<input type="submit" name="submit" value="Go">
</form>
</body>
<?php
if (isset($_POST['submit']))
{
//Connect To Server
//Store Result Set in Variable
//Display HTML Table
}
?>
<form method="get">
<button type="submit" name="csv" value="1">Generate CSV</button>
</form>
$sql="SELECT vName,id FROM employee WHERE vName LIKE '%$my_data%' ORDER BY vName";
$result = mysql_query($sql);
if($result)
{
while($row=mysqli_fetch_array($result))
$hid='<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />';
echo($hid);
echo $row['vName']."\n";
}
How to pass the value of a hidden input field to another PHP script? I am using auto complete. how to pass the value auto complete page to index page
You have two options:
Sessions
PHP Sessions
Session support in PHP consists of a way to preserve certain data across subsequent accesses.
eg:
<?php
// Page1.php
session_start();
$_SESSION["key"] = "random value";
Then:
<?php
// Page2.php
session_start();
echo $_SESSION["key"];
// Output would then be ... random value
POST
Using the PHP $_POST
Taking what you currently have, you'd do:
<form method="post" action="somescript.php">
<input type="hidden" name="xyz" id="abc" value="<?=$row['id'] ?>" />
<button type="submit" name="submit" value="submitForm" />
</form>
Then on somescript.php if you do:
<?php
print_r($_POST);
You'll see an array with the data from your form, hidden value included
Create a form
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
And Get value on action_page.php
$_GET['xyz']
You enter your html code inside php code like this
<?php
while($row=mysqli_fetch_array($result))
{
?>
<form action="action_page.php" method="get">
<input type="hidden" name="xyz" id="abc" value="'.$row['id'].'" />
<input type="submit" value="Submit">
</form>
<?php
echo $row['vName']."\n";
}
?>
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 have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.