获取特定的php变量到jquery函数参数的循环 [英] getting specific php variable out of a loop to jquery function parameter

查看:121
本文介绍了获取特定的php变量到jquery函数参数的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动建议文本框,它通过函数的onclick事件生成li标签,但我必须将php变量传递给函数作为参数,但我只希望用户单击li的变量值,现在我总是获取循环中的最后一个变量. 我希望var test为$ test = $ row ['t_product'];的值;用户点击的li的百分比

这是我现在使用的代码:

i have a auto suggest textbox witch generates li tags with a onclick event with a function but i have to pass a php variable to the function as parameter but i only want the variable value of the li the user clicked on and now i always get the last variable in the loop. And i want the var test to be the value of $test = $row['t_product']; of the li the user clicks on

here is the code im using now:

    if(isset($_POST['search_term']) == true && empty($_POST['search_term']) == false){    
    $search_term = $_POST['search_term'];
     $customerid = $_SESSION['userdata']['t_customer_id'];

 $conn = connect();
$sql = "SELECT * FROM Licenses WHERE customer_id = '$customerid' AND t_name LIKE '$search_term%'";
$query = sqlsrv_query($conn, $sql);

while(($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) != false){
$test = $row['t_product'];
?>
<script type="text/javascript">
 var test = <?php echo json_encode($test); ?>;
</script>
<?php
        echo '<li onclick="show(test)">', $row['t_name'], '</li>';
         }

}

我希望我能很好地解释我的问题,并希望有所帮助可以帮助我:)

i hope i explained my problem well and hope that some wane can help me :)

推荐答案

通过将$ test值作为id传递给每个li并在js中使用

solved it by passing the $test value as id to each li and in the js get it with

 $test = $row['t_product'];
    echo '<li onclick="show(this)" id="'.$test.'">', $row['t_name'], '</li>'

function show(id){
    alert($(id).attr("id"));}

这篇关于获取特定的php变量到jquery函数参数的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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