Add/Delete Multiple Items from One PHP Form

Codes here !

Moderators: macek, egami, gesf

Post Reply
User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Add/Delete Multiple Items from One PHP Form

Post by minimihi » Tue May 01, 2012 5:38 am

You can realize it by biding onchange event handler to checkbox (with JavaScript).
When checkbox is changed, you generated information about a change. That information would be set to PHP file (using AJAX method).
Inside PHP file you would receive that information, use it to determine what action to take (add/delete), and with which item to perform it.

Here is example of AJAX using jQuery plugin:
===========================================
HTML:
-------------------------------------------------------

Code: Select all

<html>
<head>
   <title>AJAX</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(window).ready(function() {
	// add event
	$('.checkbox').change(function() {

		// get checkbox value
		var item_id = $(this).val();
		
		// get checkbox status (checked/not)
		if(this.checked == true)
			var to_do = "add";
		else
			var to_do = "delete";
			
		// perform AJAX
		$.ajax({
			// data transfer method
			type: "POST",
			// url to request
			url: "duh.php",
			// data to transfer
			data: { action: to_do, id: item_id },
			// what to do on successful connection
			success: function(html_came_back) {
				alert(html_came_back);
			}
		});
	});
});
</script>
</head>
<body>
<input type="checkbox" class="checkbox" name="vehicle" value="1" /> bike<br />
</body>
</html>
===========================================
duh.php
-------------------------------------------------------

Code: Select all

<?php
// receive transfered data
$action = $_POST['action'];
$id= $_POST['id'];

// check what to do
switch($action) {
    case 'add':
        // your script to add item
        echo "add this ".$id;
        break;
    case 'delete':
        // your script to delete item
        echo "delete this ".$id;
        break;
}
?>

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: Add/Delete Multiple Items from One PHP Form

Post by minimihi » Tue May 01, 2012 10:09 am

Now you're talking about creating user interface.

You could created a div block which would act as a button.
Button could look like ON | OFF switch.
When adding ADD would turn green (DELETE would stay grey), on deleting DELETE would turn red (ADD would stay grey). With a little more effect that would look pretty cool switch :D

Any way you would choose, using AJAX method to submit data is best way.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests