proFX
proFX

Reputation: 71

Acess dynamically named submit buttons with PHP isset

I am fetching from the database and creating button for each ID.

echo "<td><input type='submit' id='".$row["id"]."' value='delete' name='delete'></td>";

Now, I want to delete the data from the database refered to id of delete button user clicked using PHP.

How can I do that? If any other method please suggest. enter image description here

Upvotes: 0

Views: 469

Answers (3)

Ravi
Ravi

Reputation: 173

<script>
    $("input[name='delete']").on('click',function (e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            URL:'yourpage.php',
            data:{"id":$(this).id},
            dataType:'JSON',
            success:function(data){
                if (data == 1) {
                    $(this).remove();
                }
                else{
                    console.log(data);
                }
            }
        })
    });
</script>

    //PHP

    if (isset($_POST['id'])) {

    // i assume that you have already connected to db and $conn is DB var

    $sql = "DELETE FROM yourtable WHERE id=".$_POST['id'];

    if ($conn -> query($sql) === TRUE) {
    echo 1;
    } else {
    echo "Error deleting record: ".$conn -> error;
    }

    }

Upvotes: 2

Nico Zimmer
Nico Zimmer

Reputation: 318

you can use a hidden input field, to insert the id. If you have the id its easy to delte the databaseentry.

<?php
echo "<td><form style='display:inline;'><input type='hidden' value='".$row["id"]." id='id'>
<input type='submit' id='submit' value='delete' name='delete'></td>";
if ($isset($_post["submit"])) {
    $deleteID=$_POST['id'];
    //here your order to delete from database $deleteID is your id
}

?>

Upvotes: 3

Masivuye Cokile
Masivuye Cokile

Reputation: 4772

I would suggest that you use ajax to send data to the delete page,

<?php

    echo "<td><button type=\"button\" id=\"delete_".$row["id"]."\" name='delete'>Delete</button></td>";


?>

Change the submit type to a button of a button then use click event and get the id value then send to php using ajax.

<script type="text/javascript">

    $('[id^="delete_"]').on('click', '', function() {

        var id = $(this).attr('id').split("_")[1]; // value of clicked button
        $.ajax({

                type : "POST",
                url  : "delete.php",
                data : {id:id},
                dataType : "json",
                encode   : true,
                success : function(data){

                    if(data == "ok"){

                        alert("data deleted success");
                    }else{

                        console.log(data); //return error debugging puporse only
                    }
                }
        });
    });
</script>

NB : Don't forget to include Jquery.

Then

delete.php

<?php


    $id = isset($_POST['id']) ? $_POST['id'] : null;


    //delete query where id = $id you should have this already.


    if($success){ //if query success

        echo json_encode("ok"); // send back response
    }else{

        //handle error
    }

Upvotes: 3

Related Questions