Link a php page to another php page - php

I am trying to link a make a login page using php without database but the link to the other page is not working . It is staying on the same page. No error message also.
Here is the php part of the code:
<?php
$error = "";
if(isset($_POST['MRDlogin']))
{
if(isset($_POST['username'],$_POST['password'])){
/*** You can change username & password ***/
$user = array(
"user" => "MRD",
"pass"=>"msit"
);
$username = $_POST['username'];
$pass = $_POST['password'];
if($username == $user['user'] && $pass == $user['pass']){
header("Location: http://localhost/portal/MRD.php");
}else{
$error = '<div class="alert alert-danger">Invalid Login</div>';
}
}
}
and my html form part is:
> <div class="panel-body">
> <?php echo $error; ?>
> <form accept-charset="UTF-8" role="form" method="post">
> <fieldset>
> <div class="form-group">
> <input class="form-control" placeholder="Username" name="username" type="text">
> </div>
> <div class="form-group">
> <input class="form-control" placeholder="Password" name="password" type="password" value="">
> </div>
> <input class="btn btn-lg btn-success btn-block" type="submit" value="Login">
> </fieldset>
> </form>
> </div>

The following variable cannot be found
$_POST['MRDlogin']
Add it in you button as shown below
<div class="panel-body">
<?php echo $error; ?>
<form accept-charset="UTF-8" action="page.php" role="form" method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Username" name="username" type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit" value="Login" name="MRDlogin">
</fieldset>
</form>

in form tag add action tag with value of page name that will process submited form:
<form action="page.php" accept-charset="UTF-8" role="form" method="post">

Related

Signup System in PHP

I recently started coding a website with a login/signup system with php & mysql. But when i try to make the signup it give the error, that i didn't insert the username/email. I will leave the code of the signup page and the code of the "script":
Signup page:
<?php
require 'inner.php';
?>
<?php
require 'header.php';
?>
<?php
require 'main.php';
?>
<div class="signup-header">
<center>
<h1>Signup<span>Page</span></h1>
</center>
</div>
<div class="form2">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="username" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Username</span>
</label>
</form>
</center>
</div>
<div class="form3">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="email" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Email</span>
</label>
</form>
</center>
</div>
<div class="form4">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="password" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Password</span>
</label>
</form>
</center>
</div>
<div class="form5">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="rePassword" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Repeat Password</span>
</label>
</form>
</center>
</div>
<div class="signup-btn">
<form action="includes/signup.inc.php" method="post">
<button type="text" name="signup-submit">Register</button>
</form>
</div>
<?php
require 'footer.php';
?>
Script Page:
<?php
if(isset($_POST["signup-submit"]))
{
require 'dbhandler.php';
$username = $_POST["username"];
$email = $_POST["email"];
$password = $_POST["password"];
$repeatpassword = $_POST["rePassword"];
if(empty($username) || empty($email) || empty($password) ||empty($repeatpassword))
{
header("Location: ../signup.php?error=emptyfields&username=".$username."&email=".$email);
exit();
}elseif(!filter_var($email,FILTER_VALIDATE_EMAIL)&& !preg_match("/^[a-zA-Z0-9]*$/",$username)) {
header("Location: ../signup.php?error=invalidmail&username");
exit();
}elseif (!filter_var($email,FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidmail");
exit();
}elseif (!preg_match("/^[a-zA-Z0-9]*$/",$username))
{
header("Location: ../signup.php?error=invalidusername");
exit();
}
elseif($ps !== $rp) {
header("Location: ../signup.php/error=passwordcheck");
exit();
}else {
$sql = "SELECT * FROM users WHERE uidUsers=?";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt,$sql)) {
header("Location: ../signup.php/error=connectionerror ");
exit();
}else
{
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$result_check = mysqli_stmt_num_rows();
if ($result_check > 0) {
header("Location: ../signup.php/error=usertaken ");
exit();
}else {
$sql = "INSERT INTO user(uidUsers, emailUsers, pwdUsers) VALUES (?,?,?)";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt,$sql)) {
header("Location: ../signup.php/error=connectionerror ");
exit();
}else {
$hash_ps = password_hash($ps,PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "sss", $email,$username,$hash_ps);
mysqli_stmt_execute($stmt);
header("Location: ../signup.php/signup=success");
exit();
}
}
}
}
}
?>
It's like PHP can't find the username/mail value.
You need to wrap whole form inside <form> element.
In your code, you have only this little part in form, so it is not sending all of the fields you need for registration:
<form action="includes/signup.inc.php" method="post">
<button type="text" name="signup-submit">Register</button>
</form>
So it will be something like:
<form action="includes/signup.inc.php" method="post"> <!-- FORM BEGINNING HERE -->
<div class="signup-header">
<center>
<h1>Signup<span>Page</span></h1>
</center>
</div>
<div class="form2">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="username" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Username</span>
</label>
</form>
</center>
</div>
<div class="form3">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="text" name="email" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Email</span>
</label>
</form>
</center>
</div>
<div class="form4">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="password" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Password</span>
</label>
</form>
</center>
</div>
<div class="form5">
<center>
<form class="" action="includes/signup.inc.php" method="post">
<input type="password" name="rePassword" autocomplete="off" required>
<label for="name" class="label-name">
<span class="content-name">Repeat Password</span>
</label>
</form>
</center>
</div>
<div class="signup-btn">
<button type="text" name="signup-submit">Register</button>
</div>
</form> <!-- FORM ENDING HERE -->

PHP : Echo not working on POST method from form

I am a bit new to PHP and I'm having a bit of difficulties here and there. I am developing a form and wish to display an error box if any of the fields are empty when the 'Submit' Button is pressed. I've tried the following code but the echo is still not appearing. Any suggestions ?
Form Code:
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="$_POST" action="searchEmployee.php">
<br style="clear:both">
<h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
</div>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
</div>
</form>
</div>
</div>
</div>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$required = array('name', 'surname', 'ID', 'visitorCard');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
}
}
?>
You are using method as $_POST in your form attribute,
It should be only POST.
So, replace your form line with,
<form role="form" method="POST" action="searchEmployee.php">
and also, change Submit button line to,
<input type="submit" name="submit" value="Sign In Visitor" class="btn btn-primary pull-right" />
Here are few mistakes:
You are using method as $_POST in your form attribute, It should be only POST.
Your form will not submit because your button is not a submit type. it should
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
Or
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
You need to make 2 change as below
Change button type like this
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
Change Form method
<form role="form" method="POST" action="searchEmployee.php">
Change this
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
To
<input type="button" id="submit" name="submit" value="submit" class="btn btn-primary pull-right" />Sign In Visitor
and Also
this
<form role="form" method="$_POST" action="searchEmployee.php">
To
<form role="form" method="POST" action="searchEmployee.php">
How will you get POST['submit'] value when you have not even set, that means your if code won't execute it and so it won't echo or alert it.
For Best practice of debugging always try to do this whenever such instances occurs while coding.
print_r($_POST);
This will show you an array of POST variables
change $_POST to POST and put <input type="submit" instead of <input type = "button"
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="post" action="searchEmployee.php"> <br style="clear:both"> <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3> <div class="form-group"> <label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div>
<div class="form-group"> <label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required> </div> <div class="form-group"> <label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label> <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div> <div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
</div> </form> </div>
</div> </div>
Php Code:::searchEmployee.php
<?php if (isset($_POST['submit'])) { $required = array('name', 'surname', 'idCard', 'cardNumber');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) { if (!isset($_POST[$field])) {
$error = true;
} }
if ($error) {
echo "All fields are required."; } }
?>
First think change button type="submit".
Second think change PHP array
$required = array('name', 'surname', 'idCard', 'cardNumber');

not able to add data from web page to database

