navigating back with PHP - php

I have read a few other posts and try to put them together but still could not solve my problem: Navigating back with PHP form submission, php back button
I'm writing template for a long online form, and I'm trying to break the form into several pages. And no matter if users click "back" or "next", the previously entered fields will still remain there.
However, whenever I click on the back button on the second page, all the info on the first page goes away.
This is the php block on top of the 1st page:
session_start();
$_SESSION['initialized'] = 'Aquafina';
if (!isset($_SESSION['email'])) {
$_SESSION['email'] = '';
}
if (!isset($_SESSION['fname'])) {
$_SESSION['fname'] = '';
}
foreach ($_SESSION as $key => $value) {
$temp = trim($value);
${$key} = $temp;
}
HTML of the 1st page:
<form method="post" action="secure.php">
<p>
<label for="email">Email: </label>
<input type="text" name="email" value="<?php htmlentities($email); ?>">
</p>
<p>
<label for="fname">First Name: </label>
<input type="text" name="fname" value="<?php htmlentities($fname); ?>">
</p>
<p>
<label for="hidden"> </label>
<input type="submit" name="next" value="NEXT">
</p>
</form>
I've tried many alternatives, and currently I'm stuck on the notion that I'll pass the form into a page call "secure.php" (I'm not sure why I did this in the first place), and has the code of the following:
session_start();
foreach ($_POST as $key => $value) {
$_SESSION[$key] = trim($_POST[$key]);
}
header('Location: form2.php' );
The second page looks similar, but I added a back button that caused all the problems.
The php block on top of 2nd page:
session_start();
if (isset($_SESSION['initialized'])) {
if (!isset($_SESSION['lname'])) {
$_SESSION['lname'] = '';
}
if (!isset($_SESSION['university'])) {
$_SESSION['university'] = '';
}
foreach($_SESSION as $key => $value) {
$temp = trim($value);
${$key} = $temp;
}
} else {
header('Location: form.php');
exit;
}
The HTML on the 2nd page:
<form method="post" action="secure2.php">
<p>
<label for="lname">Last Name: </label>
<input type="text" name="lname" value="<?php htmlentities($lname); ?>">
</p>
<p>
<label for="university">University: </label>
<input type="text" name="university" value="<?php htmlentities($university); ?>">
</p>
<p>
<label for="hidden"> </label>
<button class="back">Back</button>
<input type="submit" name="review" value="REVIEW">
</p>
</form>
Thank you so much whoever can help!!!!!

I don't think the problem is your back button. The problem is probably because you're not echo-ing anything in your inputs' values.
To fix it, replace
<input type="text" name="email" value="<?php htmlentities($email); ?>">
With
<input type="text" name="email" value="<?php echo htmlentities($email); ?>">
OR
<input type="text" name="email" value="<?=htmlentities($email)?>">
And do the same thing for every of your inputs. If you didn't notice what I've added, I've added the echo in the value attribute of your HTML.

Related

checking users html form info with PHP on same page

I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>

PHP form reset button - clear session with method="get"

I have created a multipage form that is storing the data in sessions. Instead of using method="post" I am using method="get" to allow the user to click the back button and hold the data on previous pages to edit. I also have email validation happening in the PHP script.
How would I be able to have a submit button advance to the next page in the form and a Reset button to clear the session and stay on this page (or reload the page) without the session data appearing within the form fields?
I have tried modifying someone else's suggestion using the following without success. Here is the PHP script at the start of the document:
<?php
session_start();
foreach ( $_GET as $key=>$value ) {
if ( $key!="submit" ) {
$value = htmlentities(stripslashes(strip_tags($value)));
$_SESSION[$key] = $value;
}
}
$firstname = $_SESSION['Firstname'];
$lastname = $_SESSION['Lastname'];
$email = $_SESSION['Email'];
if(isset($_GET['clear'])) {
session_destroy(); // Or other session-unsetting logic
header("Location: form.php"); // Reload your page
}
else if(isset($_GET['submit'])) {
//next page logic
header("Location: form-2.php"); // Reload your page
}
?>
Here is the form within the page body:
<form class="mod-columns-form" method="get" name="mailform" action="form-2.php" onsubmit="return validateForm();">
<fieldset>
<label for="Firstname">First Name <span class="mod-error">*</span></label>
<input type="text" name="Firstname" id="Firstname" placeholder="first name" value="<? echo $firstname; ?>" />
<label for="Lastname">Last Name <span class="mod-error">*</span></label>
<input type="text" name="Lastname" id="Lastname" placeholder="last name" value="<? echo $lastname; ?>" />
<label for="Email">Email <span class="mod-error">*</span></label>
<input type="text" name="Email" id="Email" placeholder="yourname#domain.com" value="<? echo $email; ?>" />
<button name="submit" type="submit" value="submit" class="submit">Next</button>
<button name="clear" type="submit" value="clear">Clear</button>
</fieldset>
</form>
to free you from the data in the form , you can create a button , which perform reset the variables.
<button name="reset" value="resert">RESET</button>
you need to check the session this page, creating a small code block in php .
<?php
session_start();
if (!isset($_SESSION['Firstname']) && $_SESSION['Lastname'] && $_SESSION['Email'])
{
print "error";
}
?>

save fields values during form submit

I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.

How to get form fields refilled when data is incorrect

The script for handling the action of my form redirects to the form page if the values are not in proper format. I want to fill the textfields and textarea with the faulty data the user entered on redirect to the form page. I have written the following script which redirects the page on wrong value submission, but does not fill the fields thereafter.
script on form page:
<?php
if(session_id('stat')=="true")
{
$isbn=$_SESSION['var1'] ;
$name=$_SESSION['var2'] ;
$author=$_SESSION['var3'] ;
$publisher=$_SESSION['var4'];
$price=$_SESSION['var5'];
$descrip=$_SESSION['var6'];
$status=$_SESSION['stat'];
}
else
{
$isbn="";
$name="";
$author="";
$publisher="";
$price="";
$descrip="";
$status=false;
}
?>
The html part of the form:
<form action="scripts/addscript.php" method="POST" enctype="multipart/form-data" name="form1" id="form1">
<label for="isbn">ISBN</label>
<input type="text" name="isbn" id="isbn" value="<?php $isbn ?>"/>
<p>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>"/>
</p>
<p>
<label for="author">Author</label>
<input type="text" name="author" id="author" value="<?php echo $author; ?>"/>
</p>
<p>
<label for="publisher">Publisher</label>
<input type="text" name="publisher" id="publisher" value="<?php echo $publisher; ?>"/>
</p>
<p>
<label for="price">Price</label>
<input type="text" name="price" id="price" value="<?php echo $price;?>"/>
</p>
<p>
<label for="description">Description</label>
<textarea name="description" id="description" cols="45" rows="5"><?php echo $descrip; ?></textarea>
</p>
<p>
<label for="img">Select an image for the book:
<input type="file" name="img" id="img" />
</label>
<input type="submit" name="submit" id="submit" value="Submit"/>
</p>
</form>
The redirecting script on addscript.php to which the form values are submitted:
<?php
// Get values from form
$isbn=$_POST['isbn'];
$name=$_POST['name'];
$author=$_POST['author'];
$publisher=$_POST['publisher'];
$price=$_POST['price'];
$descrip=$_POST['description'];
$_SESSION['var1'] = $isbn;
$_SESSION['var2'] = $name;
$_SESSION['var3'] = $author;
$_SESSION['var4'] = $publisher;
$_SESSION['var5'] = $price;
$_SESSION['var6'] = $descrip;
if(strlen($isbn)==0||strlen($name)==0||strlen($author)==0||strlen($publisher)==0||strlen($price)==0)
{
$_SESSION['stat']="true";
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
Please telll me where is the problem and how can I solve the issue?
Thanks in advance.
session_start() must be called at the top of your PHP script to use the $_SESSION variable.

Passing a value IF set

I am trying to set up an application form on my site where there is a main large application form on one page (parent.php)and a smaller form on another page (child2.php) that when users fill out some of their details and submit that smaller form, they are taken to the larger form and the details they have entered already appear in the corresponding textbox on the larger form along side some extra boxes for them to fill out(if that makes sense!)
I can get it to work in that the textboxes on the 2nd page display the values of the matching textboxes on the first page, but only when a value is set. As users can either access the main application form through the smaller form OR by directly accessing it, I need to have it so that if the value is set, the set value is displayed and will also be the value entered into the database OR if the value is not pre-set from the smaller form, the user can enter in their info to the main form and this is what's sent to the DB. I think I might need to use ifisset and have tried to do so but am getting nowhere.
Apologies for messy code and the set up of the textboxes as they are just for testing this out and I am still getting to grips with all this and would be grateful if anyone could help me/let me know if I'm on the right track/totally off. Thanks in advance!
Page 1 (parent.php)
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value="<?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
Page 2 (child2.php)
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_REQUEST['fName'];} else { echo "first name"; }?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_REQUEST['sName'];} else { echo "surname"; }?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_REQUEST['email'];} else { echo "email"; }?>"/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_REQUEST['address'];} else { echo "address"; }?>"/>
</br>
What you're looking for are $_SESSION variables, which stay active until the user closes the browser or until the Session expires (I think the default PHP config time is 24 minutes..?). This is a lot more efficient than storing them in a database for temporal purposes.
So you can set the variables with $_SESSION['fName'] = $_POST['fName']; etc. and then call them later with echo $_SESSION['fName']; etc.
To utilize session variables you will need <?php session_start(); ?> at the beginning of your pages (before any HTML is used)
As suggested by khanahk, using SESSION variables will do your job. The problem with the solution you are thinking is that its fine only as long as you are passing values from one page to some other page, that too after so much mess.
So, you should instead, you should use something like this
session_start();
$email = $_SESSION['email'];
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php echo $email; ?>"/>

Categories