Domanda

Ho problemi a usare AJAX e PHP. Quello che sto cercando di fare è chiamare una funzione AJAX che ottiene un valore dall'input di un modulo e controlla se quell'e -mail esiste in un database. Ecco il mio attuale JavaScript:

//Checks for Existing Email
function checkExisting_email() {
    $.ajax({
        type: 'POST',
        url: 'checkExist.php',
        data: input
    });

emailExists = checkExisting_email();

//If it exists
if (emailExists) {
    alert("This email already exists!");
}

Sfortunatamente, non riesco a far scattare il mio avviso. Nella mia funzione PHP, controlla se l'input è un nome utente o un'e -mail (solo per i miei scopi, e quindi lo sai), e poi lo cerca in entrambe le colonne. Se lo trova, restituisce vero e, in caso contrario, restituisce falso:

include ('func_lib.php');
connect();
check($_POST['input']);

function check($args)
{
    $checkemail = "/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i";
    if (!preg_match($checkemail, $args)) {
        //logic for username argument
        $sql = "SELECT * FROM `users` WHERE `username`='" . $args . "'";
        $res = mysql_query($sql) or die(mysql_error());

        if (mysql_num_rows($res) > 0) {
            return true;
        } else {
            return false;
        }
    } else {
        //logic for email argument
        $sql = "SELECT * FROM `users` WHERE `email`='" . $args . "'";
        $res = mysql_query($sql) or die(mysql_error());

        if (mysql_num_rows($res) > 0) {
            return true;
        } else {
            return false;
        }
    }

}

Quindi il mio problema è: in che modo l'Ajax risponde a questi rendimenti e come faccio a far funzionare l'Ajax di conseguenza? Principalmente, perché non funziona?

Qualsiasi aiuto è molto apprezzato. Grazie!

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top