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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作