Hi,
i'm using a javascript so i can do a messagebox confirmation, and then it goes to a .php where is the delete sql. But is not working:

<!---------------------------------------------->
            <script language="JavaScript">
                function confirmBox(id) 
                {
                    if (confirm("Tem a certeza que pretende eliminar este utilizador? ")) 
                    {
                        location.href="remover_user.php";
                    }
                    else 
                    {
                        return false;
                    }
                }
            </script>
            <!---------------------------------------------->

            (.............)

            <td width="5%">
                <a href="#" onclick="confirmBox('<?php echo $row_users['id_utilizador']; ?>')">Remover</a>
            </td>

remover_user.php:

 <?php
    include 'db_connect.php';

    session_start();

    if (isset($_GET['id_utilizador']))
    {
        $delete = "DELETE FROM utilizadores WHERE id_utilizador='".$_GET['id_utilizador']."'";
        $resultado = mysql_query($delete, $connect);

        header ('Location: users.php'); 
    }
    ?>

Recommended Answers

All 9 Replies

i am not great with javascript, but i dont think it passes variables via $_GET as you are not sending this information to the URL

I'm no good with JS, but you might be better off with simple redirecting to a confirmation page where is says do you really want to delete this account, yes or no.

Share the confirm box code and in remove_user.php, type

echo "<pre>";
print_r($_REQUEST);
echo "</pre>";

and share it

Member Avatar for diafol

I'd really advise against this simplistic approach. JS can't pass data to php via this method. You probably need ajax here. I would also advise that you use some sort of confirmatory / validatory hash to ensure that the data is coming from your form / you.

While I do think that Echo89 and mustafaneguib are correct (that you might be better off using one of their suggested methods instead), I think your code will work if you update the URL that you are redirecting to in your confirmBox function. If you notice, you were on the right track by defintioning the function to take the id as an argument, but you fail to use the id in the function.

Try replacing location.href="remover_user.php"; with location.href="remover_user.php?id_utilizador=id";

Note that even if that causes your code to work, visitors to your site that have JavaScript turned off will not receive any confirmation before doing the deletion, which is another reason to consider using an intermediate PHP page to do the confirmation.

Dave

edit yout javascript to:

<script language="JavaScript">
        function confirmBox(id){
            if (confirm("Tem a certeza que pretende eliminar este utilizador? ")){
                    location.href="remover_user.php?id="+id;//EDIT THIS LINE
            }
            else{
                return false;
            }
        }
</script>

and the edit the link to

<a href="#" onclick="javascript:confirmBox('<?php echo $row_users['id_utilizador']; ?>')">Remover</a>

Member Avatar for jmichae3

you CAN do this with JS, here's how:

<!---------------------------------------------->
<script language="JavaScript">
function confirmBox(id)
{
    window.confirmid=id;//global, can use elsewhere in js
    if (confirm("Tem a certeza que pretende eliminar este utilizador? "))
    {
        location.href="remover_user.php?id_utilizador="+id; //pass the id to PHP using $_GET['id_utilizador']
    }
    else
    {
        return false;
    }
}
</script>
<!---------------------------------------------->
(...some database query...)
<td width="5%">
<a href="#" onclick="confirmBox('<?php echo $row_users['id_utilizador']; ?>')">Remover</a>
</td>
Member Avatar for jmichae3

actually, I just realized that those HTML comments will cause a problem since they are nonstandard. the require a space between <!-- and --> and it will especially cause a problem with firefox.
so,

<!-- ------------------------------------------ -->
    <script language="JavaScript">
    function confirmBox(id)
    {
    window.confirmid=id;//global, can use elsewhere in js
    if (confirm("Tem a certeza que pretende eliminar este utilizador? "))
    {
    location.href="remover_user.php?id_utilizador="+id; //pass the id to PHP using $_GET['id_utilizador']
    }
    else
    {
    return false;
    }
    }
    </script>
    <!-- ------------------------------------------ -->
    (...some database query...)
    <td width="5%">
    <a href="#" onclick="confirmBox('<?php echo $row_users['id_utilizador']; ?>')">Remover</a>
    </td>
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.