无法将jQuery变量集成到php中 [英] Cannot integrate jQuery variable into php

查看:88
本文介绍了无法将jQuery变量集成到php中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用php/mysql和jquery从数据库进行查询,以获取基于其国家/地区的用户列表. 我有一个mysql查询,它在选择选项字段中提取国家表格数据库.在此之后,我放置了一个jQuery代码以根据所选国家/地区与用户自动获取表格.这就是jquery代码的样子:

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>

我的问题是:如何将$(this).val()放入php而不是$ variabilatara,所以它看起来像这样:$ variabilatara = $(this).val();并且sql查询将根据所选国家/地区的更改进行修改.谢谢!

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!

推荐答案

您要尝试的操作称为AJAX.听起来很复杂,但实际上并非如此.请参阅这些示例以进行简单的说明.不要只看它们-复制/粘贴到您的服务器并使它们工作.更改值.看看它是如何工作的-非常简单.

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.

一个简单的示例

更复杂的示例

根据下拉列表1中的选择填充下拉列表2

您的代码对我来说有点困难,但是应该将其重构. (我不确定strTara在代码中的位置,但是我相信您可以从这里弄清楚它的位置.)

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:(您的PHP AJAX处理器文件)

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;
?>

这篇关于无法将jQuery变量集成到php中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