Good Evening earth....
I have a question here, can someone guide me with my question

Here it goes.
I have a html form, which calls the function in javascript and prompts the user whether to delete a specific user.The dialog box prompts but it is not sending the value that i require.

here is my code

<script type="text/javascript">
function ConfirmChoice()
{
    answer = confirm("Do you really want to Delete?")
    if (answer !="0")
    {
        location = "deleteuser.php"
    }

}
</script>

<table>
<tr><td><input type="submit" name="exist" value="Add Staff" onclick="form.action='addstaff.php';"></td></tr>
<tr><td><select name="id">
<?php
//for each row we get from mysql, echo a form input

while ($row4 = mysql_fetch_array($result1)) {
echo "<option>$row4[ID]</option>\n";
}
?>
</select></td>
<td><input type="submit" name="exist" value="Delete Staff" onclick=" ConfirmChoice(); return false;"></td>
<td><input type="submit" name="exist" value="Edit Staff" onclick="form.action='editstaff.php';"></td></tr>
</tr>
</table>

//deleteuser.php

<?php
session_start();
        require_once 'connection_details.php';                  //Calling the Connection Detail php file
        $tbl_name="employee";                                       // Table name

        // Connect to server and select database.
        mysql_connect("$host", "$username", "$password")or die("cannot connect");
        mysql_select_db("$db_name")or die("cannot select DB");

        $id=$_GET["id"];
        $_SESSION['id']=$id; 

        $sql1=mysql_query("Delete FROM $tbl_name WHERE ID='$id'");
        echo "<h1>User has been DELETED</h1>";                    
        //header("refresh:1;url=displayemployee.php" );
?>

so, how i send the ID to deleteuser.php

Recommended Answers

All 2 Replies

Maybe I'm missing something but I don't see form tags surrounding your form.

The javascript function ConfirmChoice() should should be somehow told the ID of the user to delete. It could be read from the chosen option of the select element. So the select element should be assigned an ID (for accessing with javascript) and the options have to have values:

<tr><td><select name="id" id="user-id">

while ($row4 = mysql_fetch_array($result1)) {
    // for readability I assigned $row4[ID] to $userID variable
    $userID = $row4[ID];
    echo "<option value='$userID'>$userID</option>\n";
}

In the function ConfirmChoice() you first read the chosen value of the select element and then add it to the url as a query string.

<script type="text/javascript">
function ConfirmChoice()
{
    // assign the select element to userSelectElement
    var userSelectElement = document.getElementById('user-id');

    // read the chosen value
    var userID = userSelectElement.options[userSelectElement.selectedIndex].value;

    answer = confirm("Do you really want to Delete?")
    if (answer !="0")
    {
        // add a querystring to the url
        location = "deleteuser.php?id=" + userID
    }
}

Then you have the userID in the $_GET['id'] (which you test for) and use it in your query.

But it is safer to use POST for deleting users instead of GET which means use form tags as R0bb0b suggested, set method to post and do not use javascript since I do not think it is necessary.

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.