www.webdeveloper.com
Results 1 to 6 of 6

Thread: delete row using check box

  1. #1
    Join Date
    Apr 2014
    Posts
    10

    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:
    if (isset($_POST['del_contact'])
    'del_event' is a name of a delete button which is disabled unless a checkbox is checked.

    any help is mush appreciated.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    657
    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.

  3. #3
    Join Date
    Apr 2014
    Posts
    10
    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>
    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>
    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.

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    657
    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.

  5. #5
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    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).

  6. #6
    Join Date
    Apr 2014
    Posts
    10
    okay i got it! i had
    to merge two forms into one
    and add

    Code:
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
    so it would refresh the page. thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles