Results 1 to 6 of 6

Thread: Checkboxes in HTML

  1. #1
    The King of Vague Steve B's Avatar
    Join Date
    Oct 2005
    Location
    Glasgow
    Posts
    5,051
    Thanks
    116
    Thanked
    67 times in 63 posts

    Checkboxes in HTML

    hey folks,
    any help would be appreciated on this, as its been a persistently annoying problem.
    i've got a HTML table set up based on an array, with a checkbox at each end.
    its based on a form, which, when its submitted, will check the rows for checkboxes, and delete the according rows from the table.

    trouble is, $_POST['...'] only returns on for checkboxes, not off. So it cant work out which row in the table the values have to be deleted from, and deletes the first number of rows, depending on how many boxes are checked.

    Anyone know a way around this problem?

    TIA,
    Steve

  2. #2
    Does he need a reason? Funkstar's Avatar
    Join Date
    Aug 2005
    Location
    Aberdeen
    Posts
    19,874
    Thanks
    630
    Thanked
    965 times in 816 posts
    • Funkstar's system
      • Motherboard:
      • Gigabyte EG45M-DS2H
      • CPU:
      • Intel Core2Quad Q9550 (2.83GHz)
      • Memory:
      • 8GB OCZ PC2-6400C5 800MHz Quad Channel
      • Storage:
      • 650GB Western Digital Caviar Blue
      • Graphics card(s):
      • 512MB ATI Radeon HD4550
      • PSU:
      • Antec 350W 80+ Efficient PSU
      • Case:
      • Antec NSK1480 Slim Mini Desktop Case
      • Operating System:
      • Vista Ultimate 64bit
      • Monitor(s):
      • Dell 2407 + 2408 monitors
      • Internet:
      • Zen 8mb
    How about having a seperate form item for each row?

    <input type="checkbox" name="row_1" value="delete" />
    <input type="checkbox" name="row_2" value="delete" />
    <input type="checkbox" name="row_3" value="delete" />
    etc.

  3. Received thanks from:

    Steve B (23-06-2007)

  4. #3
    The King of Vague Steve B's Avatar
    Join Date
    Oct 2005
    Location
    Glasgow
    Posts
    5,051
    Thanks
    116
    Thanked
    67 times in 63 posts
    Quote Originally Posted by Funkstar View Post
    How about having a seperate form item for each row?

    <input type="checkbox" name="row_1" value="delete" />
    <input type="checkbox" name="row_2" value="delete" />
    <input type="checkbox" name="row_3" value="delete" />
    etc.
    thats what im trying to get to work now, by appending the row of the cable onto each value (it'd be the other way about wouldn't it, with all of the names being delete.) then checking if the value has been selected. I'm planning to revolutionise the form so i can have trash and edit too

  5. #4
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)
    Let me get this straight...

    You have X amount of rows outputted to the screen, each with its own checkbox appended to it. When a checkbox is checked and submitted you want all corresponding checked rows to be deleted? If that is the case then you can use what I use:

    Your outputted rows should look something like that following:

    Code:
    <input type="checkbox" name="chbox[]" value="$Id" id="chbox[]" />
    <input type="checkbox" name="chbox[]" value="$Id" id="chbox[]" />
    <input type="checkbox" name="chbox[]" value="$Id" id="chbox[]" /> etc..
    $Id is the unique value of the checkbox, obtained from the rows unique ID in the database. I'm not sure if you need the id="" section - its been a while since I have had to fiddle with PHP. I have left it there in case you do.


    Then to delete the actual rows use this:
    Code:
    if(isset($_POST['submitDeleteButton']))
    {
      foreach($_POST['chbox'] as $key =>$val) 
      {
    	mysql_query("DELETE FROM table_name WHERE id='$val'");
      } 
    
    }
    Its operation is simple: foreach name="chbox" submitted assign it an array id and store the id in $key then correspond each id held in $key with the value="" part of the checkbox and stick that value into $val. Thats more or less how it works, at least that how I understand it to work; I'm not saying I'm right however . if you change chbox in the foreach statement, make sure you change the name="" section of the input control. You can also change $val to whatever you like, but I believe $key has to stay as $key.


    PS - Don't forget to sanitize any user data going in/out of the database when using something like $_POST or $_GET.
    Last edited by Dorza; 23-06-2007 at 04:53 PM.

  6. Received thanks from:

    Steve B (23-06-2007)

  7. #5
    The King of Vague Steve B's Avatar
    Join Date
    Oct 2005
    Location
    Glasgow
    Posts
    5,051
    Thanks
    116
    Thanked
    67 times in 63 posts
    cheers for that Dorza. Thing is though, theres no sql database, the table is generated from an XML file. that foreach loop has given me some inspiration though

  8. #6
    Registered+
    Join Date
    Jul 2005
    Location
    Richmond, London
    Posts
    75
    Thanks
    0
    Thanked
    0 times in 0 posts
    Just to clarify what Dorza says above; be [i]very[i] careful to check the input sent to $_POST, as the above example is ripe for exploitation by some form of SQL injection attack. For instance, imagine that somebody created their own HTML that looked like this:

    Code:
    <input type="checkbox" name="chbox[]" value="3\";drop all from *;" id="chbox[]" />
    That'd be bad, right?

    Also, it's incorrect to set an id attribute value of chbox[] - IDs in HTML are meant to be unique. Only the name attribute is necessary for the posting values, the ID is more for document-scope stuff (like assigning a label element to each checkbox).

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. The HTML bit of PHP - simple!
    By Vini in forum Software
    Replies: 14
    Last Post: 26-09-2005, 09:26 AM
  2. HTML gubbins stripper?
    By Mike Fishcake in forum Software
    Replies: 14
    Last Post: 04-07-2005, 05:14 PM
  3. Smart FTP Q? + html tutor site?
    By Jeanne d'Arc in forum Software
    Replies: 2
    Last Post: 04-12-2004, 12:36 AM
  4. Moving on from HTML...
    By TomWilko in forum Software
    Replies: 21
    Last Post: 23-10-2003, 10:17 PM
  5. Replies: 1
    Last Post: 14-08-2003, 03:32 PM

Posting Permissions

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