简体   繁体   中英

How to pass two php variables in JavaScript function

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.

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){  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM