duanfu9523 2015-02-24 16:20
浏览 16
已采纳

无法将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!

  • 写回答

1条回答 默认 最新

  • dongzhina7098 2015-02-24 16:39
    关注

    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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法