spyer Posted November 24, 2006 Share Posted November 24, 2006 Good day,i have a question that maybe the silliest ever, so don't yall laugh now, ok?well, i need to know how to insert or at least get whether the checkbox is checked or not.Example:[code]<?phpswitch ($ACT){case "";echo"<form method='POST' action='$PHPSELF?ACT=VALUE'><input name='pri' type='checkbox' value='ON'> <p><input type='submit' value='Submit' name='B1'><input type='reset' value='Reset' name='B2'></p></form>";break;case "VALUE";if ($pri == "Checked"){echo "Checked";}else{ echo "Not"; }break;}?>[/code]I know this example is wrong in so many ways, but please help me, i need it so bad.thank you all Quote Link to comment Share on other sites More sharing options...
taith Posted November 24, 2006 Share Posted November 24, 2006 not so funny... i use this all the time...[code]<form method='POST' action='$PHPSELF?ACT=VALUE'><input name='pri' type='checkbox' value='ON'> <p><input type='submit' value='Submit' name='B1'><input type='reset' value='Reset' name='B2'></p></form>if($_POST[pri]==on) echo 'on';else echo 'off';[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 Your processing script will only recieved the values of checkboxes that are checked, so you can do something like this.[code]<?phpif (isset($_POST['pri'])) echo 'on';else echo 'off';?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
spyer Posted November 24, 2006 Author Share Posted November 24, 2006 thank you guys,it's so helpful,,i tried this way and it worked too :D[code]switch ($ACT){case "";echo"<form method='POST' action='$PHPSELF?ACT=VALUE'><input name='pri' type='checkbox' value='ON'><input name='pri2' type='checkbox' value='ON'><br /><input name='pri3' type='checkbox' value='ON'><br /> <p><input type='submit' value='Submit' name='B1'><input type='reset' value='Reset' name='B2'></p></form>";break;case "VALUE";if (isset($_POST['pri'])) {echo "CHECKED<br>";}else{ echo "NOT<br>"; } if (isset($_POST['pri2'])) {echo "CHECKED2<br>";}else{ echo "NOT2<br>"; } if (isset($_POST['pri3'])) {echo "CHECKED3<br>";}else{ echo "NOT3<br>"; }break;}[/code]but is it possible to get whether a number of checkboxes are checked or not,,i'll explaine..i have a form with multipul checkboxes like this# DATA1 Equipment# _________ TYPE __________ DATE __________#+ DATA2 Equipment# _________ TYPE __________ DATE __________#+ DATA3 Equipment# _________ TYPE __________ DATE __________SUBMIT --- RESETnow, above you'll see the form.. where:# => CHECKBOX[unchecked]#+ => CHECKBOX[checked]___ => TEXTBOX FILEDSUBMIT => SUBMIT BUTTONRESET => RESET BUTTONThe default for all checkboxes is CHECKED, and if it's checked it won't be inserted to the database.so i have to uncheck the fields that i want to insert to the database.if DATA1 is unchecked and DATA2 AND 3 are checked, DATA1 will be added but DATA2,3 won't be added to the databasein my example above you'll see that it will work that way, but it will be too long, is there any way to use it with arrays or something like that..i hope i made my self clear.. thank you again Quote Link to comment Share on other sites More sharing options...
spyer Posted November 25, 2006 Author Share Posted November 25, 2006 help :-( Quote Link to comment Share on other sites More sharing options...
.josh Posted November 25, 2006 Share Posted November 25, 2006 okay let's say you have a form with 5 checkboxes. If you want to know which check boxes were checked, make the checkbox names an array like blah[] but explicitly specify which element to use for each checkbox. example:[code]<form action = 'somewhere.php' method = 'post'> <input type = 'checkbox' name = 'blah[1]' value = 'value1'> something </br> <input type = 'checkbox' name = 'blah[2]' value = 'value1'> something </br> <input type = 'checkbox' name = 'blah[3]' value = 'value1'> something </br> <input type = 'checkbox' name = 'blah[4]' value = 'value1'> something </br> <input type = 'checkbox' name = 'blah[5]' value = 'value1'> something </br> <input type = 'submit' value = 'submit'></form>[/code]You can even make a loop to generate $x amount of checkboxes with values; just add the loop counter inside the [] brackets so that it specifies an array element for each one. If you do it this way, if you for example only check box 4 and 5, you will get the following posted:$_POST['blah'][4] and $_POST['blah'][5]now you know which ones [i]were[/i] checked, by virtue of what array positions exist. ['blah'][1], ['blah'][2], and ['blah'][3] do not exist, but you know that the 4th and 5th check boxes were checked, because ['blah'][4] and ['blah'][5] do exist. Here is a little example script where you can see this in action to hopefully understand better:[code]<?php$someNumber = 6;if ($_POST['blah']) { foreach ($_POST['blah'] as $key => $val) { echo "checkbox # <b>$key</b> with value <b>$val</b> was checked.</br>"; }}echo "<form action = '{$_SERVER['PHP_SELF']}' method = 'post'>";for($x = 1; $x < $someNumber; $x++) { echo "<input type = 'checkbox' name = 'blah[$x]' value = 'value$x'> value$x </br>";}echo "<input type = 'submit' value = 'submit'>";?>[/code] Quote Link to comment Share on other sites More sharing options...
taith Posted November 25, 2006 Share Posted November 25, 2006 [code]<?switch ($ACT){ case ""; echo"<form method='POST' action='$PHPSELF?ACT=VALUE'><input name='pri1' type='checkbox' value='ON'><input name='pri2' type='checkbox' value='ON'><br /><input name='pri3' type='checkbox' value='ON'><br /><p><input type='submit' value='Submit' name='B1'><input type='reset' value='Reset' name='B2'></p></form>"; break; case "VALUE"; $checkboxes=3; for($i=0;$i<=$checkboxes;$i++) if(isset($_POST[pri.$i])) echo "$i is CHECKED<br>"; break;}?>[/code] Quote Link to comment Share on other sites More sharing options...
spyer Posted November 25, 2006 Author Share Posted November 25, 2006 Thank you guys,,, you were so helpful,,,it worked smoothly... :DTHANKS A LOT..CHEERS Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.