Hello,

I am trying to create a delete button when you press the delete button it takes to a list of codes to delete the row.

input_image.php

<?php
                    $i=0;
                    while ($data = mysql_fetch_array($result)){
                    $result2=($i%2)?'#DFA09D':'white';


                            //echo "<tr bgcolor='$result2'>";                    
                            //echo '<td>'.$data['page'].'</td>';
                            //echo "<td><a href='post.php?post_id=".$data['post_ID']."'><img src='../images/post.jpg'></a></td>";
                            //echo '</tr>';                  

                            echo "<tr bgcolor='$result2'>";                  
                            echo '<td><a href="update_image2.php?image_id='.$data['image_id'].'"><img src="images/'.$data['newfilename'].'" height="100"></a></td>';
                            //echo '<td>'.$data['newfilename'].'</td>';
                            echo '<td><div style="margin: -40px 0 0 -10px; position: absolute;">'.$data['minwidth'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['maxwidth'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['minheight'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 20px; position: absolute;">'.$data['maxheight'].'px</div></td>';
                            //echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['location'].'</div></td>';
                            echo '<td><a href="update_image2.php?image_id='.$data['image_id'].'"><img src="images/'.$data['newfilename_bn'].'" height="100"></a></td>';
                            echo '<td><div style="margin: -40px 0 0 -10px; position: absolute;">'.$data['minwidth_bn'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['maxwidth_bn'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['minheight_bn'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 20px; position: absolute;">'.$data['maxheight_bn'].'px</div></td>';
                            //echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['location_bn'].'</div></td>';
                            echo '<td><button>Delete</button></td>';
                            echo '</div></tr>';


                    $i++;   
                    }
                    ?>

How to connect the delete button with the list of codes(to delete the row) ?

Recommended Answers

All 15 Replies

Member Avatar for diafol

Sorry davy, but this php-generated markup is horrible. Can't you apply styling via css? I know we've discussed this before many times. It just makes the whole thing extremely difficult to maintain.

The delete button can be used via ajax, in which case you need only apply the "id" value fo the row to delete in a data attribute on the button itself. I suggest giving the buttons a class for a javascript hook...

<button class="deleterow" data-id="<?php echo $row['id'];?>">DELETE</button>

If using jquery...

$('.deleterow').click(function(e)
    e.preventDefault();
    var id = $(this).data("id");
    //run ajax routine
    //delete the row on success - you can use the closest('tr') to get the row from the active button
)};

You should ensure that the delete routine can only be run if the logged in user has sufficient rights.

The simplest way is to enclose the whole html table within a form. In each row there is a delete button which is actually an input element with a type="submit". The name attribute of each delete button should be a corresponding ID of the row in the database (typicaly a primary key by which you address the row to be deleted). So hitting a delete button will trigger a request and send the ID of the row to be deleted in a $_POST (or possibly a $_GET but not recommended). All you need in a deleting part of the script to fire a DELETE SQL statement to the database.

The disadvantage of this approach is in that the page gets refreshed on each delete which might be not that user friendly if you wish to delete many rows quickly. Other solutions are to have a checkbox in each row and one delete button for the whole html table or to use Ajax (so the rows get deleted behind the scenes).

Edit: Ajax, as Diafol already suggested above :-)

Member Avatar for diafol

Alternatively you can use the traditional "value" attribute - probably better actually.

<button class="deleterow" name="deleterow[]" value="<?php echo $row['id'];?>" type="submit">DELETE</button>

If you contain the table in a form tag with a method and action it should then work even when javascript turned off.

So jQ now:

$('.deleterow').click(function(e)
    e.preventDefault();
    var id = $(this).val();
    //run ajax routine
    //delete the row on success - you can use the closest('tr') to get the row from the active button
)};
<form action="" method="post">
echo '<td><input type="submit" name="delete" value="'.$row['id'].'" /></td>"';
<?php

if(isset($_POST['delete']) and is_numeric($_POST['delete']))
{
  //  $_POST['delete'] as your id
}
Member Avatar for diafol

From a 'delete many' at once approach as broj1 suggests (nice one!) you could do a two in one approach.

BTW - reason for using button instead of input:submit is that you get to specify the value attribute to whatever you want. With input:submit the value also ends up being the "button label".

<tr>
    <td><input class="rowcheck" name="rowcheck[]" type="checkbox" value="<?php echo $row['id'];?>" /></td>
    <td>Some data</td>
    <td>Some data</td>
    <td>Some data</td>
    <td>Some data</td>
    <td>Some data</td>
    <td><button class="deleterow" type="submit" value="<?php echo $row['id'];?>">DELETE</button></td>
</tr>    

The markup above should allow you to do a "check multiple rows and select a suitable action to perform" type operation. I'm sure you've seen them. This is why I called the checkboxes "rowcheck" and not something like "deletecheck" - which would be fine too if the only action that you need to perform on selected checkboxes is "delete".

The row ids can be picked up by traditional form submission thus:

session_start();
if(!isset($_SESSION['level']) || $_SESSION['level'] > 1) header("Location: index.php"); //prevent naughty people running your script
if(isset($_POST['rowcheck']) && isset($_POST['action']) && $_POST['action'] == 'delete') 
{
    $ids = (array) $_POST['rowcheck'];
    $sql = "DELETE FROM table WHERE id IN (" . implode(",", $ids) . ")";
    //run query etc
}

The code above can be used for Ajax too.

commented: Nice one, too +11

This is what I tried:

I wonder why it doesn't work?

<?php


if($_POST['submit'])
{
echo "hello";

}

?>


<td><button class="deleterow" type="submit" value="">DELETE</button></td>

Notice: Undefined index: submit in C:\xampp\htdocs\delete_test\index.php on line 15

This is what I tried: #revised

<?php


if(!empty($_POST['delete']))
{
echo "hello there!";

}

?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <td><button class="deleterow" type="submit" name="delete" value="">DELETE</button></td>

</form>  

You should use isset():

if(isset($_POST['delete'])) {
    ...
}

input_image.php

<?php


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

            echo "hello!";
            }

?>


        echo '<td><button class="deleterow" type="submit" name="delete" value="">DELETE</button></td>';

I wonder why the hello! text does not work?

Member Avatar for diafol

When you submit, you need a form "method" set to "post" as the default is "get". I'm assuming you have no form tag or the method is not set to "post".

update_image2.php

$image = mysql_real_escape_string($image);
$location = mysql_real_escape_string($location);
Member Avatar for diafol

I have no idea what this post is all about. You must attempt to articulate yourself more clearly. You've jumped from one thing to another without any explanation as to what you're doing. Is there a problem? What's thed point of showing update_image2.php?

http://localhost/portal/administrator2/admin/input_image.php?delete=

input_image.php

<form method="get" action="<?php echo $_SERVER['PHP_SELF'].'?image_id='.$image_id; ?>">
                <table style="margin: 50px 0 0 0px; position: relative;" id="admintable" border="1" cellpadding="2" cellspacing="0">
                    <tr>
                        <th style="width: 300px"><center>image</center></th><th style="width: 80px"><center>minwidth</center></th><th style="width: 80px"><center>maxwidth</center></th><th style="width: 80px"><center>minheight</center></th><th style="width: 80px"><center>maxheight</center></th><th style="width: 80px"><!--<center>location</center>--><center>image_bn</center><th style="width: 80px"><center>minwidth_bn</center></th><th style="width: 80px"><center>maxwidth_bn</center></th><th style="width: 80px"><center>minheight_bn</center></th><th style="width: 80px"><center>maxheight_bn</center></th><th>Post</th><!--<th><center>location_bn</center></th>-->
                    </tr>
                    <?php
                    $i=0;
                    while ($data = mysql_fetch_array($result)){
                    $result2=($i%2)?'#DFA09D':'white';


                            //echo "<tr bgcolor='$result2'>";                    
                            //echo '<td>'.$data['page'].'</td>';
                            //echo "<td><a href='post.php?post_id=".$data['post_ID']."'><img src='../images/post.jpg'></a></td>";
                            //echo '</tr>';                  

                            echo "<tr bgcolor='$result2'>";                  
                            echo '<td><a href="update_image2.php?image_id='.$data['image_id'].'"><img src="images/'.$data['newfilename'].'" height="100"></a></td>';
                            //echo '<td>'.$data['newfilename'].'</td>';
                            echo '<td><div style="margin: -40px 0 0 -10px; position: absolute;">'.$data['minwidth'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['maxwidth'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['minheight'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 20px; position: absolute;">'.$data['maxheight'].'px</div></td>';
                            //echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['location'].'</div></td>';
                            echo '<td><a href="update_image2.php?image_id='.$data['image_id'].'"><img src="images/'.$data['newfilename_bn'].'" height="100"></a></td>';
                            echo '<td><div style="margin: -40px 0 0 -10px; position: absolute;">'.$data['minwidth_bn'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['maxwidth_bn'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['minheight_bn'].'px</div></td>';
                            echo '<td><div style="margin: -40px 0 0 20px; position: absolute;">'.$data['maxheight_bn'].'px</div></td>';
                            //echo '<td><div style="margin: -40px 0 0 10px; position: absolute;">'.$data['location_bn'].'</div></td>';
                            echo '<td><button class="deleterow" type="submit" name="delete" value="">DELETE</button></td>';
                            echo '</div></tr>';


                    $i++;   
                    }
                    ?>
                </table>
                </form>

http://localhost/portal/administrator2/admin/input_image.php?image_id=

I wonder why when I click the delete button, the variable after the ?delete appears image_id instead of ?image_id

Jesus, you've been asking questions since 2011 and you still don't understand basics.

moderators, feel free to delete this reply if you want, but this just makes my stomach ache

commented: Well we might think it but we're too polite to say it +15

This still appears strange to me, since I already put:

<form method="get" action="<?php echo $_SERVER['PHP_SELF'].'?image_id='.$image_id; ?>">

and still have not seen ?image_id='.$image_id; appears.

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.