Jump to content

Archived

This topic is now archived and is closed to further replies.

KittyKate

Parsing Input:checkbox

Recommended Posts

I am feeling rather frustrated and braindead at this point... too many stupid HTML bugs in a day (if only I could at least say that they were because of me, but I'm suffering through another's code!).

I've got the front end working at this point, but here is the setup and please tell me the name/value/id I need to set to get this thing to work.

A pair of radio buttons trigger a javascript for one of two sets of checkboxes to be enabled.

I can figure out which radio button was set, but I'm having issues parsing the checkboxes. The basic idea of the code I'm using is:

[code]<?php
foreach($_POST['name'] as $value) {
    //add $value to database
}
?>[/code]

Is this remotely correct? And will only the the ones that are checked show up, or do I need to test for that (and if so how)?

Share this post


Link to post
Share on other sites
We need to see the source for your form.

Ken

Share this post


Link to post
Share on other sites
Note: pseudo code!!!!

[code]<form name='name1'>
  <input type='radio' onclick='javascript(a)' name='type' value='a' id='a' />A
  <input type='radio' onclick='javascript(b)' name='type' value='b' id='b' />B

  <input type='checkbox' name='a' id='a#' value='#' />String
  <input type='checkbox' name='a' id='a#' value='#' />String
...
  <input type='checkbox' name='b' id='b#' value='#' />String
  <input type='checkbox' name='b' id='b#' value='#' />String
...
</form>
[/code]

Share this post


Link to post
Share on other sites
But yes, only the checked checkbox info is posted

Check it out
[code]<?php
if (isset($_POST['submit'])) {
    echo '<pre>', print_r($_POST, true), '</pre><hr>';

?>
<FORM method='POST'>
<input type="checkbox" name="chkbox[1]" value="1"> 1 <BR/>
<input type="checkbox" name="chkbox[2]" value="2"> 2 <BR/>
<input type="checkbox" name="chkbox[3]" value="3"> 3 <BR/>
<input type="checkbox" name="chkbox[4]" value="4"> 4 <BR/>
<input type="checkbox" name="chkbox[5]" value="5"> 5 <BR/>
<input type="submit" name="submit" value="Submit">
</FORM>[/code]

Share this post


Link to post
Share on other sites
I never thought of using print_r with a true on it. I usually use if(isset($_POST['chkboz[]'])) because like Barand mentioned, they only get posted if they are checked.

:)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.