Originariamente inviato da Feder
E' possibile far partire una funzione php da Ajax?
Diciamo che detto così non è proprio corretto ma il risultato è proprio quello :-)

ti posto un esmpio (preso da un libro) che fa proprio quello che vuoi te (manca la parte sql ma la puoi integrare te quella)

file Ajax1.js
Codice PHP:
var request false;

if (
navigator.appName == "Microsoft Internet Explorer") {
    
request = new ActiveXObject("Microsoft.XMLHTTP");
} else {
    
request = new XMLHttpRequest();
}

function 
check(name
{
    
request.abort();
    
request.open("GET""Ajax1.php?name=" nametrue);
    
    
request.onreadystatechange=function() {
        if (
request.readyState == 4) {
            
document.getElementById('message').innerHTML request.responseText;
        }
    }
    
request.send(null);

come vedi questo non fa altro che fare una get al file Ajax1.php, ma questò verrà effettuato in modo asincrono

file Ajax1.php
Codice PHP:
<?php
function checkUsername($username) {
    
$existingUsers = array('rasmus''zeev''andi');
    
// empty check
    
if ($username == '') {
        return 
'';
    } elseif (
strlen($username) < 4) {
        return 
'<span class="error">
            Username is less than 4 characters
            </span>'
;
    } elseif (
in_array($username$existingUsers)) {
        return 
'<span class="error">
            Username already exists
            </span>'
;
    } else {
        return 
'<span class="ok">
            Username is acceptable
            </span>'
;
    }
}

if(!
class_exists('PHPUnit_Framework_TestCase')) {
    
$name = isset($_GET['name']) ? $_GET['name'] : '';
    echo 
checkUsername(trim($name));
}
Come vedi c'è un vettore di usr gia inserite invece della ricerca nel db, ma questo si può adattare facilmente

ed infine il file Ajax1.html
Codice PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>
<
html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<
head>
<
title>Simple Ajax Example</title>
<
meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<
style>
    .
ok {colorgreen;}
    .
error {colorred;}
</
style>
<
script type="text/javascript" src="Ajax1.js"></script>
</head>
<body>

<h1>Registration:</h1>

<form action="#">
 <div>
 <label for="name">Username:</label>
 <input id="name" name="name" type="text" onkeyup="check(this.value)" />
 <div id="message"></div>
 </div>
</form>
</body>
</html> 
fatti questi 3 file poi apri Ajax1.html e guarda il risultato