Error in action file PHP - php

I have one file, that file is a form for administrator change Username and Password. The file named "change_password.php". Here's the code :
<?php
session_start();
include_once('../config.php');
?>
<head>
<meta charset="UTF-8" />
<!-- <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> -->
<title>***********</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Login and Registration Form with HTML5 and CSS3" />
<meta name="keywords" content="html5, css3, form, switch, animation, :target, pseudo-class" />
<meta name="author" content="Codrops" />
<link rel="shortcut icon" href="../favicon.ico">
<link rel="stylesheet" type="text/css" href="../css/demo.css" />
<link rel="stylesheet" type="text/css" href="../css/style2.css" />
<link rel="stylesheet" type="text/css" href="../css/animate-custom.css" />
</head>
<body>
<div class="container">
<!-- Codrops top bar -->
<div class="codrops-top">
<div class="clr"></div>
</div><!--/ Codrops top bar -->
<header>
</header>
<section>
<div id="container_demo" >
<!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4 -->
<a class="hiddenanchor" id="toregister"></a>
<a class="hiddenanchor" id="tologin"></a>
<div id="wrapper">
<div id="signup" class="animate form">
<form action="change.php" autocomplete="on" method="post" enctype="multipart/form-data">
<h1>Admin's Profile Account</h1>
<?php
$admin_id = $_GET['id_admin'];
$query = mysql_query("select * from administrator where ID_Admin='$admin_id'");
$data = mysql_fetch_array($query);?>
<p>
<label for="username">Username : </label>
<input id="username" name="username" class="wide" type="text" required="required" value="<?php echo $data['username']; ?>"/>
<input id="username2" name="username2" class="wide" type="hidden" required="required" value="<?php echo $data['username']; ?>"/>
</p>
<p>
<label for="password">New Password :</label><input id="password" name="password" class="wide" type="password" required="required" placeholder="Your password" />
</p>
<p>
<label for="confirm_password">Confirm Password : </label> <input id="confirm_password" name="confirm_password" class="wide" type="password" required="required" placeholder="Confirm your password" />
</p>
<p> </p>
<center><p class="signup button">
<input type="submit" value="Submit" />
</p></center>
</form>
<center>Panel Administrator</center>
</div>
</div>
</section>
</body>
</html>
And I have one action file for changing username and password for Administrator. The file named "change.php". When I try to change username and password, this file only just blank. It didn't redirecting to another page. So where's my mistake? I tried to change the part of if else and another but the result is still the same. Here's my code :
<?php
session_start();
include_once('../config.php');
$ID_Admin = $_POST['admin_id'];
$username = $_POST['username'];
$password = $_POST['password'];
$encrypt = md5($password);
$confirm_pass = $_POST['confirm_password'];
if ($confirm_pass != $password) {
echo" <script language='JavaScript'> alert ('Password doesn't match');</script>";
echo '<script language="JavaScript"> window.location.href ="change_password.php" </script>';
} else {
if($_POST['username'] != $_POST['username2']){
$dup = mysql_query("SELECT username FROM administrator WHERE username='".$_POST['username']."'");
if(mysql_num_rows($dup) >0){
echo"<script language='JavaScript'> alert ('Username already use');</script>";
echo '<script language="JavaScript"> window.location.href ="change_password.php" </script>';
}
} else {
$query = mysql_query("update administrator set username='$username', password='$ecnrypt' where ID_Admin='$ID_Admin'") or die(mysql_error());
print '<script type="text/javascript">';
print 'alert("Profile has changed")';
print '</script>';
print '<script type="text/javascript">';
print "window.location.href='panelAdmin.php'";
print '</script>';}
}
?>
Thank you in advance for your help. I think either the part of "if else" or "script alert" that make this file doesn't work.

you need to escape your single quote so this:
echo" <script language='JavaScript'> alert ('Password doesn't match');</script>";
should be this:
echo" <script language='JavaScript'> alert ('Password doesn\'t match');</script>";
i'm guessing that is why its failing, also make sure your display_error() is enabled, to make sure you're not getting any PHP errors.

