dqotv26286 2014-01-17 06:43
浏览 46
已采纳

如何在JavaScript函数中传递两个php变量

This is my code for fetching values from DB in a Table. After fetching values I'm simply taking the ID of user in function to Activate and Deactivate that user.

<?php
$sql = "SELECT * FROM registration";
$result = mysqli_query($con, $sql);
while ($test = mysqli_fetch_array($result)) {
    $id = $test['id'];
    $status = $test['status'];
    echo "<tr align='center'>";
    echo"<td><font color='black'>".$test['username']."</font></td>";
    echo"<td><font color='black'>".$test['firstname']." " .$test['lastname'] .
    "</font></td>";
    echo"<td><font color='black'>" . $test['status'] . "</font></td>";
    echo"<td><a id='link' onclick=\"activate(" . $id . ");\">Activate</a></td>";
    echo"<td><a id='link' onclick=\"deactivate(" . $id . ");\">Deactivate</a></td>";

    echo "</tr>";
}
?>

Now, this is my AJAX code for activate()

function activate(item)
{
    var id = item;

    if (confirm("Do you wants to Activate user"))
    {
        $("#wait").css("display", "block");
        var id = item;
        var status = status;

        var xmlhttp;
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                window.location.reload();
            }
        }
        xmlhttp.open("GET", "php/update_act.php?id=" + id + "&req=activate", true);
        xmlhttp.send();
    }
}

I want to write or update the activate function so that, I will get the current status of the user so that I'll check if the User is already Activated or not. And if user is already Activated it should prompt me that this user is already activated else I should be able to Activate the User.

  • 写回答

1条回答 默认 最新

  • doupixian1436 2014-01-17 06:49
    关注

    I assume you want to pass $status in the function as well so you can do something as

    In Php code

     echo '<td><a id="link" onclick="activate(\''.$id.'\',\''.$status.'\');">Activate</a></td>';
    

    And you can change the JS function signature as

    In Javascript

    function activate(yourItem,uStatus){  }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