How can i pass 1st page value to 3rd page without session - php

I have 3 pages. How can I print 1st page value to 3rd page. And my code is following (it just for sample).
page1.php
<form methord="post" action="page2.php">
username:<input type="text" name="username" >
password:<input type="text" name="password" >
<input type="submit" name="">
</form>
page2.php
<?php
$u=$_POST['username'];
$p=$_POST['password'];
?>
<form methord="post" action="page3.php">
username:<input type="hidden" name="username" value="<?php echo $u?>">
password:<input type="hidden" name="password" value="<?php echo $p?>">
mobile:<input type="text" name="mobile">
<input type="submit" name="">
</form>
Now how to print username, password, mobile in page3.php ?

Mistake 1 :
You method name is wrong.
Change this
<form methord="post" action="page2.php">
to
<form method="post" action="page2.php">
Implementation
In the page2
<?php
$u=$_POST['username'];
$p=$_POST['password'];
setcookie("u", $u);
setcookie("p", $p);
?>
You can get those values in the page3.php
<?php
$u = $_COOKIE["u"];
$p = $_COOKIE["p"];
?>
The value of Username is <?php echo $u?><br>
The value of Password is <?php echo $p?>
<br>
Learn more about Cookies here
Here is the eval for you

username:<input type="hidden" name="username" value="<?php echo $u;>">

There is a typo,
replace methord to method
and simply echo username and other fields on 3rd page like
<?php echo $_POST['username']; ?>

Related

Sending form post data to session variables

I have a page with a form that needs to be sent via post.
My htaccess has redirects in it causing the post data to be lost so I would like to put that data into session variables to be picked up by another page.
The session variable "favcolor" below on the form page is there just as a tester.
The output on the second page should show the data from the form but it is not. It's an empty array. It is showing the color variable ok though.
I've cut the code right back here to the simplest few lines to test this out and can't get it to work. Can anyone help point me in the right direction here please?
Form page:
<?php
session_start();
$_SESSION['post-data'] = $_POST;
?>
<form action="zv.php" method="post">
Name:<br>
<input type="text" id="inputName" name="inputName">
<br>
Email:<br>
<input type="text" id="inputEmail" name="inputEmail">
<br>
Telephone Number:<br>
<input type="text" id="inputTel" name="inputTel">
<input type="submit" id="submit" value="Submit">
</form>
<?php
$_SESSION["favcolor"] = "green";
?>
Second page (zv.php):
<?php
session_start();
print_r($_SESSION['post-data']);
echo "<br>";
print_r($_SESSION);
?>
Output from second page (zv.php):
Array ( )
Array ( [post_data] => Array ( ) [post-data] => Array ( ) [favcolor] => green )
You need to set this ($_SESSION['post-data'] = $_POST) only if you have post request.
<?php
session_start();
if (isset($_POST['Submit'])) {
$_SESSION['post-data'] = $_POST;
header('location: zv.php');
}
?>
<form action="<?=$_SEVER['PHP_SELF']?>" method="post">
Name:<br>
<input type="text" id="inputName" name="inputName">
<br>
Email:<br>
<input type="text" id="inputEmail" name="inputEmail">
<br>
Telephone Number:<br>
<input type="text" id="inputTel" name="inputTel">
<input type="submit" id="submit" value="Submit">
</form>
Then you can access the Session data in the second page.
<?php
if (isset($_POST['inputName'])) {
session_start();
$_SESSION['post-data'] = $_POST;
header('location: zv.php');
}
?>
<form method="post" action="<?php echo $_SEVER['PHP_SELF']; ?>" >
Name:<br>
<input type="text" id="inputName" name="inputName">
<br>
Email:<br>
<input type="text" id="inputEmail" name="inputEmail">
<br>
Telephone Number:<br>
<input type="text" id="inputTel" name="inputTel">
<input type="submit" id="submit" value="Submit">
</form>

How can I keep the URL id on submit?

When I click submit, the data is submitted to the database but the URL id of the book disappears to ----book.php
I want the URL to go back to the id of the page e.g. ----book.php?id=3
Is it possible to keep the first line as action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" and add the value="<?php echo $book_id ?>"?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="book_id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly /></p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
PHP code:
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
}
Please try this code:
// save values in DB
header('Location: book.php?id=' . $book_id);
exit;
It is because $_SERVER["PHP_SELF"] contains only URL . It does not contain get queries . To solve the problem leave your action empty
e.g
<form method="post" action="">.....
You send a POST form but are trying to retrieve a GET value. Additinally, the parameter is named book_id, not id.
Use $book_id = $_POST['book_id'];
if (isset($_GET['id'])) { also won't work, for the same reasons.
There is no name with "id"
If you want to get the book id:
if (isset($_POST['book_id'])) {
$book_id = $_POST['book_id'];
}
OR modify the HTML like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly />
</p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
On the other hand, if you have problems with POST, just modify the first line of HTML for this one:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . echo $book_id; ?>">
You can use anyone of these:
PHP_SELF returns just URL. Use REQUEST_URI that returns URL with query string:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
[you can also omit action values - that will have same behavior]
or if you want just id, then use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . $_GET['id'];?>">
Note: Use validation wherever necessary. I just gave you an idea.

How to use values in the URL in PHP

I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help

Form Submit to change/modify session variables in PHP

At the top I have a session start and the session ID can be passed across all the webpages because the ID is the same on all, this also includes a session variable called 'username' with the value of Guest.
I want to change this variable to the one entered in the form.
This is my first post so sorry for any mistakes.
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
Thanks
You need to take out ./ is in the action="" quotes like below (this is because you are using the same file to process the form)... and always start your php opening with <?php
<form class="form1" method="post" action="" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
And if you want to test it try something like this:
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>
Tested the code after making the changes, and it works fine... look:
Update answer based of requests in the comments
<form class="form1" method="post" action="./" id="form1">
<fieldset>
<ul>
<p>Please enter your username to continue to the webshop.</p>
<label for="name">User Name:</label><span><input type="text" name="username" placeholder="User Name" class="required" role="input" aria-required="true"/></span>
<input class="submit .transparentButton" value="Next" type="submit" name="Submit"/>
</ul>
<br/>
</fieldset>
</form>
PUT THIS IN YOUR index.php
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
}
?>

TwoPHP blocks in file

i have two HTML forms and two PHP blocks in one file (index.php). for example i want the second php script belonged to the second form. i dont know, how to do it. What i write to the action atribute ?
here is my code:
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="htmlspecialchars $_SERVER ["PHP_SELF"]">
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php
echo $_POST ["name"];
?>
<?php
echo $_POST ["age"];
?>
Hope it helps you,
First form,
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?> ">
<input type="text" name="name"> <br>
<input type="submit" name='submit' >
</form>
<?php
if(isset($_POST['submit'])){
echo $_POST ["name"];
}
?>
Second form
<form method="post" action="<?php echo $_SERVER ["PHP_SELF"];?>">
<input type="text" name="age"> <br>
<input type="submit" name='submitsecond' > // name submitsecond indicates as second form
</form>
<?php
if(isset($_POST['submitsecond'])){
echo $_POST ["age"];
}
?>
You can use a hidden input field to distinguish both scripts. And you'll have to echo/print the script name ($_SERVER['PHP_SELF']), htmlspecialchars is not needed...
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="name_form" />
<input type="text" name="name"> <br>
<input type="submit">
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="form" value="age_form" />
<input type="text" name="age"> <br>
<input type="submit">
</form>
<?php if($_POST['form'] == 'name_form'): ?>
The name form is submitted.<br>
Name: <?php echo $_POST['name']; ?>
<?php endif; ?>
<?php if($_POST['form'] == 'age_form'): ?>
The age form is submitted.<br>
Age: <?php echo $_POST['age']; ?>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER ["PHP_SELF"]); ?>">
<input type="text" name="name"> <br>
<input type="submit" name="name_sub">
</form>
<?php
if(isset($_POST ["name_sub"])) // check if name form is submit
echo $_POST ["name"];
?>

Categories