Pass variables to another page PHP - php

I am trying to pass 2 variables to another page and 1 var is required. I don't know why my code is not working properly. Take a look pls.
Prova1.php :
<html>
<body>
<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
$nameErr="";
$name="";
$url="Prova1.php";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["name"])){
$nameErr = "Name is Required";
}else{
$url="active.php";
}
}
?>
<form method="post" action="<?php echo $url ?>" >
Name: <input type="text" name="name"/>* <?php echo $nameErr;?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit"/>
</form>
</body>
</html>
active.php :
<html>
<body>
Welcome <?php echo $_POST["name"]; ?> </br>
Your email is <?php echo $_POST["email"]; ?>
</body>
</html>

When the user fills in the required information, you should use a redirect, not another form, to go to active.php. You can pass variables using a session.
<?php
session_start();
if (isset($_POST['name']) && !empty($_POST['name']) {
$_SESSION['name'] = $_POST['name'];
$_SESSION['email'] = $_POST['email'];
header("Location: active.php");
} else {
$nameErr = '';
if (isset($_INPUT['submit'])) {
$nameErr = 'Name is required'];
}
?>
<html>
<body>
<form method="post" action="" >
Name: <input type="text" name="name" required/>* <?php echo $nameErr;?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit" name="submit"/>
</form>
</body>
</html>
<?php
}

Following your logic, I think your code should be something like this:
prova1.php
<html>
<style>
.error {color: #FF0000;}
</style>
<body>
<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
$nameErr = "";
$name = "";
$url = "active.php";
if(isset($_GET['error'])){
if($_GET['error'] == 1){ //code for name error
$nameErr = "name is required";
}
}
?>
<form method="post" action="<?php echo $url ?>" >
Name: <input type="text" name="name"/>* <?php echo $nameErr; ?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit"/>
</form>
</body>
</html>
active.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
header("Location: prova1.php?error=1");
}
}else{
header("Location: prova1.php");
}
?>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?> </br>
Your email is <?php echo $_POST["email"]; ?>
</body>
</html>

proval.php
if(!isset($_POST['name']) || empty($_POST['name'])){
include 'form.php';
}else{
include 'active.php';
}
form.php
<html>
<body><?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);
$nameErr="";
$name="";
if (isset($_POST["name"])){
$nameErr = "Name is Required";
}
?>
<form method="post">
Name: <input type="text" name="name"/>* <?php echo $nameErr;?>
</br>
Email: <input type="text" name="email"/></br>
<input type="submit"/>
</form>
</body>
</html>
active.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?> </br>
Your email is <?php echo $_POST["email"]; ?>
</body>
</html>

Related

PHP Session always working

I've got an if statement to check if a variable within the $_SESSION is active and set, and if it is then a message is returned to the user. Here's my header.php:
<?php
$conn = HIDDEN;
session_start();
$username = '';
$_SESSION['username'] = $username;
?>
<header>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style/main.css">
<title>webshop</title>
</header>
<div id="LogIn">
<?php
if (isset($_SESSION['username']))
{
echo "its working";
} else {
?><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 } ?>
</div>
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
}
?>
<?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'];
}
?>
The first time running, or before the user logs out (to be implemented later), the site should prompt for a username to be entered and then upon refreshing the page the welcome message should be display.
As of right now the code simply returns "it's working" despite no variable in $username existing. The code:
<?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'];
}
?>
should print out the variable underneath the welcome message, or nothing at all if it's empty. As of right now, the welcome message "it's working" is displayed always but no variables are in $username. Can anyone tell me why?
Thanks in advance.
$_SESSION['username'] is SET/NULL but is EMPTY you should try !empty() instead if isset(). See below.
<?php
if (!empty($_SESSION['username']))
{
echo "its working";
} else {
?><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 } ?>
EDIT 2
As to the comment.
IF statement needed to tell if there was a submit if there was don't display the form else display the form. See below Code
<?php
$conn = ""; //HIDDEN kept throwing error whilst I was testing
session_start();
$username = '';
$_SESSION['username'] = $username;
?>
<header>
<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="style/main.css">
<title>webshop</title>
</header>
<div id="LogIn">
<?php
if (isset($_POST['Submit'])) {
$_SESSION['username'] = $_POST['username'];
$_SESSION['username'] = $_POST['username'];
// Use the following code to print out the variables.
echo 'Session: '.$_SESSION['username'];
echo '<br>';
echo 'POST: '.$_POST['username'];
} else {
if (!empty($_SESSION['username']))
{
echo "its working";
} else {
?><form class="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']?>" 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"/> //removed css selector .
</ul>
<br/>
</fieldset>
</form>
<?php } } ?>
</div>
The isset() checks only whether the variable is set or not and it returns true since it is initialized to null string. Here you have to use !empty().
if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
}
Just a quick solution is to change
if (isset($_SESSION['username']))
to
if (strlen($_SESSION['username']) > 0)
That will work. Im guessing it because technically u did set username so it isset but if u check the length u know its not empty

