function edit_user($username){
        if ( $username == 'admin' ) {
                $query = "SELECT * FROM login WHERE username != 'admin'";
                $queryResult = mysql_query($query) or die (mysql_error());
                echo "<table border='1'>

                <tr>
                <th>Loginid</th>
                <th>username</th>
                <th>email</th>
                <th>activated</th>
                <th>Edit User</th>
                <th>Delete user</th>
                </tr>";
                while($rows = mysql_fetch_array($queryResult)) {
//Todo: Here is a problem
//probably use mysql_num_array function...gonna fix that this.
//
//              $_SESSION['username'] = $rows['username'];
                echo "<tr>";
                echo "<td bgcolor='#FF9933'>". $rows['loginid'] . "</td>";
                echo "<td bgcolor='#FF9933'>" . $rows['username'] . "</td>";
                echo "<td bgcolor='#FF9933'>" . $rows['email'] . "</td>";
                echo "<td bgcolor='#FF9933'>" . $rows['activated'] . "</td>";
                echo "<td bgcolor='#FFFFFF'><center>" . '<input type="button"  name="Activate" value="Activate" class="form"
                         onClick="activate_user_id('.$rows['username'].','.$rows['loginid'].','.$rows['activated'].')">' . "</center></td>";
                echo "<td bgcolor='#FFFFFF'><center>" . '<input type="submit" name="Delete" value="Delete" class="form"
                         onClick="delete_user('.$rows['username'].','.$rows['loginid'].','.$rows['activated'].')">' . "</center></td>";
                echo "</tr>";
        }
        echo "</table>";
        }else {
        echo "$username";
      echo "<p>You dont have permission to access the Admin site</p>";
        }
}

function activate_user_id ($username, $loginid, $activated) {
global $seed;
        if (!valid_username($username) || !user_exists($username))
    {
        return false;
    }

    $query = sprintf("select activated from login where username = '%s' and loginid = '%s' and activated = '%s'  limit 1",
    mysql_real_escape_string($username), mysql_real_escape_string($loginid));
    $result = mysql_query($query);

    if (mysql_num_rows($result) == 1)
    {

      $sql = sprintf("update login set username = 'eric15'  where username ='%s' and loginid = '%s' and activated = '%s'",
       mysql_real_escape_string($username), mysql_real_escape_string($loginid));
        return true;
    }
}

function delete_user ($username, $loginid, $activated) {
    $query = sprintf("select activated from login where username = '%s' and loginid = '%s' and activated = '%s'  limit 1",
    mysql_real_escape_string($username), mysql_real_escape_string($loginid));
    $result = mysql_query($query);

    if (mysql_num_rows($result) == 1)
    {

      $sql = sprintf("delete from login where username = '%s' and loginid = '%s' and activated = '%s'",
       mysql_real_escape_string($username), mysql_real_escape_string($loginid));
        }
}

Hi erixxye,

As this is your first post to the forums, you might want to keep some things in mind:

- Place code between CODE tags
- When you post, first describe your problem, then post code

But anyway, onto your issue. You are mixing up PHP with JavaScript. The OnClick attribute is HTML, generated by the PHP code. PHP retrieves form values from submitted HTML forms. So when you click the button in your code, it just simply looks for a JavaScript function called activate_user_id and since it does not exist, it does nothing.

It would appear as if you got the code from some place and tried to adapt it to work on your own website, but I could be wrong...

Anyway, you should replace the <input type="button|submit"> with:

echo '<form action="" method="post">
<input type="submit" name="delUserSubmit">
<input type="hidden" name="loginid" value="'.$rows["loginid"].'" />
</form>';

And then you can delete the user using the following PHP code:

<?php

/* Checking whether a user needs to be deleted: */
if (isset($_POST['delUserSubmit'])) {

    /* Retrieving loginid from the form: */
    $loginid = htmlentities(addslashes($_POST['loginid']));

    // TODO : Delete the user from the login table

}

?>

I would recommend that you read a beginner book on PHP & MySQL (like PHP & MySQL for dummies) and a beginner book on HTML.

~G

ericxye, I have no idea what you are looking for...

echo "<td bgcolor='#FFFFFF'><center>" . '<input type="button" name="Activate" value="Activate" class="form"
onClick="activate_user_id('.$rows['username'].','.$rows['loginid'].','.$rows['activated'].')">' . "</center></td>";

Can anyone help me to fix this code as an AJAX code so that i can click on a button so that it can call a function and update my sql. At the same time it can refresh the page

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.