Simple login form with php/javascript - javascript

I'm working on a simple login in form that verfies one username and one password using php and javascript. I have a index.php a javascript file to check for errors and a results.php. I can't see anything that I'm doing wrong, but the page continues to submit Invalid login in no matter what is input. Looking for some hints if you guys can give me some. Thanks in advance. I have also looked at similar questions on here and my code pretty much matches the others.
results.php
<?php
$user="joesmith";
$password="abc123";
if(isset($_POST['submit'])){
if(($_POST['user'] == $user) && ($_POST['password'] == $password))
echo "You have successfully logged in";
}
else
{
echo "Invalid login! Try again.";
}
?>
index.php
<body>
<form class="basic-grey" action="results.php" method="post">
<div>
<label for="username" id="lUsernameLabel">* Username:</label>
<input type="text" name="username" id="iUsername" onblur="validateUserName()" onfocus="resetUserName()" />
<div id="dUsernameError" class="errorMessage"></div>
</div>
<div>
<label for="password" id="lPasswordLabel">* Password:</label>
<input type="password" name="password" id="iPassword" onblur="validatePassword()" onfocus="resetPassword()" />
<div id="dPasswordError" class="errorMessage"></div>
</div>
<div>
<input type="submit" class="button" value="Submit" />
</div>

your input has name="username" but you are looking for $_POST['user']

You are submitting username input with "username" but trying to check its validity with "user".
Change the part
($_POST['user'] == $user)
to
($_POST['username'] == $user)
it will start to work

Try $_POST['username'] instead of $_POST['user'].
(This would be a comment if I had the rep)

Related

how to prin a message in screen and then redirect to other page php/js

i have a form and i connected with php to a database.It works perfect but i 'd like to show me o pop up message that is succesfull login .
<form id = "quiz" name="quiz" method="post" action="insert.php" >
name: <input id="name" type="text" name="name" placeholder="π.χ. George" required="">
<label for="name"> </label>
faculty: <input type="text" name="faculty" placeholder="Enter your Faculty" required="">
<input class="btn btn-one" type="submit" value="Submit" />
</form>
insert.php is the php file that connect the form fields with the database.
then try this in this file:
echo "<script>alert('message successfully sent');</script>";
header('Location: main.html');
$stmt->close();
$conn->close();
but i get an error.If i delete echo "alert('message successfully sent');"; works perfect but i would like to have a message that's successfull.
This will work
echo "<script>alert('message successfully sent');
window.location.href='main.html'
</script>";

How to get data user input data from my webpage

I have made a form in my site, which will allow me to get suggestions about Rubik's cube algorithms, but how to know what input the user has? For better understanding, I've given the code below:
<form method="POST">
<label>Your name: </label><br>
<input type="text" name="name" placeholder="Your Name" required><br><br>
<label>Your E-mail: </label><br>
<input type="email" name="email" placeholder="email#domain.com" required><br><br>
<label>Select puzzle: </label><br>
<input type="radio" name="2x2" value="2x2">2x2<br>
<input type="radio" name="3x3" value="3x3">3x3<br><br>
<label>Select set/subset: </label><br>
<input list="set"><br><br>
<datalist id="set">
<option>Ortega OLL</option>
<option>Ortega PBLL</option>
<option>CLL</option>
<option>EG-1</option>
<option>EG-2</option>
<option>F2L</option>
<option>OLL</option>
<option>PLL</option>
<option>COLL</option>
<option>WV</option>
</datalist>
<label>Your Alg: </label><br>
<input type="text" name="alg"><br><br>
<input type="submit" name="submit" class="w3-black w3-button w3-hover-white w3-hover-text-blue w3-text-white">
</form>
Please add action attribute to your form tag and on submit here is the example
<form action="getvalue.php" method="post">
</form>
Note: Every form element must have unique name .
after adding action attribute then create getvalue.php file and add following code in to it
<?php
print_r($_POST);
?>
Above code will give all the form field values
do let me know if it was helpfull...
I'm not sure exactly what you want to do, but here is an example of a form that submits to itself. This will allow you to remain on the same page after the form has been submitted. You can change what the user sees to indicate that the form was done successfully/etc. I have tested this code and it works.
<main>
<?php
// When the form is submitted
if (isset($_POST["submitButton"])){
//Do something with the data from the form - you don't have to print it out.
//This is all done on the server.
//Connect to DATABASE, send an EMAIL, VALIDATE the form, etc.
print("<pre>");
print_r($_POST); // for all GET variables
print("</pre>")
?>
<!-- This HTML will be visible to the user after the form has been submitted. -->
<h1>The form has been submitted successfully!</h1>
<?php
}else{ //If the form has not been submitted
?>
<form method = "post" >
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" id = "submitButton" name = "submitButton" value="Submit">
</form>
<?php
} //End else
?>
</main>

onClick() to perform function?

