issue with echo div in PHP - php

I am doing the login page If the user not successfully login I will use the the below to handle it, which means the message will be showed on the left of text box pwd. But the message never showed when the login failed. Can you tell me why
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Username:</label>
<input type="text" name="name" size="10"> </br>
<label>Password:</label>
<input type="text" name="pwd" size="10"> </br>
<?php
echo '<div style="position:absolute; left:650px; top:25px;float:left">';
if (isset($username))
{
// if they've tried and failed to log in
echo 'Your password is not right<br />';
}
echo '</div>';
?>
Register &nbsp
<input type=submit name=login value=Login>
</form>
If I dont put PHP code, the message will be showed blow the pwd textbox.But I want is to the left of text box. I am sure the problem is CSS style, because when I just define DIV only has one properties, which is float left the error will be showed blow pwd text box, login and register will be to the right of this error message, which is very strange

If you are not using AJAX, and you are not familiar with it.
Try this!
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label>Username:</label>
<input type="text" name="name" size="10"> </br>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if(!empty($_POST['name']) && !empty($_POST['pwd'])){
$name = trim($_POST['name']);
$password = trim($_POST['pwd']); # Normally this password is encrypted
# Database Connection Here
if // if they've tried and failed to log in
{
echo '<div style="position:absolute; left:650px; top:25px;float:right">Your password is not right<div/>';
}
}
}
?>
<label>Password:</label>
<input type="text" name="pwd" size="10"> </br>
Register &nbsp
<input type="submit" name="login" value="Login">
The better solution or practice would be to use AJAX. With AJAX you can display your error message anywhere you want. If you want to practice go to the website link below:-
http://www.developphp.com/view.php?tid=1296

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 session dropped after form submit

