'Get user ID from user Email with Ajax query

I would like to have User ID with User Email with javascript on a click button action but unfortunately it doesnt work,

What am i doing wrong please ?

Here is the Javascript

( EDIT I MISSED A LINE )

(function($){
    $(document).on("click","#button_unsubscribe",function() {
        $.ajax({
            type: "GET",
            url: "/unsubscribe_user_id.php",
            data: { mail : $_GET('mail') },
            success: function(data){
                alert('user id = ' + data);
            }
        });
    });
})(jQuery);

Here is the php function ( more complex at the end but for now i'm blocking here)

<?php

$mail = $_REQUEST['mail'];
$user = get_user_by( 'email', $mail );
$userid = $user->ID;
echo $userid;

?>

Doesn't echo anything and return error 500

Thank you



Solution 1:[1]

Ok i find the trick i need to add some code line in the php file otherwise it seems he can't access to database

define('SHORTINIT', true);
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb;

Cheers

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christophe P.