Ho all, here I explain my problem (as far as i red i didnt find any working solution). here I link my files:
progetto.html
<html>
<head>
<script type="text/javascript" src="funzioni.js"></script>
<title>Pagina iniziale</title>
</head>
<body align='center'>
<p>Gymnasium</p>
<p>icona</p>
<form id="ajaxForm" name="ajaxForm">
<table align="center">
<tr>
<td><label>Utente</label></td>
<td><input type="text" name="user" id="user" value="" /></td>
</tr>
<tr>
<td><label>Password</label></td>
<td><input type="password" name="password" id="password" value="" /></td>
</tr>
</table>
<input type="button" id="submit" name="submit" value="Accedi" onclick='submitForm()' />
</form>
<div id="risultato"></div>
</body>
javascript file
function createXMLHttpRequestObject(){
if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
if (window.ActiveXObject) { return new ActiveXObject(Microsoft.XMLHTTP); }
return null;
}
function submitForm(){
var ajax = createXMLHttpRequestObject();
ajax.onreadystatechange = function () {
if (ajax.readyState==4 && ajax.status==200){
var response = ajax.responseText;
document.getElementById("risultato").innerHTML = response;
}
}
ajax.open("post", "ajaxLogin.php", true);
var data = "utente=" + document.getElementById('user').value + "&password=" + document.getElementById('password').value;
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.send(data);
}
ajaxLogin.php
<?php
if (!isset($_POST["user"]) || !isset($_POST["password"])){
die("Bad login");
}
$user = $_POST['user'];
$pwd = $_POST['password'];
if ( (($user == "angel") && ($pwd == "devil")) || (($user == "john") && ($pwd == "smith")) ){
$response = "Benvenuto " . $user;
echo $response;
}
?>
Problem is I always receive Bad Login message even if I use the right user and password. It's a POST problem with I'm really having hard time figuring out the solution.
This is your data:
var data = "utente=" + document.getElementById('user').value + "&password=" + document.getElementById('password').value;
And this is what you are checking:
if (!isset($_POST["user"]) || !isset($_POST["password"])){
You should change utente
to user
or the other way around. In your form you are using user
as well so I would recommend using that everywhere.
So:
var data = "user=" + document.getElementById('user').value + "&password=" + document.getElementById('password').value;