How to check sign using javascript and php , Like this? - javascript

How to check sign using javascript and php , Like this ?
First, user will fill data in to input username and password
If username and password correct, It's will be alert SUCCESS
But username or password incorrect, It's will be alert FAIL
I tested my code, But not work. How can i do that ?
HTML
<form method="post" action="" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);" id="sign_in_fid">
<label>
Your Username
</label>
<input type="text" name="username" id="username">
<br>
<label>
Your Password
</label>
<input type="password" name="password" id="password">
<br>
<br>
<input name="submit" type="submit" value="Sign in"/>
</form>
JAVASCRIPT
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
var username_val = document.getElementById("username").value;
var password_val = document.getElementById("password").value;
</script>
<?PHP
include("connect.php");
$strUsername = "<script>document.writeln(username_val);</script>";
$strPassword = "<script>document.writeln(password_val);</script>";
$sql = "SELECT * FROM av8_users WHERE BINARY username = '$strUsername' and password = '$strPassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);
if($count==1)
{
?>
<script>
alert("SUCCESS");
</script>
<?PHP
}
else
{
?>
<script>
alert("FAIL");
</script>
<?PHP
}
?>
<script>
return true ;
}
</script>

The reason your code is failing is you are using JavaScript inside php code.
Try changing your
$strUsername = "<script>document.writeln(username_val);</script>";
$strPassword = "<script>document.writeln(password_val);</script>";
To
$strUsername = $_POST['username'];
$strPassword = $_POST['password'];

Related

Can't redirect to another php page after I login, the login form reloades on the same page instead

Recently I've been facing this problem for a couple days. The login validation works perfectly. Unfortunately, I can't redirect to another page which is index.php after I try to login, and the login form is reloading on the same page at the bottom of the form instead, which is supposed to be for the error message just in case the user inputted the wrong username or password. Here are the screenshots of the form:
Screenshot of the login form at login.php
Screenshot of the login form when the user doesn't fill all the fields
Screenshot of the login form when the user inputted the wrong username or password
Screenshot of the login form after the user inputted the correct username and password, the form is reloading on the same page. Instead, I want it to redirect to another page, which is index.php
Here's my code:
login.php
<?php
require_once 'templates/header.php';
?>
<link rel="stylesheet" type="text/css" href="styles/login-style.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#login').click(function(){
var username = $('#usernameID').val();
var password = $('#passwordID').val();
$.post("classes/validation_login.php",{
user_val : username,
password_val : password,
},function(data){
if(data == 'success'){
window.location.href='index.php';
}else{
$('.error-message').html(data);
}
});
});
});
</script>
<title>Login</title>
<form id="login-form">
<h1>Login</h1>
<input type="text" id="usernameID" name="username" placeholder="Username" autocomplete="off"> <br>
<input type="password" id="passwordID" name="password" placeholder="Password" autocomplete="off"> <br>
<input type="button" id="login" name="register-button" value="Login">
</form>
<div class="error-message">
</div>
<?php
require_once 'templates/footer.php';
?>
validation_login.php
<?php
require_once '../core/init.php';
class validation_login{
private $username,$password;
public $errorMessage;
public function validate_login(){
$db = new database();
$this->username = input::get('user_val');
$this->password = input::get('password_val');
if(empty($this->username) || empty($this->password)){
$this->errorMessage = "Please fill all the fields!";
return false;
}else if(!$db->login()){
$this->errorMessage = "Invalid username or password!";
return false;
}else{
session::set('username',$this->username);
return true;
}
}
}
$login = new validation_login;
$login->validate_login();
echo "$login->errorMessage";
?>
Any help would be appreciated. Thank you!
You redirect within the ajax call. Thats the misstake.
Remove the AJAX call and add an action tag to the form. (to validation_login.php)
<form action="validate_login.php" method="post" id="login-form">
It is because you cannot redirect on the server side when using ajax, try returning a value and redirect base in the value, you can rewrite your code as below
validation_login.php
<?php
require_once '../core/init.php';
class validation_login{
private $username,$password;
public $errorMessage;
public function validate_login(){
$db = new database();
$this->username = input::get('user_val');
$this->password = input::get('password_val');
if(empty($this->username) || empty($this->password)){
$this->errorMessage = "Please fill all the fields!";
}else if(!$db->login()){
return false;
}else{
session::set('username',$this->username);
return true;
}
}
}
?>
login.php
<?php
require_once 'templates/header.php';
?>
<link rel="stylesheet" type="text/css" href="styles/login-style.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#login').click(function(){
var username = $('#usernameID').val();
var password = $('#passwordID').val();
$.post("classes/validation_login.php",{
user_val : username,
password_val : password,
},function(data){
if(data){
//means validation went through
//redirect user
window.location.href='index.php';
}else{
//validation returned false
//display error message
$('.error-message').html('incorrect Username or password');
}
});
});
});
</script>
<title>Login</title>
<form id="login-form">
<h1>Login</h1>
<input type="text" id="usernameID" name="username" placeholder="Username" autocomplete="off"> <br>
<input type="password" id="passwordID" name="password" placeholder="Password" autocomplete="off"> <br>
<input type="button" id="login" name="register-button" value="Login">
</form>
<div class="error-message">
</div>
<?php
require_once 'templates/footer.php';
?>

