i need to put html login and php in same page and save it as loginphp can i do that

<?php require_once("nocache.php"); ?>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>login.php</title>
<?php
if(isset($_POST['Submit']))
	if (empty($id) || empty($pword)){
	    header("location: login.php");
		}else{
			$conn = mysql_connect("localhost", "TWAassign2", "student");
			if(!$conn){
				die('Conection could not be opened: ' . mysql_error());
			}
			$db_selected = mysql_select_db('acadcredit', $conn);
			$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
			$rs = mysql_query($sql, $conn);
			if (mysql_num_rows($rs)> 0 ) {
		        session_start();
			    $_SESSION["who"] = $id;
				$_SESSION["level"] = mysql_result($rs,0,"level");
				header("location: selectView.php");
		    }else{
				header("location: login.php");
			}
		}
?>

</head>
<body>
<form method="post" action="login.php">
<h1> Welcome to Login Page </h1>
<h2> Please enter Your Staff ID and password- Thanks</h2>
	<p>Staff ID  <input type="text" name="ID"  /><br/>
    Password <input type="password" name="pass" /></p> 
<p> 
<?php
	if(isset($_POST['ID'])){
		$id = $_POST["ID"];
		$pword = $_POST["pass"];
	}
?>

	<input type="submit" value="login" />
    <input type="reset"  value="Reset" /></p>
</form>
</body>
</html>
Member Avatar for diafol

Seems a bit odd to me that you redirect to a form handler from the $_POST conditional. I'd have thought it better to send the form to the form handler directly / do the processing / return to the original page if unsuccessful / go to another page if successful.

This way you keep the form processing logic neatly tucked away in your form handling file as opposed to cluttering up your nice clean form page. Also, refreshing the page will not resend the form.

sorry but im new to php but what you mean by I'd have thought it better to send the form to the form handler directly / do the processing / return to the original page if unsuccessful / go to another page if successful.

it is possible to handle html and php in same page.move you form action on same page if you are using your php code at same page or move your action to other page if you want to use the php code on new page and then apply header to redirect the page on previous page.

im putting the action but its not working i dont know why

Member Avatar for diafol

if you're sending the form back to the same page do this:

action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"

hi there this is the code i have but it doesnt do anything i dont know why its just open to login then if i press submit then its telling me the page not found even when the text box its empty or even if i put right user name and password doesnt go to next page
please help im new to php

<?php session_start();
?>
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>login.php</title>
</head>
<body >
    <br><br>
 
 
 <?php
 
 if(isset($_POST['submit'])){
 
    $id= $_POST['ID'];
    $pword= $_POST['pass'];
    //echo "password".$pword;
 //echo "id".$id;
}
 if (empty($id) || empty($pword)){
header("location: login.php");
}else{
$conn = mysql_connect("localhost", "TWAassign2", "student");
if(!$conn){
die('Conection could not be opened: ' . mysql_error());
}
$db_selected = mysql_select_db('acadcredit', $conn);
$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
$rs = mysql_query($sql, $conn);
if (mysql_num_rows($rs)> 0 ) {
 
$_SESSION["who"] = $id;
$_SESSION["level"] = mysql_result($rs,0,"level");
header("location: selectview.php");
}else{
header("location: login.php");
}
}
 ?>

 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1> Welcome to Login Page </h1>
<h2> Please enter Your Staff ID and password- Thanks</h2>
<p>Staff ID <input type="text" name="ID" /><br/>
Password <input type="password" name="pass" /></p> 
 
<input type="submit" value="login" />
<input type="reset" value="Reset" /></p>
</form>
 
 
 
</body>
</html>

Use the code below and save it as "login.php"

<?php
	
session_start();
 
if(isset($_POST['pass'])){
	
    $id= $_POST['ID'];
    $pword= $_POST['pass'];
	
	if (empty($id) || empty($pword)){
		 header("location: login.php");
	} 
	else { 
		$conn = mysql_connect("localhost", "TWAassign2", "student");
		
		if(!$conn){
			die('Conection could not be opened: ' . mysql_error());
		}
		
		$db_selected = mysql_select_db('acadcredit', $conn);
		$sql = "SELECT staffID, accessLevel FROM staff WHERE id = '$id' AND password = '$pword'";
		$rs = mysql_query($sql, $conn);
		
		if (mysql_num_rows($rs)> 0 ) {
 			
			$_SESSION["who"] = $id;
			$_SESSION["level"] = mysql_result($rs,0,"level");
			
			header("location: selectview.php");
		}
		else {
			header("location: login.php");
		}
	}
}

?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>login.php</title>
</head>
<body >
    <br><br>
    
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1> Welcome to Login Page </h1>
<h2> Please enter Your Staff ID and password- Thanks</h2>
<p>Staff ID <input type="text" name="ID" /><br/>
Password <input type="password" name="pass" /></p> 
 
<input type="submit" value="login" />
<input type="reset" value="Reset" /></p>
</form>
</body>
</html>

If the page is not found that means you don't have the pages that are redirected to.

how can we make it so that an error is displayed for the wrong username and differently for the wrong password?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.