Home » Php » mysql – Why doesn't my delete button work in PHP?

mysql – Why doesn't my delete button work in PHP?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m just starting out with PHP and am a bit lost. I feel like it’s probably something small I’m missing but when I click the delete button it does not work. It doesn’t give me any errors, the button just clicks and does nothing. Any ideas why? I’ve spent hours trying to figure it out but I have come up with nothing. I’ve done add user and show users in much the same way and it works but this has me stumped. Thanks in advance 🙂

users.controller.php

static public function DeleteUserController(){

        if(isset($_GET["userId"])){

            $table ="users";
            $data = $_GET["userId"];
            var_dump($data);
            $answer = UserModel::DeleteUserModel($table, $data);

            if($answer == "ok"){

                echo'<script>

                swal({
                      type: "success",
                      title: "User has been Deleted",
                      showConfirmButton: true,
                      confirmButtonText: "Close"

                      }).then(function(result){

                        if (result.value) {

                        window.location = "users";

                        }
                    })

                </script>';

            }       

        }

    }

users.model.php

static public function DeleteUserModel($table, $data){

        $stmt = Connection::connect()->prepare("DELETE FROM $table WHERE id = :id");

        $stmt -> bindParam(":id", $data, PDO::PARAM_STR);

        if ($stmt->execute()) {

            return 'ok';

        } else {

            return 'error';

        }

        $stmt -> close();

        $stmt = null;
    }

users.js

$(".btnDeleteUser").click(function(){
    var userId = $(this).attr("userId");
    var username = $(this).attr("username");

    swal({
        title: 'Are you sure you want to delete user?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
          cancelButtonColor: '#d33',
          cancelButtonText: 'Cancel',
          confirmButtonText: 'Confirm'
        }).then((result)=>{

        if(result.value){

          window.location = "index.php?route=users&userId="+userId+"&username="+username;

        }

    })

})

users.php

<button class="btn btn-danger btnDeleteUser" userId="'.$value["id"].'" username="'.$value["user"].'"><i class="fa fa-times"></i></button>

users.php

<?php

  $deleteUser = new ControllerUsers();
  $deleteUser -> DeleteUserController();

?> 

index.php

<?php

require_once "controllers/template.controller.php";
require_once "controllers/products.controller.php";
require_once "controllers/sales.controller.php";
require_once "controllers/users.controller.php";

require_once "models/products.model.php";
require_once "models/sales.model.php";
require_once "models/users.model.php";

$template = new TemplateController();
$template -> tempController();
How to&Answers:

I see a few issues going on in this script that we can start with. First, it’s public static function, not static public function as per the documentation, php Static. Next, you are instantiating the ControllerUsers with new ControllerUsers() and calling the DeleteUserController as an instantiated method. When you use static methods or variables, you do not instantiate the class, you call it directly using ControllerUsers::DeleteUserController(). Again, see php Static from above. That’s the purpose of using static methods, you can call them without instantiating a class.

My recommendation is to not use static methods at all when you are first learning PHP unless you are familiar with other programming languages and their use of Static Methods. Static methods are generally frowned upon and have limited real world use anyway, so it’s best to instantiate the class and work with methods that way instead.

Anyway, back to the script. When you are creating a program to delete a user, it is more accepted to use POST instead of GET, as it does not transfer data in the URL, which makes it more secure. I would recommend switching to a POST action instead of a GET action.

Also, can you post the index.php file? I am curious to see how these routes work in the context of your program.

Let’s start with these before continuing.

Update:

So, if you click the Delete User Button and nothing happens, not even any errors, there can be a few different reasons for that. The first big one is that the users.js file has not been added to the page that has the Delete User button, so make sure that it’s been added to that page.

Next, you need to make sure that the page is loaded before using the click function in Jquery. Check here for reference, Jquery Click.

For me personally, I have had issues using the .click method in Jquery, so I normally use the .on method instead. It’s a newer method with better functionality, I’ve found, so give it a try as well. An example can be found in Jquery Click from above as well for you.

Answer:

Thanks for all your help. It was, as I said, a simple error. I didn’t link the Javascript file in my template.php file:

<script src="views/javascript/template.js"></script>
<script src="views/javascript/users.js"></script>
<script src="views/javascript/categories.js"></script>