Angularjs validate

I want validate my form using ajax , but the errors are not showing properly.
Here is my code:
Html
<body ng-app="app" ng-controller="dkController">
<form ng-submit="dk()">
<label>Username</label>
<input type="text" name="username" ng-model="formdata.user">
{{errorName}}
<label>Password</label>
<input type="text" name="password" ng-model="formdata.pass">
<label>Email</label>
<input type="text" name="email" ng-model="formdata.email">
<button type="submit" >Submit</button>
{{message}}
</form>
</body>
Js
function dkController($http,$scope){
$scope.formdata={};
$scope.dk = function(){
$http({
method:"POST",
url:"dk.php",
data:$.param($scope.formdata)
})
.then(function(response){
$scope.errorName = response.data.errors.username;
})
and PHP
$conn= new mysqli('localhost','root','','dangky');
$username = isset($_POST['username']) ? trim($_POST['username']) : '';
$sql="SELECT *FROM dangky where username='$username'";
$result= mysqli_query($conn,$sql);
$errors=array();
$data=array();
if(empty($username))
$errors['username']='user name is required';
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_assoc($result);
if ($row['username'] == $username){
$errors['check'] = 'Tên đăng nhập đã tồn tại';
}
}
if(!empty($errors)){
$data['success']=false;
$data['errors']=$errors;
}
echo json_encode($data);
Error username is showing but errors check not showing .
This is the php output
{"success":false,"errors":{"username":"user name is required"}} .
The problem is youu passed $scope.formdata which contains
{ user : 'someone', pass: 'abc123!', email: 'me#medotcom' }
Your PHP script is looking for post data { username: 'someone' }
So rename your ng-model to ng-model="formdata.username" or change your PHP to find $_POST['user']. Your choice but they must match.

having trouble in showing php success message with ajax

Here is my below code I'm just trying to send a message form with php script but the message is submitted well. but when it comes to showing success message I'm having trouble page submits the data but shows no code.
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#send").click(function(event){
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "process.php",
data:{
name: name,
email: email,
message: message,
},
success:function(data) {
$('#msg').html(data);
}
});
});
});
</script>
Below is php and html code:
<?php
include 'db.php';
$name=$_POST["name"];
$email=$_POST["email"];
$message=$_POST["message"];
$query = mysqli_query($con, "INSERT INTO test(name, email, message) VALUES ('$name', '$email', '$message')");
if($query){
echo "Your message has been sent successfully!";
}
else{
echo "Your message has been not sent successfully!";
}
mysqli_close($con);
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="text" class="form-control" id="name" name="name">
<input type="text" class="form-control" id="email" name="email">
<textarea class="form-control" rows="5" id="message" name="message"></textarea>
<button id="send" type="submit" class="btn btn-primary">Send</button>
</form>
<div id="msg" class="alert alert-success hidden"><strong></strong></div>
It is because your form will be submitted as the button has the type submit. By changing the type to button the ajax should be working.
Change type="submit" to type="button"
I hope this will help!