Seems, that I can't add password protection to the script: it should allow to login with the pass and to submit data from the form to mysql. Login looks fine, but if I try to press submit, it returns me to login page. Seems, that session is dropped or overwritten, but is not clear, how:
//login area
<?php
$password = "test";
session_start();
$_SESSION['txtPassword']= $_POST['txtPassword'] ;
if ( $_SESSION['txtPassword']!=$password ) {
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><label for="txtPassword">Password:</label>
<br /><input type="text" title="Enter your password" name="txtPassword" /></p>
<p><input type="submit" name="Submit" value="Login" /></p>
</form>
<?
}
elseif ( $_SESSION['txtPassword']=$password ) {
echo $_SESSION['txtPassword'] ; // tried to print password, result is correct: test
//my db connection, just in case:
include "config.php";
$connect = mysqli_connect(HOST, USER, PASSWORD, NAME);
// data which should be inserted to db
if
(#$_POST['posted']=='1' $_POST['posted'])) {
$sSQL = "UPDATE users SET user_login='".mysqli_real_escape_string($connect, $_POST['usern'])."',user_pass='".mysqli_real_escape_string($connect, dohashpw($_POST['passw']))."' WHERE ID=1";
mysqli_query($connect, $sSQL) or print(mysql_error());
print ' <div class="container"> <p class="pstype">Password updated! </p>';
...
//input form
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"><input type="hidden" name="posted" value="1" />
<div class="col-xs-3">
<label for="ex2">New Username: </label>
<input type="text" class="form-control input-lg" name="usern" >
</div>
<div class="col-xs-3">
<label for="ex2">New Password: </label>
<input type="password" class="form-control input-lg" name="passw" >
</div>
<div class="col-xs-3">
<input type="submit" value="Submit" onclick="<? mysqli_query ($connect, $sSQL);?>; ">
</div>
</form>
I am able to login this page, but when I fill the form and click Submit, I get login area again. If echo $_SESSION show a correct result, I think that it was established, but data are lost after for submit. Could you please help to find my error?
You are assigning and not comparing here :
elseif ( $_SESSION['txtPassword']=$password ) {
this is better
elseif ( $_SESSION['txtPassword']==$password ) {
but thats a bad idea anyway, passwords should not be stored in session variables like this, and you have to hash them once the user submit them and only manipulate and store the hashed passwords in your code and database
<?php
$password = "test";
session_start();
$_SESSION['txtPassword']= $_POST['txtPassword'] ;
if($_SESSION['txtPassword']!=$password ){
?>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ? >">
<p><label for="txtPassword">Password:</label></p>
</br>
<p><input type="text" title="Enter your password" name="txtPassword"/> </p>
<p><input type="submit" name="Submit" value="Login"/></p>
</form>
<?php
}
else{
echo $_SESSION['txtPassword'];
}
?>
i am not understanding why the elseif stands for? you are already checking inside the if condition which both are not equal?.

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";
}
?>

PHP,HTML Need to direct from one form to another form

I have written a code to get First name,last name and NIC no. If First name is missing I will show a error call "missing" infront of the textbox. All this thing is working and it will send the information in to the same page.
If first name is missing show the error in the same form which will allow user to refill it. If the user entered information correctly, I need to redirect user to another form. How can I do it?
<?php
$fnameerr="";
$name="";
if($_SERVER['REQUEST_METHOD']=="POST")
{
if(empty($_POST["fname"]))
{
$fnameerr="Missing";
}
}
?>
<head>
<title>Untitled Document</title>
</head>
<body>
<form name="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
First Name:<input type="text" name="fname" value="<?php echo htmlspecialchars($name);?>">
<span class="error" style="color:#FF0000">*<?php echo $fnameerr;?></span>
<br/><br/>
Last Name:<input type="text" name="lname" /><br/><br/>
NIC:<input type="text" name="nic" /><br/><br/>
<input type="submit" name="sub" value="Register"/>
<input type="reset" name="re" value="Reset"/>
</form>
</body>
</html>
Use header() to redirect
if(empty($_POST["fname"])) {
$fnameerr="Missing";
}
else {
header("Location: anotherForm.php");
}

Php if statement redirection

I have a simple php page that contain form with action that directed to a file called getData.php, here’s the PHP page:
<?php
if(isset($_GET['login_attempt'])) {
echo 'LOGIN ATTEMP 1';
}else{
echo 'NO LOGIN ATTEMP';
}
?>
<form id="info" action="getData.php" method="post">
<input type="text" class="input" name="info1" /><br>
<input type="password" class="input" name="info2" /><br>
<input type="submit" value="" id="submit-button">
</form>
And here's the getData.php file:
if (!isset($_GET['login_attempt'])) {
header('location: /login.php?login_attempt=1');
} else {
header('location: http://www.stackoverflow.com');
}
As you can see in the getData.php file after two attempts of logging in the php page should go to http://www.stackoverflow.com, but the php page get stuck in /login.php?login_attempt=1 forever after the first attempt, why?
Here is how you can handle this
<?php
$attempt = 1;
if(isset($_GET['login_attempt'])) {
$attempt ++;//Now attempt will have 2
echo 'LOGIN ATTEMP 1';
}else{
echo 'NO LOGIN ATTEMP';
}
?>
<form id="info" action="getData.php" method="post">
<input type="hidden" class="input" name="login_attempt" value='<?php echo $attempt;?>' /><br>
<input type="text" class="input" name="info1" /><br>
<input type="password" class="input" name="info2" /><br>
<input type="submit" value="" id="submit-button">
</form>
And here's the "getData.php" file:
<?php
if (isset($_POST['login_attempt']) AND $_POST['login_attempt'] !== 2) {
header('location: /login.php?login_attempt=1');
} else {
header('location:http://www.stackoverflow.com');
}
?>
EDIT
Why your page stuck on first attempt. Here is the answer.
When ever you redirect to login page the first condition is matched. As you have no criteria to tell getData.php which attempt is this there fore you are stuck. Use a variable $attempt as i have used in login.php initialize it with 1. If there is 2nd attempt increment it with 1. Put it in h hidden field so that getData.php its number. and will then redirect to according to your needs.
Change the line
<input type="submit" value="" id="submit-button">
to
<input type="submit" value="" id="submit-button" name="login_attempt">
and
from file getData.php
change the line
if (!isset($_GET['login_attempt']))
to
if (!isset($_POST['login_attempt']))
as you are using post method in the form ( <form id="info" action="getData.php" method="post">).
and change this line to
if(isset($_GET['login_attempt']))
to
if($_GET['login_attempt']==1)
i think this will solve your problem.

Categories