dqotv26286 2014-01-16 22: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-16 22: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){  }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部