Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Add/Delete Multiple Items from One PHP Form

Codes here !

Moderators: macek, egami, gesf

Add/Delete Multiple Items from One PHP Form

Postby willinghamd » Tue May 01, 2012 4:59 am

Looking for a way to add or delete items from a single form. I have three tables: users, items, useritems. The items display in a form with checkboxes next to each one. When the user checks OR unchecks the items, I want the code to update the useritems table with the new selections.

Any suggestions, links you could provide or code snippets are greatly appreciated. I'm new to php, obviously.

Thanks in advance!
willinghamd
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue May 01, 2012 4:52 am

Re: Add/Delete Multiple Items from One PHP Form

Postby 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

Postby willinghamd » Tue May 01, 2012 9:01 am

Great...thanks for the help!!

Anyone have alternative ways of accomplishing the same thing without using checkboxes? Trying to make it as intuitive as possible. I'd like to possibly display an add button and delete button for each item. Any other options?

Thanks again!
willinghamd
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue May 01, 2012 4:52 am

Re: Add/Delete Multiple Items from One PHP Form

Postby 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.
User avatar
minimihi
New php-forum User
New php-forum User
 
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania


Return to mySQL & php coding

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.