I Need Tips to make my PHP password work

I have trouble with these codes:
password.php:
<html>
<head>
<title>Password!</title>
<meta charset="UTF-8">
</head>
<body>
<ul>
<form action="check.php" method="POST">
<li><input type="password" name="no1" placeholder="enter your password"></li>
<li><input type="submit" value="GO!"></li>
</form>
</ul>
</body>
Here is password2.php:
<html>
<head>
<title>Password!</title>
<meta charset="UTF-8">
</head>
<body>
<ul>
<form action="check.php" method="POST">
<li><input type="password" name="name" placeholder="verify your password"></li>
<li><input type="submit" value="GO!"></li>
</form>
</ul>
</body>
And here is check.php:
<?php
$enter = $_POST['no1'];
if (empty($enter)) {
echo "Please enter password!";
}
if (!(empty($enter))) {
echo "Your password is $enter";
}
?>
<html>
<body>
<p>Move on!</p>
</body>
</html>
<?php
$check = $_POST['name'];
if ($check == $enter) {
echo "Acces Granted";
}
if (!($check == $enter)) {
echo "Acces denied!";
}
?>
The troubles I have are:
check.php doesn't recognise "name" from password2.php
And I can't verify the password
Because $_POST variable is not persistent between requests it would not work. You can store the value from first form in the $_SESSION variable and retrieve it from session.
More info about php sessions here
Leave everything as it is in your question except check.php, here is the modified one:
<?php
//starting the session in PHP
session_start();
$enter = null;
// this is all the logic you need for retrieving `no1` from POST or SESSION
if (isset($_POST['no1'])){
$enter = $_POST['no1'];
$_SESSION['no1'] = $enter;
}elseif(isset($_SESSION['no1'])){
$enter = $_SESSION['no1'];
}
if (empty($enter)) {
echo "Please enter password!";
}
if (!(empty($enter))) {
echo "Your password is $enter";
}
?>
<html>
<body>
<p>Move on!</p>
</body>
</html>
<?php
$check = $_POST['name'];
if ($check == $enter) {
echo "Acces Granted";
// you can comment the next line if you are debugging,
// but after that you should destroy de session so you don't have a password as plain text
session_destroy();
}
if (!($check == $enter)) {
echo "Acces denied!";
}
?>

Having problems with sessions and pages in php

I really don't understand what I am doing here. I have this page profesor.php in which I want to insert some data into the database. After I submit the data from the form I want to be redirected to another page insert.php and display a message.
So I have profesor.php:
<?php
session_start();
if (isset($_SESSION['id'])) {
$fullname = $_SESSION['name'];
echo "<h1> Welcome " . $fullname . "</h1>";
} else {
$result = "You are not logged in yet";
}
if (isset($_POST['studname'])) {
include_once("dbConnect.php");
$studname = strip_tags($_POST['studname']);
$course = strip_tags($_POST['course']);
$grade = strip_tags($_POST['grade']);
$getStudidStm = "SELECT userid FROM users WHERE name = '$studname'";
$getStudidQuery = mysqli_query($dbCon, $getStudidStm);
$row = mysqli_fetch_row($getStudidQuery);
$studid = $row[0];
$_SESSION['studid'] = $studid;
$_SESSION['course'] = $course;
$_SESSION['grade'] = $grade;
header("Location: insert.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $fullname ;?></title>
</head>
<body>
<div id="wrapper">
<h2>Insert new grade</h2>
<form id="insertForm" action="insert.php" method="post" enctype="multipart/form-data">
Student: <input type="text" name="studname" /> <br />
Course : <input type="text" name="course" /> <br />
Grade : <input type="text" name="grade" /> <br />
<input type="submit" value="Insert" name="Submit" />
</form></div>
</form>
</body>
</html>
and insert.php
<?php
session_start();
if (isset($_SESSION['studid'])) {
include_once("dbConnect.php");
$studid = $_SESSION['studid'];
$course = $_SESSION['course'];
$grade = $_SESSION['grade'];
echo $studid;
echo $course;
echo $grade;
}
My problem is that insert.php doesn't display anything. I really don't understand what I'm doing wrong. Need some help.
your problem is in your form:
<form id="insertForm" action="insert.php" [...]
you send data to insert.php but all the 'magic' with
$_SESSION['studid'] = $studid;
$_SESSION['course'] = $course;
$_SESSION['grade'] = $grade;
you keep in profesor.php
Just change action="insert.php" to action="profesor.php" and it should work fine.

Create a simple PHP login with sessions and multiple users

I am searching for a answer how to create a $_SESSION login.
The result I get every time is Dexter. Even when I just press the login button. I am using sessions and no MySQL or other database. I am just in the beginning to learn PHP and have looked around here and used google but I can't relate was I am doing wrong.
The login page looks like this:
<?php
session_start();
$_SESSION['usernamne1'] = "Dexter";
$_SESSION['usernamne2'] = "River";
$_SESSION['usernamne3'] = "Miro";
$_SESSION['password1'] = "meow1";
$_SESSION['password2'] = "meow2";
$_SESSION['password3'] = "meow3";
?>
<?php //Header include fil
$page_title = 'Login'; //Dynamic titel
include('includes/header.html');
?>
<?php
echo "<h3>Login!</h3>";
echo "<br />";
?>
<form method="post" Action="sida7logged.php">
<fieldset><legend>Fyll i dina användaruppgifter</legend>
<p><label>Username: <br />
<input name="usernamne" type="text"></label></p>
<p><label>Password: <br />
<input name="password" type="password"></label></p>
<input type="Submit" value="Login">
</fieldset>
</form>
<?php //Footer include file
include('includes/footer.html');
?>
And when logged in:
<?php
$page_title = 'Logged in'; //Dynamisc title
include('includes/header.html');
?>
<?php
session_start();
if($_SESSION['usernamne1']==true || ($_POST['username']=="Dexter"
&& ($_SESSION['password1']==true || $_POST['password']="meow1"))) {
$_SESSION['usernamne1']=true;
echo "Hello Dexter";
}
elseif($_SESSION['usernamne2']==true || ($_POST['username']=="River"
&& ($_SESSION['password2']==true || $_POST['password']="meow2"))) {
$_SESSION['usernamne2']=true;
echo "Hello River";
}
elseif($_SESSION['usernamne3']==true || ($_POST['username']=="Miro"
&& ($_SESSION['password3']==true || $_POST['password']="meow3"))) {
$_SESSION['usernamne1']=true;
echo "Hello Miro";
}
else {
echo "Please login";
}
?>
<?php //Footer include file
include('includes/footer.html');
?>
You have a better example here: Easy login script without database
<?php
session_start();
$userinfo = array(
'user1'=>'password1',
'user2'=>'password2'
);
if(isset($_GET['logout'])) {
$_SESSION['username'] = '';
header('Location: ' . $_SERVER['PHP_SELF']);
}
if(isset($_POST['username'])) {
if($userinfo[$_POST['username']] == $_POST['password']) {
$_SESSION['username'] = $_POST['username'];
}else {
//Invalid Login
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
</head>
<body>
<?php if($_SESSION['username']): ?>
<p>You are logged in as <?=$_SESSION['username']?></p>
<p>Logout</p>
<?php endif; ?>
<form name="login" action="" method="post">
Username: <input type="text" name="username" value="" /><br />
Password: <input type="password" name="password" value="" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

PHP_SELF call for the same file

<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
i want only if part to run after submit and display form should not be seen after clicking submit form button
Write your html form code inside else part.
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "User Has submitted the form and entered this name : <b> $name </b>";
} else {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php } ?>
Use exit.
if(isset($_POST['submit']) {
$name = $_POST['name'];
echo "User has submitted the form and entered this name : <b> $name </b>";
exit;
}

Categories