0

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!

RCtm
  • 3
  • 1
  • You can't directly pass a jquery or javascript variable to a php variable, you have to make the php script on another page, and then use ajax to post to that page, and get the result. – Jesper Feb 24 '15 at 16:24
  • 1
    php and javascript run in different environments and at different times. You need to get a better understanding of this. php runs on server, javascript in browser – charlietfl Feb 24 '15 at 16:25
  • Javascript is a client-side language, while php is a server-side one. They don't fit... Not directly, at least. check what is an AJAX request :) . Also mysql_* prototype is **deprecated**, use mysqli_* or PDO instead (check the official PHP documentation) – briosheje Feb 24 '15 at 16:25
  • 1
    Before web programming you have to understand the fundamentals of comunication between server and client. You are asking server to modify a document that has alredy been downloaded. The right way is you send the client modification done with jQuery back to the server making it aware and than using the php to modify it. – kidwon Feb 24 '15 at 16:26
  • The fact you're asking this indicates that you're a bit light on the knowledge of how HTTP and page requests work. PHP executes on the server to create the page, and JavaScript executes in the browser entirely AFTER the PHP finishes executing. It's not possible to pass a variable from the JavaScript back into the PHP inline because the PHP is already finished. – amphetamachine Feb 24 '15 at 16:29

1 Answers1

0

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;
?>
Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111