0

Hope you´re doing well today. ;)

First of all, here is my problem:

Im loading data from my database table dynamic in a HTML-table. So far, so good. The dynamic loading of the data in my HTML table works pretty well...

Now, I want to check if the delete button is pressed and also want the ID from the record where the delete button was pressed.

The HTML table has the following values: id, FirstName, LastName, Radio-Button (To select the record which should be deleted later on), Delete - Button (To delete the selected record)

Here is my source Code:

<?php

$query = $con->prepare("SELECT id, vorname, nachname, herkunftsland FROM schauspieler;");
$query->bind_result($id, $firstname, $lastname, $city);
$query->execute();

while($query->fetch()) {

    $actor_information[$id]['id'] = $id;
    $actor_information[$id]['firstname'] = $firstname;
    $actor_information[$id]['lastname'] = $lastname;
    $actor_information[$id]['city'] = $city;
}

?>

<html>
<head>
    <title></title>
</head>
<body>

<p></p>
<h4>Actors:</h4>
<hr /><br />

<!--
<table border="3">
<tr>
    <td><input type="radio" name="1"></input></td>
    <td><input type="radio" name="1"></input></td>
    <td><input type="radio" name="1"></input></td>
    <td><input type="radio" name="1"></input></td>
</tr>
</table>
-->

<form action="" method="post">
<table border="3">
    <th>id</th>
    <th>FirstName</th>
    <th>LastName</th>
    <th>City</th>
    <?php

    foreach($actor_information as $key => $value) {

        echo "<tr>";
        printf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><input type='radio' name='dvd_id' value='%s'></input></td><td><input type='button' name='delete_actor' value='delete'></input></td>", $value['id'], $value['firstname'], $value['lastname'], $value['city'], $value['id']);
        echo "<tr>";
    }

    ?>
</table>
</form>

Could anyone help me out to get the ID from the selected record...? I just need it to delete the record where the radio button is set.

Hopefully I can get some help from you guys...

Best regards & have a nice day, Peter

herzDev
  • 13
  • 3

4 Answers4

1

If you give your delete button a "name" attribute, it will be included in the post data. Thus, you can check for its presence in post and act on it.

The following PHP provides a text input and two different submit buttons, each with different names. It also prints $_POST's contents, so you can see how it changes depending on which button you press.

<!DOCTYPE html>
<html>
<head><title>Test post!</title></head>
<body>

<?php var_dump($_POST); ?>

<form method="post">
    <input name="test" type="text" value="Test" />
    <input type="submit" name="delete" value="Delete!" />
    <input type="submit" name="submit" value="Submit!" />
</form>
</body>
</html>

In your specific case, it should be enough to use a different name attribute for each actor, for example to use name='delete_".$value["id"]."'" or similar.

I appear to have mis-understood the question. If you're using just one delete button, have a bullet for each record, and the bullet's value is the ID of the record to delete, you should only have to check that the delete button's name exists in $_POST, then use whatever database code you are using to delete the record that matches $_POST["dvd_id"].

Marcus Harrison
  • 819
  • 6
  • 19
0

What's the idea of having these 4 radio buttons in the first row?

The way I implemented this in my apps is to add a X-image in the first col of every row which opens a new popup (possibly after confirmation and with off-screen coordinates, so the user does not see it) which calls a delete_record.php?id=$id-URL.

MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Not a good idea to use URL parameters when deleting data, Google will crawl the inks and give you endless headaches... – scrowler Feb 10 '14 at 09:09
  • Thanks - you are right. I've been talking intranet, and would also hope and crawlers would not follow links embedded in JS. – MBaas Feb 10 '14 at 13:14
0

You can use a delete link, and style it like a button (see). The link location is the current url + the parameter 'delete_resource' with as value the row ID.

On top of the page, check if the parameter 'delete_resource' isset, and delete row based on that id.

Community
  • 1
  • 1
Ruben
  • 343
  • 2
  • 11
0

I would use jquery ajax for this, a radio is intended only to have one select-able option, a checkbox is what you need to do this the way you intend. however if you consider you will have to click each row anyway, it would be simpler to do away with the form and add a button on each row with a data-id='%s' for carrying the id,and fire through jquery so.

<?php
$query = $con->prepare("SELECT id, vorname, nachname, herkunftsland FROM schauspieler;");
$query->bind_result($id, $firstname, $lastname, $city);
$query->execute();

while($query->fetch()) {
   $actor_information[$id]['id'] = $id;
   $actor_information[$id]['firstname'] = $firstname;
   $actor_information[$id]['lastname'] = $lastname;
   $actor_information[$id]['city'] = $city;
}

?>


<!DOCTYPE html>
<html>
<head>
  <title>Some Page Title</title>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<table border="3">
   <th>id</th><th>FirstName</th><th>LastName</th><th>City</th><th>Option</th>
<?php
foreach($actor_information as $key => $value) {
    echo '<tr class="user-row">';
    printf('<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td><button class="delete-user" data-id="%s">Delete </button></td>', $value['id'], $value['firstname'], $value['lastname'], $value['city'], $value['id']);
    echo '<tr>';
}

?>
</table>

<script type="text/javascript">
  jQuery(document).ready(function($){
      $('.user-row').each(function () {
          $(this).find('.delete-user').click(function(e){
               var user_id = $(this).data('id'); 
               var callback = function (response) {
                        alert(response);
                       $(this).parent('.user-row').remove();
               }
              delete_user(user_id,callback);
          });
      });
  });

function delete_user(user_id,callback){
var ajax_url = "/path/to.php";
    $.ajax({
    type: "POST",
    url: ajax_url,
    data: { user_id:user_id },
    context:this,
    success: function(response) {
                callback(response);
            }
     });            
};
</script>

</body>
</html>

the value can then be retrived by php with $_POST['user_id']; and passed into mysql for deletion however beware mysql injection as a method like this without some sanitation can open your database up to hackers

dom
  • 254
  • 1
  • 4
  • 18