Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    submitting values from multiple checkboxes (and forms)

    Hello,

    I'm struggling to work out a way to do this.

    Essentially I have a list of rows that I'm outputting from my database in a table. Each row will have a checkbox. I'd like to be able to check multiple checkboxes and then to click on a link like 'delete selected' and for every row with a checkbox to be deleted.

    As each checkbox is in a new table row (because they are attached to that row via a unique ID) I can't use one form. So I assume I need some kind of javascript which will submit lots of forms when a link is clicked?

    I'd then need a way to go through each submitted form and check to see if the checkbox was ticked.

    To be honest I'm not sure how to go about doing this. Any ideas? I'm not sure if there's a simple way of doing something which looks VERY complicated to me!

    Thanks,

    Nicky

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by nickyfraggle View Post
    As each checkbox is in a new table row (because they are attached to that row via a unique ID) I can't use one form. So I assume I need some kind of javascript which will submit lots of forms when a link is clicked?
    I'm completely lost by what you mean (it helps to show us the relevant code).

    Most folks just have an array of checkboxes like this:
    <form>
    <input type="checkbox" name="item_id[]" value="1">
    <input type="checkbox" name="item_id[]" value="2">
    <input type="checkbox" name="item_id[]" value="3">
    <input type="checkbox" name="item_id[]" value="4">
    <input type="submit" value="Submit">
    </form>

    You see the [] at the name of each box? - That puts them into an array. You therefore do not need to give each box a unique name so that is one complication out of the way.

    Now, when you check a box it is sent. If it is not checked, the browser does not send it. For this example lets suppose that the 2nd and 4th boxes are checked.

    In php, you iterate through a loop of the items:
    PHP Code:
    foreach ($_POST['item_id'] as $Key => $Value)
       {
       print 
    $Value .', '// 2, 4,
       

    Thats it. Simple. It doesn't matter that each checkbox is inside a seperate table row as long as they're inside the same form tags.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    wlf
    wlf is offline
    New Coder
    Join Date
    Aug 2012
    Posts
    53
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by nickyfraggle View Post
    I'd like to be able to check multiple checkboxes and then to click on a link like 'delete selected' and for every row with a checkbox to be deleted.
    I believe you mean "for every checked row to be deleted", correct?

    Quote Originally Posted by nickyfraggle View Post
    As each checkbox is in a new table row (because they are attached to that row via a unique ID) I can't use one form
    I don't understand this. You have a table for each row? That sounds terrible.

    Can you show us your code?

    That would make things easier

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Hi guys,

    Thanks to both of you for your help.

    I didn't know about the array thing so that's great.

    My code runs something like this.

    Code:
    <table>
      <tr>
        <th>Date/th>
        <th>Update</th>
        <th>Delete?</th>
      </tr>
    
      <!-- REPEATED TR FOR EACH ENTRY -->
      <tr>
        <td>10/05/2013/td>
        <td>
          <form name="formBasic" method="post" action="update.php">
          <div>
            <label>Item Name</label>
            <textarea name="item_name" rows="20" cols="20">
             Name of item in here</textarea>
          </div>
          <div>
            <input name="id" type="hidden" value="12"/>
            <input name="go" type="submit" class="btn" value="Go update it!" />
          </div>
          </form>
        </td>
        <td>
          <form name="formBasic2" method="post" action="delete.php">
          <div>
            <!-- CHECKBOX VALUE IS UNIQUE ID OF THE ROW TO DELETE -->
            <input name="delete" type="checkbox" value="12" />
          </div>
          </form>
        </td>
      </tr>
    </table>
    <p><a href="#">Delete all selected rows</a></p>
    So someone would go through and select the checkboxes they want, click on the link to 'delete all selected' and some php code would use the unique ID passed by the checked checkboxes to delete the selected items.

    I believe you mean "for every checked row to be deleted", correct?
    Yes that's it!

    I hope that makes some sense! As you can see from my code I can't put all the checkboxes in one form (it would be easy to do if I could!) because within that TR there is also another form to update the item.

    I think what I need is some kind of 'when you click this link submit all forms with a given ID/class/name'?

    Hope this answers your questions and thanks again for your help.
    Nicky

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by nickyfraggle View Post
    As you can see from my code I can't put all the checkboxes in one form (it would be easy to do if I could!) because within that TR there is also another form to update the item.
    Yes you can use one form and you are over complicating this.

    Use one form. Have your text boxes and individual buttons. You can have many submit buttons in one form - they will all do the same thing - submit.

    As I have said previously, use one form with an array for your delete action - no big major problem is it?

    As for your updates, you could use a javascript there to submit an ajax request or submit form data in a hidden form that will redirect the page.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Hello,

    Thanks for your response. I'm not 100% sure that I understand but I guess I'll have to give it a try and see what happens.

    Nicola


  •  

    Posting Permissions

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