Submit button not working on login form, using PHP and JavaScript -
i trying code simple login page using javascript , php validate user input values values in database.
however, submit button doesn't seem anything... clears fields.
here code. there many errors in , apologize that... php code wrote collection of other login pages found on web , started learning javascript couple days ago...
edit: have updated code based on mistakes guys pointed out. log in button no longer refreshes instead doesn't anything. try figure out or start scratch since there code don't understand, haha.
login.html:
`<html> <head> <link href='https://fonts.googleapis.com/css?family=montserrat' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="bootstrap.min.css"> <link type="text/css" rel="stylesheet" href="stylesheet.css" /> <title>login</title> </head> <body> <div class="header"> <div class="container"> <h1>login</h1> </div> </div> <div class="form"> <form class="form-horizontal"> <div class="form-group"> <div class="col-sm-10"> <input type="text" class="form-control" id="inputusername3" name="username" placeholder="username"> </div> </div> <div class="form-group"> <div class="col-sm-10"> <input type="password" class="form-control" id="inputpassword3" name="password" placeholder="password"> </div> </div> <div class="form-group"> <div class=" col-sm-10"> <div class="checkbox"> <label> <input type="checkbox">remember me </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-10"> <input id="submit" type="submit" class="btn btn-default" value="log in"> </div> </div> </form> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type="text/javascript" src="js/vendor/jquery.md5.js"></script> <script src="login.js"></script> </div> </body> </html>'
login.js
$(document).ready(function(){ $("form").on("submit", function(e){ e.preventdefault();{ var username = $('#inputusername3').val(); var password = $('#inputpassword3').val(); var newpw = $.md5(password); if (username != "" && password !="") { $.ajax ({ url: "dologin.php", type: "post", async: false, data: "$username="+username+"&password="+newpw, success: function(result) { window.open(success.html); } }); } else { alert("please enter username , password."); } return false; }); });
dologin.php:
<?php $host="localhost"; &username=""; $password=""; $db_name="atasgtsar"; $tbl_name="members"; $connection = mysqli_connect("$host", "$username", "$password")or die("unable connect."); mysqli_select_db("$db_name"); $myusername=$_post['username']; $mypassword=$_post['password']; $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysqli_real_escape_string($myusername); $mypassword = mysqli_real_escape_string($mypassword); $sql="select * $tbl_name username='$myusername' , password='$mypassword'"; $result=mysqli_query($sql); $count=mysqli_num_rows($result); if ($count == 1) { session_register("myusername"); session_register("mypassword"); } else { echo "wrong username or password."; } mysqli_close($connection); ?>
your fields cleared submit button reloads page. use preventdefault(); stop submit happening want submit data using ajax.
$("form").on("submit", function(e){ e.preventdefault(); //...
please not store passwords plain text in databases, use password_hash() , password_verify(). mysql version use deprecated, please use mysqli or pdo. there no type='username', use 'text'. call "check(this.form)" submit button, bind jquery handler submit event using js. if want select elements there id in jquery, instead of input[id=username], use #username
also, there sure more mistakes in these codes. recommend start extremely basic layout, print out information (in js using console.log or alert , in php using echo) , go n small steps, until got working code.
Comments
Post a Comment