$_POST not working in PHP. have tried $_REQUEST also

This is my HTML
<form class="login-form" method="POST">
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="login" name="login" value="login" onclick="redirect()"></input>
</form>
And my JS
<script type="text/javascript">
$(document).ready(function redirect(){
$("#login").click(function(){
var pwd="<?php echo $pwd?>";
alert("<?php echo "this is pass".$pword?>");
if(userID===user && pwd===pass)
{
window.location.href = 'http://localhost/Annapoorna/Welcome_Page.php';
}
else
{
alert("Please enter correct username and password!");
}
}
});
});
</script>
And PHP
<?php
$username="";
$pwd="";
$pword="";
function confirm(){
if(isset($_REQUEST['password']))
{
$pword = $_REQUEST['password'];
}
$sth->execute(array(':pass'=>$pword));
foreach ($sth->fetchAll() as $row) {
$username=$row['username'];
$pwd=$row['password'];
echo $pword;
}
?>
$_POST is not returning any value. Have tried $_REQUEST, but didn't work. Have also assigned both ID and name to the password field.
You are not submitting your form, just redirecting user to that page. Try this
<form id='myForm' class="login-form" method="POST" action='/Annapoorna/Welcome_Page.php'>
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="login" name="login" value="login" onclick="redirect()"></input>
</form>
in your javascript
<script type="text/javascript">
$(document).ready(function redirect(){
$("#login").click(function(){
var pwd="<?php echo $pwd?>";
alert("<?php echo "this is pass".$pword?>");
if(userID===user && pwd===pass)
{
$('#myForm').submit();
}
else
{
alert("Please enter correct username and password!");
}
}
});
});

Using the $.post with JQuery for two forms on the same page?

Hello I am wondering if this is possible? Having two different forms on the same page using the jquery post to send it php to do some checking. The first from works flawlessly, however when I go to the second form I get an error saying it is an undefined variable but I am using the exact same method I used for the first form. It will load anything echoed in the php page for the feed form but will not echo back what I am typing in. Is there a better, more correct way to do it?
This is not for a real site, just testing for a project I am working on.
HTML:
<form action="php/signup.php" method="post" class="form-inline" name="signupForm">
<input type="text" maxlength="20" name="username" id="user_in">
<input type="password" maxtlength="20" name="password" id="pass_in">
<input type="submit" name="submit" Value="Sign Up">
</form>
<div id="feedback"></div> <!-- Feedback for Sign Up Form -->
<br /><br />
<form name="feedForm">
<input type="text" id="feed_in" name="feed_me_in" placeholder="feed">
<div id="feedme"></div> <!-- FEEDback for feed form -->
</form>
<script src="js/jquery-1.9.1.js"></script>
JavaScript:
<script>
$(document).ready(function() {
$('#feedback').load('php/signup.php').show();
//SIGN IN FORM
$('#user_in, #pass_in').keyup(function() {
$.post('php/signup.php', { username: document.signupForm.username.value,
password: document.signupForm.password.value },
function(result) {
$('#feedback').html(result).show
});
});
$('#feedme').load('php/feed.php').show();
//FEED FORM
$('#feed_in').keyup(function() {
$.post('php/feed.php', { feed: document.feedForm.feed_me_in.value },
function(result) {
$('$feedme').html(result).show
});
});
});
</script>
PHP for Feed Form:
<?php
$feed = mysql_real_escape_string($_POST['feed']);
if(isset($feed)) {
echo $feed;
} else {}
?>
PHP for the Sign Up Form:
<?php
if(isset($_POST['username'])) {
include_once('connect.php'); //Connect
$username = mysql_real_escape_string($_POST['username']);
$sql1 = "SELECT username FROM users WHERE username='$username'";
$check = mysql_query($sql1);
$numrows = mysql_num_rows($check);
if(strlen($username)<=4) {
echo "Username is too short";
} elseif($numrows == 0) {
echo "Username is available";
} elseif($numrows > 0) {
echo "Username is already taken";
}
} else {
echo "Please type a username";
}
?>
$('$feedme').html(result).show
should be
$('#feedme').html(result).show();

Categories