AJAX JQUERY变量

I try to send data with ajax in php, but I use an on jQuery click ( on a button which send data while quantity > 1), but I have an error, php does not recognize my two variables : name and nb.

This is my JQUERY/AJAX :

$('.btn-circle').on('click', function(){
    var name = $('.quantite').attr('name');
    var nb = $('.quantite').text();
});

$('#form_top_ten').submit(function() {
    if(nb>=1){
        $.ajax({
            type: "POST",
            url: "handle_form/commande.php",
            data: {nb:nb,name:name}
        });
    }
    return false;
});

and this my php :

<?php
    session_start();
    require_once '../includes/config.inc.php';

    $id_user = $_SESSION['id_user'];
    $quantite = $_POST['nb'];
    $article = $_POST['name'];

    if(isset($quantite)){
        $req = $connection->prepare('INSERT INTO commande_articles(id_user,quantite,article) VALUES(:id_user,:quantite,:article)');
        $req->execute(array(
            'quantite' => $quantite,
            'article' => $article,
            'id_user' => $id_user
            ));
    }   
    else {
        echo('erreur');
    }
?>

Define the variables on a higher scope. Try this:

var name,nb;
$('.btn-circle').on('click', function()
{
     name = $('.quantite').attr('name');
     nb = $('.quantite').text();
});

In javascript, variables have function scope. What this means is, your variables name and nb are within an anonymous function, and hence they are unavailable outside. The quickest fix is to just remove the "var" keyword from your variable declarations. This will make the scope of these variables global instantly, and your code should work.

A neater solution, however, would be to declare them outside the anonymous function, and then refer them within the function - this way, you have more control over the scope of these variables (Refer Siamak's answer above)