Jump to content


Photo

Parsing Input:checkbox


  • Please log in to reply
5 replies to this topic

#1 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 August 2006 - 10:54 PM

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:

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

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)?
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 August 2006 - 10:56 PM

We need to see the source for your form.

Ken

#3 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 21 August 2006 - 11:00 PM

Note: pseudo 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>

PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 21 August 2006 - 11:09 PM

But yes, only the checked checkbox info is posted

Check it out
<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 KittyKate

KittyKate
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 22 August 2006 - 05:50 PM

Finally got it working. Thanks Barand!
PHP Version 4.3.2
MySQL version unknown (greater or equal to 3.23)

#6 SharkBait

SharkBait
  • Members
  • PipPipPip
  • Advanced Member
  • 845 posts
  • LocationMetro Vancouver, BC

Posted 22 August 2006 - 10:32 PM

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.

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users