Is it possible for in a form of username and password to perform JS validation with the onClick()?
Here is the code:
<form action="signup.php" method="post" name="formInput" onclick="validateForm()">
Username: <br> <input type="text" name="username" placeholder="Username"><br>
Password <br> <input type="password" name="pass" placeholder="Password"><br>
<input type="submit" name="submit">
</form>
I remember doing it before but cannot remember where to call the actual onClick() in the form creation or in the button submission?
Thanks for any suggestions.
<script>
function validateForm() {
// if valid return true else false
}
</script>
<form action="signup.php" method="post" name="formInput" onsubmit="return validateForm()">
Username: <br> <input type="text" name="username" placeholder="Username"><br>
Password <br> <input type="password" name="pass" placeholder="Password"><br>
<input type="submit" name="submit">
</form>
As soon as the submit-button is clicked, signup.php gets executed and your validateForm() is ignored. I do not know why you are trying to validate the data with JavaScript instead of inside the signup.php itself. Furthermore it is almost certainly a huge security-flaw to do the validation on the front-end.
What I suggest you do is to forget about validating in JavaScript, and do it in PHP instead. If you post your validateForm() code we can help you translating it into PHP (and probably improve it as well).
Edit: You have stated that your validation is to make sure the input fields are not empty. So here we go:
<?php
session_start();
$username = $_POST['username'];
$pass = $_POST['pass'];
if (strlen($username) < 1 || strlen($pass) < 1) {
header(...); //redirect back to the home-page
$_SESSION['message'] = "Username and password must be filled out.";
}
... //rest of your signup.php
?>
and change the extension of your HTML-page to .php, then add this to the top:
<?php
session_start();
echo("<script> alert('" . $_SESSION['message'] . "'); </script>");
?>
This seems like overkill for such a simple task, and probably is. However you will want to check for other things than the emptiness of the fields, and that has to be done in this way.
However, if you do not care and want a simple way to do it, you can probably disable the submit-button with JavaScript somehow until both fields are filled. Check if both fields are filled with something like onChange = validateForm() function.
SOLUTION:
<form action="signup.php" method="post" name="formInput" onsubmit="return validateForm()">
Username: <br> <input type="text" name="username" placeholder="Username"><br>
Password <br> <input type="password" name="pass" placeholder="Password"><br>
<input type="submit" name="submit" onsubmit="validateForm()">
</form>

white space occur in textarea

I am trying to update query using php and database and display the textarea in a page. Here is my code,problem is i get the text area with some whitespace. Please help.
if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=$_POST["id"];
$title=$_POST["events"];
$description=$_POST["description"];
}
HTML
<form action="hotel1_galery_eventedit.php" method="post" class="col-sm-4" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $val->event_id;?>">
<div class="form-group has-info">
<label class="control-label" for="inputSuccess">Event title</label>
<input type="text" class="form-control" name="events" value="<?php echo $val->event_title;?>">
</div>
<div class="form-group has-info">
<label class="control-label" >Event Description</label>
<textarea name="description" class="form-control text-left" rows="3">
<?php echo $val->event_description;?>
</textarea>
</div>
</form>
Have you tried trim() function. use like this
<textarea name="description" class="form-control text-left" rows="3">
<?php echo trim($val->event_description);?>
</textarea>
hope this will help you.
use trim();
if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=$_POST["id"];
$title=trim($_POST["events"]);
$description= trim($_POST["description"]);
}
if (isset($_POST["events"])&&isset($_POST["description"]))
{
$id=mysql_real_escape_string(trim($_POST["id"]));
$title=mysql_real_escape_string(trim($_POST["events"]));
$description=mysql_real_escape_string(trim($_POST["description"]));
}
It's a secure method to protect our db from unexpected problems and you can also show data without any extra space...
Keep your textarea code in same line, the enter inside text area is causing this issue.
<textarea name="description" class="form-control text-left" rows="3"><?php echo $val->event_description;?></textarea>
The enter which you have added before echoing the value is the reason for this.
Use trim() with conditions, if value will be blank in text area then it will be treated as NULL or you can use ''
if(isset($_POST["events"])&&isset($_POST["description"])){
$id=$_POST["id"];
$title=$_POST["events"];
$description= ($_POST["description"] == '') ? NULL : trim($_POST["description"]);
}

Login Form Directing user to multiple pages depending on the password

Could somebody please show me how I can set up my form below to log into different pages sharing the same name as the password. For instance Username: David Password: Customer1
This will then direct the user to customer1.html.
and Username: Steve Password: customer8, directs the user to customer8.html. I remember seeing a tutorial on this years ago using Javascript but I can not find anything now on how to do this.
Thanks in advance.
<form name="password1">
<blockquote>
<blockquote>
<p align="center">Client Login</p>
<p><strong>Username: </strong>
<input type="text" name="username2" size="15">
<br>
<strong>Password: </strong>
<input type="password" name="password2" size="15">
</p>
<div align="center">
<input type="button" value="Submit" onclick="submitentry()" />
</div>
</blockquote>
</blockquote>
As mentioned in comments & as you have tagged PHP in your question, so I am providing you a server based solution.
index.html
<form name="password1" action="check.php" method="POST">
<blockquote>
<p align="center">Client Login</p>
<p><strong>Username: </strong>
<input type="text" name="username2" size="15">
<br>
<strong>Password: </strong>
<input type="password" name="password2" size="15">
</p>
<div align="center">
<input type="submit" value="Submit" />
</div>
</blockquote>
</form>
check.php
<?php
if(isset($_POST['password2'])){
$password = stripslashes($_POST['password2']);
$password = mysqli_real_escape_string($password);
// You may verify whether the username & password entered matches with database or not
$address = "$password".".html";
header("location: $address");
}
?>
In your check.php
SELECT your username & password for checking.
Use IF ELSE to make it work.
if($password = 'customer1')
{
header("location: customer1.html");
exit;
}
.
.
.
else if($password = 'customer8')
{
header("location: customer8.html");
exit;
}

Categories

Resources