this is my form .html file getting connected to adddatabase.php page .having firstname and secondname
<form action="addtodatabase.php" method="post">
<div class="container">
<form class="form-inline">
<fieldset>
<legend>Security Department User Registration</legend>
<div class="form-group">
<label for="Firstname">First Name</label>
<input type="text" class="form-control" id="Firstname" name="firstname" placeholder="Text input"><br/>
</div>
<div class="form-group">
<label for="Secondname">Second Name</label>
<input type="text" class="form-control" id="Secondname" name="secondname" placeholder="Text input"><br/>
</div>
</form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
addtodatabase.php page as follows .
if (isset($_POST)) {
$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : '';
$secondname = isset($_POST['secondname']) ? $_POST['secondname'] : '';
echo 'Your first name is ' .$firstname. '<br>';
echo 'Your second name is ' .$secondname. '<br>';
}
mysqli_query($connect,"INSERT INTO form_details(firstname,secondname)
VALUES('$firstname','$secondname')");
You have form inside a form which is wrong. Do it like this:
<form method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
</form>
Also, put your submit button inside the form:
<form method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
<input type="submit" value="Submit" />
</form>
Or if the submit button is outside, set the form attribute of the button to the id of the form:
<form id="myForm" method="POST" action="addtodatabase.php" >
<!-- Your inputs -->
</form>
<input type="submit" value="Submit" form="myForm" />

Variable PHP in a String with HTML Tags

I need pass this string to the HTML. I have this now:
if(empty($_SESSION['usuario'])){
$html = '<form class="form-inline formulario5" action="datos.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label for="user">Usuario</label>
<input type="text" class="form-control" id="user" placeholder="" name="user"/>
</div>
<div class="form-group">
<label for="password">Contraseña</label>
<input type="password" class="form-control" id="password" placeholder="" name="password"/>
</div>
<button type="submit" class="btn btn-success">Log-In</button>
</form>';
}else{
$html = '<h3><?$_SESSION['usuario']?></h3>';
}
To this HTML:
<div class="container">
<h1>Producto</h1>
<h2 class="absorver">App</h2>
<?echo $html?>
<hr />
</div>
The problem is that i can´t pass the $html with the $_SESSION.
try this:
if(empty($_SESSION['usuario'])){
$html = '<form class="form-inline formulario5" action="datos.php" enctype="multipart/form-data" method="post">
<div class="form-group">
<label for="user">Usuario</label>
<input type="text" class="form-control" id="user" placeholder="" name="user"/>
</div>
<div class="form-group">
<label for="password">Contraseña</label>
<input type="password" class="form-control" id="password" placeholder="" name="password"/>
</div>
<button type="submit" class="btn btn-success">Log-In</button>
</form>';
}
else
{
$sess = $_SESSION['usuario'];
$html = "<h3>". $sess . "</h3>";
}

How to append form data to the same page?

I currently have two forms that appear when two different buttons are clicked. So, when button a is clicked, then button b appears and button a is still on the screen. I would like it so when the page loads, that both buttons are side by side. Also, when I submit the form data for form A it works and goes to the top left corner of the screen where I want it, but when I submit form B, the data from form B appears under the last input field when I click button A to show button A's form. I would like for when I submit form b, it's data to appear under from a's already added data to the web page please, not under a's form when I click button a to input form data into form a's input field.
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="index.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
</body>
</html>
<div id="formData" >
<?php
#FOR THE DRIVERS ONLY
session_start();
if (isset($_POST['clear'])) {
$_SESSION['inputs'] = array();
}
if (!empty($_POST['name']) && !empty($_POST['age']) && !empty($_POST['departLoc'])
&& !empty($_POST['arriveLoc']) && !empty($_POST['departDate']) && !empty($_POST['returnDate'])
&& !empty($_POST['desiredNum'])) {
if (!isset($_SESSION['inputs'])) {
// initialize the saved input session variable the first time
$_SESSION['inputs'] = array();
}
$_SESSION['inputs'][] = $_POST;
foreach ($_SESSION['inputs'] as $input) {
echo " <p class='driverAlert' > DRIVER </p> <img src='taxi.png' width ='50' height='50'title='driver'> <span class='trip'> {$input['name']}, Age: {$input['age']} <br> Is planning to go to {$input['arriveLoc']} from {$input['departLoc']}<br>
Leaves on {$input['departDate']} and returns on {$input['returnDate']} <br> Will drive up to: {$input['desiredNum']} people <br> <br></span>
";
}
}
?>
</div>
<input class="btn btn-default" type="button" id="driverbtn"value="Driver">
<div id="driver">
<form action="index.php" id="driverForm" method="post" class="form-inline">
<div class="form-group">
<input type="text" class="form-control input-sm" id="desireNum" name="desiredNum" placeholder="Number of potential riders">
</div><br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="name" name="name" placeholder="Enter Name">
</div><br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="age" name="age" placeholder="Enter Age">
</div> <br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="depart" name="departLoc" placeholder="Departure Location">
</div><br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="arrive" name="arriveLoc" placeholder="Destination Location">
</div><br> <br>
<div class="form-group">
<input type="date" class="form-control input-sm" id="departDate" name="departDate" placeholder="Depart Date">
</div><br> <br>
<div class="form-group">
<input type="date" class="form-control input-sm" id="returnDate" name="returnDate" placeholder="Return Date">
</div><br> <br>
<input class="btn btn-default" type="submit" value="Submit"> <input class="btn btn-default" title="Clear page content"type="submit" name="clear" value="Clear">
</form>
<!-- Creates form for Riders -->
<div id="formData2" >
<?php
#FOR THE DRIVERS ONLY
if (isset($_POST['clear'])) {
$_SESSION['inputs2'] = array();
}
if (!empty($_POST['nameTwo']) && !empty($_POST['ageTwo']) && !empty($_POST['departLocTwo'])
&& !empty($_POST['arriveLocTwo']) && !empty($_POST['departDateTwo']) && !empty($_POST['returnDateTwo'])) {
if (!isset($_SESSION['inputs'])) {
// initialize the saved input session variable the first time
$_SESSION['inputs2'] = array();
}
$_SESSION['inputs2'][] = $_POST;
foreach ($_SESSION['inputs2'] as $input) {
echo " <span class='tripTwo'> <p class='riderAlert' > PASSENGER </p> <img src='rider.png' width ='50' height='50'title='rider'> {$input['nameTwo']}, Age: {$input['ageTwo']} <br> Would like to go to {$input['departLocTwo']} from {$input['arriveLocTwo']} on {$input['departDateTwo']} and return on
{$input['returnDateTwo']} <br> <br></span>
";
}
}
?>
</div>
<input class="btn btn-default" type="button" id="riderbtn"value="Passenger">
<div id="rider">
<form action="index.php" id="riderForm" method="post" class="form-inline">
<div class="form-group">
<input type="text" class="form-control input-sm" id="nameTwo" name="nameTwo" placeholder="Enter Name">
</div><br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="ageTwo" name="ageTwo" placeholder="Enter Age">
</div> <br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="departLocTwo" name="departLocTwo" placeholder="Departure Location">
</div><br> <br>
<div class="form-group">
<input type="text" class="form-control input-sm" id="arriveLocTwo" name="arriveLocTwo" placeholder="Destination Location">
</div><br> <br>
<div class="form-group">
<input type="date" class="form-control input-sm" id="departDateTwo" name="departDateTwo" placeholder="Depart Date">
</div><br> <br>
<div class="form-group">
<input type="date" class="form-control input-sm" id="returnDateTwo" name="returnDateTwo" placeholder="Return Date">
</div><br> <br>
<input class="btn btn-default" type="submit" value="Submit"> <input class="btn btn-default" title="Clear page content"type="submit" name="clear" value="Clear">
</form>
<script type="text/javascript">
$('#riderbtn').click(function()
{
$('#rider').toggle();
});
</script>
<!--- End of rider data -->
<script type="text/javascript">
$('#driverbtn').click(function()
{
$('#driver').toggle();
});
</script>
</div>
When the user submits the form, append the inputs to a session variable, and display all the saved values from the variable.
<?php
session_start();
if (isset($_POST['clear'])) {
$_SESSION['inputs'] = array();
}
if (!empty($_POST['name']) && !empty($_POST['age'])) {
if (!isset($_SESSION['inputs'])) {
// initialize the saved input session variable the first time
$_SESSION['inputs'] = array();
}
$_SESSION['inputs'][] = $_POST;
foreach ($_SESSION['inputs'] as $input) {
echo "You are {$input['name']} and you are {$input['age']} years old.<br>";
}
}
?>
<form action="index.php" method="post">
<input type="text" name="name" placeholder="Name">
<input type="text" name="age" placeholder="Age">
<input type="submit"> <input type="submit" name="clear" value="Clear">
</form>

Categories