无法将jQuery变量集成到php中

I try to make a query from database to get the list of users based on their country using php/mysql and jquery. I have a mysql query that extracts the countries form database in a select options field. After this, i put a jquery code to automatically get the table with users based on the selected country. This is how jquery code looks like:

<script>
$( "#tara" )
.change(function () {
var str = "";
$( "select option:selected" ).each(function() {
str += "<table class='table table-bordered table-striped'>" +
"<thead><tr><th><p><?php echo _('Tara');?></p></th></tr></thead>" +
"<tbody>" +
"<?php
$variabilatara = 182;
$test = mysql_query("SELECT * FROM utilizatori WHERE idt='$variabilatara'") ?>" +
"<?php while($row=mysql_fetch_object($test))
{
echo "<tr>";
echo "<td><p>$row->nume</p></td>";
echo "</tr>";
}
?>" + $( this ).val() + " ";
});
$( "#testare" ).html( str );
})
.change();
</script>

My question is: How to put the $( this ).val() into php instead of $variabilatara, so it will looks something like: $variabilatara = $( this ).val(); and the sql query will modify on change of selected country. Thank you!

What you are trying to do is called AJAX. Sounds complicated, but it really isn't. See these examples for a simplistic explanation. Do not just look at them -- copy/paste to your server and make them work. Change the values. See how it works - really very simple.

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1


Your code is a bit difficult for me to follow, but should be refactored something like this. (I am unsure where strTara figures in the code, but I'm sure you will be able to figure it out from here).

javascript/jQuery:

var strTara = <?php echo _('Tara');?>

$( "#tara" ).change(function () {
    selVal = $(this).val();
    $.ajax({
        type: 'post',
         url: 'another_php_file.php',
        data: 'variabilatara=' + selVal,
        success: function(data){
            var tblHead = "
                <table class='table table-bordered table-striped'>
                    <thead><tr><th><p>strTara</p></th></tr></thead><tbody>
            ";
            $( "#testare" ).html( tblHead + data );
        }
    });
});

another_php_file.php: (your PHP AJAX processor file)

<?php
    $var = $_POST['variabilatara'];
    $out = '';

    $result = mysql_query("SELECT * FROM utilizatori WHERE idt='$variabilatara'") or die(mysql_error());
    while($row=mysql_fetch_object($result)){
        $out .= "<tr>";
        $out .= "<td><p>" .$row->nume. "</p></td>"; //<== fixed
        $out .= "</tr>";
    }
    $out .= '</tbody></table>'; //<== fixed
    echo $out;
?>