/    Sign up×
Community /Pin to ProfileBookmark

delete row using check box

hey guys so I’m trying to delete a row from table which is connected to database using checkbox(and it wouldnt matter if its just one row or multiple rows) but it is not working, as in nothing happens. it doesn’t delete, no errors or warnings appear just a refresh.

page :

[CODE]
<form class=”buttons” method=”post” action=”event.php”>
<div class=”button-wrap”>
<input type=”button” id=”add_event” name=”add_event” value=”Add Event”/>
<input type=”submit” id=”del_event” name=”del_event” value=”Delete Event”/>
</div>
</form>

<form method=”post” action=”contact_data.php”>
<table class=”contact-list-table”>
<thead>
<tr>
<th >Salutation</th>
<th>Name</th>
<th>House Address</th>
<th>Email Address</th>
<th>Telephone No.</th>
<th>Office No.</th>
</tr>
</thead>
<tbody>
<?php
require “connection.php”;

$check = mysql_query(“SELECT * FROM contact”) or die(mysql_error());
$count=mysql_num_rows($check);
if($count > 0)
{
while($rows = mysql_fetch_array($check))
{
$salutation = $rows[‘salutation’];
$firstname = $rows[‘fname’];
$lastname = $rows[‘lname’];

echo
“<tr>
<td ><input type=’checkbox’ name=’check[]’ class=’check’ value=’$id’>$salutation</td>
<td>$firstname $lastname</td>
</tr>”;
}
}
else
{
echo
“<tr>
<td colspan=’6′>Contact Database is empty.</td>
</tr>”;
}
?>
</tbody>
<?php
if (isset($_POST[‘del_contact’]) && isset($_POST[‘check’]))
{
foreach($_POST[‘check’] as $del_id)
{
$del_id = (int)$del_id;
$sql = “DELETE FROM contact WHERE contact_id = $del_id”;
mysql_query($sql);
}
}
?>
</table>
</form>
[/CODE]

[CODE]if (isset($_POST[‘del_contact’])[/CODE]

‘del_event’ is a name of a delete button which is disabled unless a checkbox is checked.

any help is mush appreciated.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmApr 29.2014 — Your submit buttons are in one form and your checkboxes are in another. You can only submit ONE form at a time. Also - you are testing for an element named 'del_contact' but I don't see one in this code, nor do I see anything that is "disabled" or any code to enable anything.
Copy linkTweet thisAlerts:
@noobdvlprauthorApr 30.2014 — ah crap. sorry. i posted the wrong codes. crap is there a way to edit our post? i don't see one.

i'll post the right one here then.

[CODE]
<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
<input type="submit" id="edit_event" name="edit_event" value="Edit Event">
</div>

<table class="event-table">
<thead>
<tr>
<th>Event Name</th>
<th>Start Event</th>
<th>End Event</th>
<th>Venue</th>
</tr>
</thead>
<tbody class="tbody-event-list-table2">
<?php
require "connection.php";
$check = mysql_query("SELECT * FROM event") or die(mysql_error());
if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
$id = $row['event_id'];
$name = $row['event_name'];
$start = $row['start'];
$end = $row['end'];
$venue = $row['event_venue'];

echo
"<tr>
<td><input type='checkbox' name='check[]' class='check' value='$id'><a href='' class='event-link' value='$id' name='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
echo "</tr>";
}
}
else
{
echo
"<tr>
<td colspan='4'>There Are No Events.</td>
</tr>";
}
?>
</tbody>
<?php
if (isset($_POST['del_event']) && isset($_POST['check']))
{
foreach($_POST['check'] as $del_id)
{
$del_id = (int)$del_id;
$sql = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error());
}
}
?>
</table>
</form>
[/CODE]


the disable/enable code:
[CODE]
<script type="text/javascript">
var chkbox = $(".check"),
button = $("#del_event");
button.attr("disabled","disabled");
chkbox.change(function(){
if(this.checked){
button.removeAttr("disabled");
}else{
button.attr("disabled","disabled");
}
});
</script>
[/CODE]


so yeah, i removed the "form" that was separating the button and the table so that form is encased around both table and button. the row still does not delete though.
Copy linkTweet thisAlerts:
@ginerjmApr 30.2014 — I see an incomplete form and don't know that you are actually getting the input you expect. Try putting an echo of the id values you receive instead of doing the delete query and see what you get.
Copy linkTweet thisAlerts:
@ShrineDesignsApr 30.2014 — You need to merge the two forms into one.

web browsers only submit one form per transaction (unless you have javascript to tie-in and handle the multiple forms).
Copy linkTweet thisAlerts:
@noobdvlprauthorMay 05.2014 — okay i got it! i had to merge two forms into one[/QUOTE] and add

[CODE]
echo "<meta http-equiv="refresh" content="0;URL=event.php">";
[/CODE]


so it would refresh the page. thanks!
×

Success!

Help @noobdvlpr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...