Jump to content

HELP needed :-(


spyer

Recommended Posts

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]
<?php
switch ($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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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  --- RESET

now, above you'll see the form.. where:

#         => CHECKBOX[unchecked]
#+       => CHECKBOX[checked]
___      => TEXTBOX FILED
SUBMIT => SUBMIT BUTTON
RESET   => RESET BUTTON

The 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 database

in 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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.