Reputation: 172
I am attempting to have the user enter their un/pw, click a button, have a javascript function take the un/pw entered, send it to a php script which will return a 1 or 0 based on whether the un/pw was valid.
In the javascript page I have:
function handleLogin() {
var username = document.getElementById('un').value;
var password = document.getElementById('pw').value;
var valid = $.post("getLogin.php", {"un": username, "pw": password}, "json");
alert(valid);
}
In the php file I have:
$username = $_POST['un'];
$password = $_['pw'];
$valid = 0;
# This section will open a connection to the existing backup server and get the last ith_rid used.
# It will then store that ith_rid to be used later and then close the database connection
$mysqlconn = new mysqli('localhost','username','password','database');
if ($mysqlconn->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqlconn->connect_errno . ") " . $mysqlconn->connect_error;
}
###################################################
## Get username and password
##################################################
$res = $mysqlconn->query("SELECT username, password FROM table WHERE username = '" .$username . "'");
if (!$res) { ##If there is an error running query, display it to the screen.
echo "Error: $mysqlconn->error \n";
}
while($row = $res->fetch_assoc()) {
$un = trim($row["username"]);
$pw = trim($row["password"]);
}
if ($un == $username && $pw == $password){
$valid = 1;
}
echo json_encode($valid);
The php does return something, but it is in an object. Not sure how to access the variable from the javascript in order to determine if it is 1 or 0.
Edit:
So I changed things up a bit and it is working correctly now.
carrierchange.js
jQuery(document).ready(function () {
$("#content").append("<form name='loginForm' autocomplete='off'>");
$("#content").append("<table align=center>");
$("#content").append("<tr><td colspan=2 bgcolor=#87C9FF><center><h2>Login</h2></center></td></tr>");
$("#content").append("<tr><td><label for='un'>Username:</label></td><td><input id='un' name='un'></td></tr>");
$("#content").append("<tr><td><label for='pw'>Password:</label></td><td><input id='pw' name='pw' type='password'></td></tr>");
$("#content").append("<tr><td colspan=2><center><input type='submit' class='btn' value='Login' onClick='handleLogin()'></center></td></tr>");
$("#content").append("</table>");
$("#content").append("</form>");
document.getElementById('un').focus().focus();
});
function handleLogin() {
var username = document.getElementById('un').value;
var password = document.getElementById('pw').value;
var valid;
$.get("getLogin.php", {un: username, pw: password}, "json", function(data) {
console.log(data);
});
}
getLogin.php
<?php
$username = $_GET['un'];
$password = $_GET['pw'];
$valid = 0;
# This section will open a connection to the existing backup server and get the last ith_rid used.
# It will then store that ith_rid to be used later and then close the database connection
$mysqlconn = new mysqli('localhost','username','password','datebase');
if ($mysqlconn->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqlconn->connect_errno . ") " . $mysqlconn->connect_error;
}
###################################################
## Get username and password
##################################################
$res = $mysqlconn->query("SELECT username, password FROM cc_user WHERE username = '" .$username . "'");
if (!$res) { ##If there is an error running query, display it to the screen.
echo "Error: $mysqlconn->error \n";
}
while($row = $res->fetch_assoc()) {
$dbusername = trim($row["username"]);
$dbpassword = trim($row["password"]);
}
if ($dbusername == $username){
$valid = 1;
}
echo json_encode($valid);
?>
Upvotes: 0
Views: 1468
Reputation: 104795
Since $.post
is an asynchronous request, you should handle the received data in the callback function:
$.post("getLogin.php", {"un": username, "pw": password}, "json", function(data) {
console.log(data); //received data
});
Upvotes: 2