Try to use this where you have the redirects, just window.location will do the work for you:
echo '<script language="JavaScript">window.location = "change_password.php"; </script>';
Also, note the semicolon after .php"

There is no formelement in your form with the name admin_id. E.g.
<input type="hidden" name="admin_id" value=5" />
So the array $_POST has no index "admin_id", and thus the error message with the undefined index.

Since your PHP code cannot find value passed via POST, you need to include an input named with admin_id inside your form.
Example:
<input type="hidden" name="admin_id" value="<?php echo $admin_id; ?>" />
That should fix your error about:
Notice: Undefined index: admin_id

Related

MadLibs overwrite inputfields

im trying to make a madlibs kind of game, right now i got everything working besides 1 thing. the output should overwrite the questions instead of showing above the questions. how can i manage to do this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Mad Libs</title>
<link rel="stylesheet" type="text/css" href="MadLibs.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville:wght#700&display=swap" rel="stylesheet">
</head>
<body>
<form action="" method="POST">
<h1> MadLibs</h1>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Input1 = $_POST["Input1"];
$Input2 = $_POST["Input2"];
$Input3 = $_POST["Input3"];
$Input4 = $_POST["Input4"];
echo "here is a test $Input1. here is an other test $Input2 and again an other test
$Input4 test $Input3.";
}else{
<p id="Text">test question <input type="text" required name="Input1" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input2" placeholder="Enter your answer.."></p>
<p id="Text">test question<input type="text" required name="Input3" placeholder="Enter your answer.."></p>
<p id="Text">test question <input type="text" required name="Input4" placeholder="Enter your answer.."></p>
<input id="Submit" type="submit" name="submit" value="Send">
</form>
?>
<footer>
<p> A #2021 website</p>
</footer>
</body>
</html>
The end result shows like this but the questions and the input field should be gone when the submit button is pressed

PHP session clearing out on page refresh - show and hide based on input

So I have two pages, both pages have an input field, and if the user fills out the input field and goes to the next page they will see new content in the form's place. So it's basically like "fill out this form and show new content". Whether you fill out the form on page A or page B, the other page will recognize if you have filled it out or not. Make sense?
I have it working as long as you don't refresh the page or get to the other page other than clicking the submit input.
What am I doing wrong here?
<!--- Page A -->
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport">
<meta content="noindex, nofollow" name="robots">
<title>Page A</title>
<meta content="Page" name="description">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<form action="/b.php" id="l-form" method="post" name="l-form">
<div class="wrapper localize" style="<?php if(isset($_POST['treasure'])) { echo "display:none;"; } else { echo "display:block;"; } ?>">
<div class="header_container">
<h2>Make sure we offer service at your location</h2>
</div>
<div class="input_container">
<input autocomplete="off" class="input-box street" id="street" name="userLogin" placeholder="Street address" tabindex="0" type="text">
<input autocomplete="off" class="input-box suite" id="suite" name="suite" placeholder="Suite (Optional)" tabindex="0" type="text">
<input autocomplete="off" class="input-box zipcode" id="zipcode" name="zipcode" placeholder="Zip code" tabindex="0" type="text">
<input type="submit" name="treasure" value="See offers">
</div>
</div>
<!-- results 3 -->
<div class="results" style="<?php if(isset($_POST['treasure'])) { echo "display:block;"; } else { echo "display:none;"; } ?>">
<p class="results-heading">Excellent news. We know who you are.</p
</div>
</form>
</body>
</html>
<!--- Page B -->
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui" name="viewport">
<meta content="noindex, nofollow" name="robots">
<title>Page A</title>
<meta content="Page" name="description">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<form action="/a.php" id="l-form" method="post" name="l-form">
<div class="wrapper localize" style="<?php if(isset($_POST['treasure'])) { echo "display:none;"; } else { echo "display:block;"; } ?>">
<div class="header_container">
<h2>Make sure we offer service at your location</h2>
</div>
<div class="input_container">
<input autocomplete="off" class="input-box street" id="street" name="userLogin" placeholder="Street address" tabindex="0" type="text">
<input autocomplete="off" class="input-box suite" id="suite" name="suite" placeholder="Suite (Optional)" tabindex="0" type="text">
<input autocomplete="off" class="input-box zipcode" id="zipcode" name="zipcode" placeholder="Zip code" tabindex="0" type="text">
<input type="submit" name="treasure" value="See offers">
</div>
</div>
<!-- results 3 -->
<div class="results" style="<?php if(isset($_POST['treasure'])) { echo "display:block;"; } else { echo "display:none;"; } ?>">
<p class="results-heading">Excellent news. We know who you are.</p
</div>
</form>
</body>
</html>
You're not using session variables anywhere. When someone submits your form, you need to do something like:
$_SESSION['treasure'] = $_POST['treasure'];
to set the 'treasure' session variable. On the other page or after a refresh, you can then access $_SESSION['treasure'].
Currently, this seems to work when you click submt, because you are making $_POST['treasure'] available to the target page, not because you are using sessions as intended.

PHP produces a blank webpage

When ever I use this piece of PHP, it makes the webpage blank (white) and does not show anything. Here is my code:
<?php
if(isset($_POST['submit'])){
//
$user = $_POST['username'];
$pwrd = $_POST['pwrd'];
//include database connection
include('../includes/db_connect.php');
if(empty($user) || empty($pwrd)){
echo 'Missing information';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="container">
<form action="Login.php" mthod="post">
<p>
<label>Username</label><input type="text" name="username" />
</p>
<p>
<label>Password</label><input type="password" name="pwrd" />
</p>
<input type="submit" name="submit" value="logIn" />
</form>
</div>
</body>
</html>
After testing different pieces of the PHP code, I have noticed only this code make the page blank
if(empty($user) || empty($pwrd)){
echo 'Missing information';
}
?>
Is this possibly something to do with Apache, or is there something wrong with my PHP?
There are following problem:-
your first if bracket is not closed. So closed it.
your form method is not correct. Spelling mistake:-
So the correct code is here :-
<?php
if(isset($_POST['submit'])){
//
$user = $_POST['username'];
$pwrd = $_POST['pwrd'];
//include database connection
include('../includes/db_connect.php');
if(empty($user) || empty($pwrd)){
echo 'Missing information';
}
}// missing
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="container">
<form action="Login.php" method="post"> // method is written mthod
<p>
<label>Username</label><input type="text" name="username" />
</p>
<p>
<label>Password</label><input type="password" name="pwrd" />
</p>
<input type="submit" name="submit" value="logIn" />
</form>
</div>
</body>
</html>
Output:- before submit :- http://prntscr.com/74xhb7
After submit :- http://prntscr.com/74xhmm
Note:- the bracket that i said you to close you can close it based on your convinence. Thanks.
Also don't panic with error you seen in second screenshot. It's because i am not having included files at my end.

Beginner: IF statement/PHP/HTML issue

I do apologise in advance for my lack of knowledge in PHP and HTML. I have scoured the internet for 3/4 days trying to create what is probably a simple system. I need the user to enter a digit before the next page is loaded. I have an IF statement on my page.
1.php
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link href="css/reset.css" rel="stylesheet" type="text/css" />
<div id="container">
<div id="authorise">
<form action="2.php" method="post">
<!--Product Comment Box--><br>
<p>Please enter your 4<br> digit authorisation code:<br> <br>
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
</div>
</div>
2.php
<?php
if ($_POST['digits'] === '210392')
{echo 'https://www.facebook.com/'
?>
But I need a form first which the user would input the code '210392' and press submit. Then the if statement can take place. I know how to do a form but dont know how to assign that form a variable name 'digits'. All help is appreciated.
A basic form:
<form action="your_file.php" method="post">
<input type="text" name="digits" />
<input type="submit" name="submit" />
</form>
Then in your_file.php:
<?php
if ($_POST['digits'] === '210392') {
// etc.
$_POST is a superglobal array that has all data POSTed to your script. digits in this case is an index in that array. It comes from the name of the input field.
<form action="check.php" method="POST">
<input type="text" name="digits" />
<input type="submit" value="click here" />
</form>
//in check.php
if (isset($_POST['digits']) and $_POST['digits'] == 12345){
header("Location: http://google.com");
}
else {
echo " error !!";
}
study html from w3school
http://www.w3schools.com/html/html_forms.asp
and php
http://www.w3schools.com/PhP/php_forms.asp
<form action="https://scm-intranet.tees.ac.uk/users/l1071039/bobbin/admin.php" method="post">
Password : <input type="password" name="pwd" />
<input type="submit" name="send">
</form>
// admin.php
<?php
if( isset($_POST['pwd']) ){
$pwd = $_POST['pwd'];
}
?>
You first need to create an input with the very important name attribute which you'll use later.
<form method="post">
<input type="password" name="mypassword" />
<button type="submit">Go</button>
</form>
PHP:
//Post method access with $_POST variable:
if ($_POST['mypassword'] == 'my-password') { //Oh, you should probably use SSH and hash the password
//Hooray!
}
Instead of echo, use header.
<?php
if ($_POST['digits'] === '210392')
{
header (loaction:https://www.facebook.com/);
}
?>

PHP code not Re-directing

what is the problem in this code...??? it is not redirecting to the other page but stay on the login.php page (Blank Page)... Database are all correct...
can someone help me on this code...
your help is much appreciated...
<?
require_once('include/config.php');
require_once('include/functions.php');
session_start();
session_destroy();
$message="";
$Login=$_POST['Login'];
if($Login){ // If clicked on Login button.
$username=$_POST['username'];
$md5_password=md5($_POST['password']); // Encrypt password with md5() function.
mysql_select_db("$db", $con);
$result=mysql_query("select * from staff where stf_username='$username' and stf_password='$md5_password'");
$sql =mysql_query("select stf_level from staff where stf_username='$username'");
if(mysql_num_rows($result)!='0'){ // If match.
session_register("username");
while($row = mysql_fetch_array($sql))
{
//echo $row['level'] . " ";
if($row['stf_level'] == 1){
header("location:admin/index.php");
}
elseif($row['stf_level'] == 2){
header("location:cashier/index.php");
}
elseif($row['stf_level'] == 3){
header("location:waiter/index.php");
}
elseif($row['stf_level'] == 4){
header("location:kitchen/index.php");
}
}
//header("location:main.php"); // Re-direct to main.php
exit;
}else{ // If not match.
$message="--- Incorrect Username or Password ---";
}
} // End Login authorize check.
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="MSSmartTagsPreventParsing" content="true" />
<meta name="description" content="Description" />
<meta name="keywords" content="Keywords" />
<meta name="author" content="Aafrin" />
<title>iCafe Login</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="css/keyboard.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="js/keyboard.js"></script>
</head>
<body>
<div id="wrapper">
<div id="loginPanel">
<img class="image" alt="" src="images/logo.png" width="220" height="120" />
<div id="error"><? echo $message; ?></div>
<form id="form" name="form" method="post" action="<? echo $PHP_SELF; ?>">
<label>Username :</label>
<input type="text" name="username" class="keyboardInput" />
<label>Password :</label>
<input type="password" name="password" class="keyboardInput" />
<input name="Login" type="submit" id="Login" value="Login" class="submit" />
<!--
<input type="submit" value="Submit" name="submit" class="submit" />
-->
</form>
<div id="footer">
XYZ © 2012 - All Rights Reserver | Powered By Kesavan
</div>
</div>
<!--
<div id="details">
<p>Login Details</p>
Admin : admin
<br />
Waiter : waiter
<br />
Kitchen : kitchen
<br />
Cashier : cash
</div>
-->
</body>
</html>
try #header("") may be you have an error. Why dont you save all the location in a variable and pass the variable to the header function?
if($x=1){$loc="admin/index.php"}
if($x=2){$loc="staff/index.php"}
if($x=3){$loc="manager/index.php"}
#header('location:$loc')
i think you should put space after the :
it will be like
header("location: cashier/index.php");

Categories