Can somebody help me? i cant echo my checkbox group with a multiple selection.
Every time i echo the checkbox group the only things show is the last box that Ive check.
here's my code
<?php
$submit = $_POST['submit'];
$incharge = $_POST['incharge'];
if ($submit)
{
echo $incharge;
}
?>
<table width="500" height="69">
<tr>
<td width="73"><label>
<input type="checkbox" name="incharge" value="1" id="responsible_0" />
MNFA</label></td>
<td width="72"><label>
<input type="checkbox" name="incharge" value="2" id="responsible_1" />
HJB</label></td>
<td width="70"><label>
<input type="checkbox" name="incharge" value="3" id="responsible_2" />
JBG</label></td>
<td width="75"><label>
<input type="checkbox" name="incharge" value="4" id="responsible_3" />
MSG</label></td>
<td width="275"><label>
<input type="checkbox" name="incharge" value="5" id="responsible_4" />
MGR</label></td>
</tr>
<tr>
<td height="33"><label>
<input type="checkbox" name="incharge" value="6" id="responsible_5" />
AAP</label></td>
<td><label>
<input type="checkbox" name="incharge" value="7" id="responsible_6" />
EPM</label></td>
<td><label>
<input type="checkbox" name="incharge" value="8" id="responsible_7" />
SGA</label></td>
<td><label>
<input type="checkbox" name="incharge" value="9" id="responsible_8" />
JLL</label></td>
<td><label>
<input type="checkbox" name="incharge" value="10" id="responsible_9" />
AFM</label></td>
</tr>
</table>
Thank you in advance.. .
Change the name attribute to:
name="incharge[]"
That will produce an array, $incharge.
Note that you won't be able to just echo that value; you will need to "print_r" or loop through it.
You need to change the "name" attribute of your "input" elements to indicate that it's an array by adding square brackets [] at the end. $_POST['incharge'] will be an array instead of a string.
Example
<input type="checkbox" name="incharge[]" value="1" id="responsible_0" />
The reason only the last value is being sent is because all the checkboxes have the same name, thereby renaming them over and over. What you want is to assign all the checkboxes to an array, like so:
Change name="incharge" to name="incharge[]"
You'll then want to iterate it:
if ($submit)
{
// PHP throws a fit if we try to loop a non-array
if(is_array($incharge))
{
foreach($incharge as $val)
{
echo $val . '<br />';
}
}
}
if(isset($_post['calculations'])
{
$member = $_POST['member'];//get the total values in an array
if(is_array($member))// confirm $member is an array
{
foreach($member as $names)
{
echo $names ."<br/>";//take the values
}
}
<input type="checkbox" name="member[]" value="value1">
Related
I have an HTML form that I need to then reference to in PHP, so that I can eventually filter through data. Right now it is just echoing a bit of text for testing purposes.
I use the $_GET variable to get which values are equal to what, then use an if/else statement to tell me if each is checked.
If I say it only equals one value (== .25) it returns false.
However if I add one more or both more values (== .25 or .375 or .5) it will return the value I need.
How can I get it to return true with only one value?
<table stlye="width:100%">
<tr>
<td style="width:50%">
<form method="GET">
Tool Diameter: <br>
<input type="checkbox" name="Tool Diameter" value=.25 checked> .25<br>
<input type="checkbox" name="Tool Diameter" value=.375 checked> 3/8<br>
<input type="checkbox" name="Tool Diameter" value=.5 checked> 1/2<br><br>
Brand: <br>
<input type="checkbox" name="Brand" value="Lakeshore Carbide " checked> Lakeshore Carbide<br>
<input type="checkbox" name="Brand" value="AB Tools" checked> AB Tools<br>
<input type="checkbox" name="Brand" value="Helical Tools" checked> Helical Tools<br><br>
Flutes: <br>
<input type="checkbox" name="Flutes" value="2" checked> 2<br>
<input type="checkbox" name="Flutes" value="3" checked> 3<br>
<input type="checkbox" name="Flutes" value="4" checked> 4<br><br>
Tool Material: <br>
<input type="checkbox" name="Material" value="HSS" checked> HSS<br>
<input type="checkbox" name="Material" value="Carbide" checked> Carbide<br>
<input type="checkbox" name="Material" value="Cobalt" checked> Cobalt<br><br>
Coating: <br>
<input type="checkbox" name="Coating" value="Uncoated" checked> Uncoated<br>
<input type="checkbox" name="Coating" value="ZrN" checked> ZrN<br>
<input type="checkbox" name="Coating" value="TiCN" checked> TiCN<br><br>
Tool Type: <br>
<input type="checkbox" name="Type" value="Face Mill" checked> Face Mill<br>
<input type="checkbox" name="Type" value="Flat Endmill" checked> Flat Endmill<br>
<input type="checkbox" name="Type" value="Ball Endmill" checked> Ball Endmill<br>
<br><button>Filter</button><br>
</form>
</td>
<td style="width:50%">
<style type="text/css">
td
{
padding:0 50px 0 50px;
}
</style>
<?php
//while (true){
if ($_GET['Tool Diameter'] == .375) {
echo 'test = true';
}
else {
echo "false";
}
?>
</td>
</tr>
</table>
Convert the values to strings e.g.
<input type="checkbox" name="Tool Diameter" value=".375" checked> .375<br>
then check
if ($_GET['Tool Diameter'] == ".375"){
enter code here
}
try this
<input type="checkbox" name="Tool Diameter[]" value=".25" checked> .25<br>
<input type="checkbox" name="Tool Diameter[]" value=".375" checked> 3/8<br>
<input type="checkbox" name="Tool Diameter[]" value=".5" checked> 1/2<br><br>
.....
<?php
// for checking the condition 'atleast 1 ' should be checked
if(sizeof($_GET['Tool Diameter']) >=1){
echo 'test = true';
}
else {
echo "test = false";
}
?>
1st mistake
values have to be wrapped in parenthesise value=".25"
2nd mistake
names have to be unique, as a result you have only one value in the array $_GET['Tool Diameter']
you should have a slot in $_GET that contains i.e. array of your results, so lets say
$_GET['Dimensions'] = [
'Dimension 1' => '.25',
'Dimension 2' => '.375',
'Dimension 3' => '.5'
];
and then refer to each of them separately
lets say we have this:
echo '<form method="post">
<div class="form-group">
<table class="table table-bordered table-hover table-striped" style="width:auto">
<tr>
<td><label for="array">ARRAY_NAME</label></td>
<td>
<input type="checkbox" name="array[]" value="1" /> option1<br />
<input type="checkbox" name="array[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array2">ARRAY_NAME2</label></td>
<td>
<input type="checkbox" name="array2[]" value="1" /> option1<br />
<input type="checkbox" name="array2[]" value="2" /> option2
</td>
</tr>
<tr>
<td><label for="array3">ARRAY_NAME3</label></td>
<td>
<input type="checkbox" name="array3[]" value="1" /> option1<br />
<input type="checkbox" name="array3[]" value="2" /> option2
</td>
</tr>
</table>
</div>
<button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';
I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>
but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].
Can someone give me a solution because I'm stuck! Thanks in advance guys!!
You are trying to access the values incorrectly.
You can access a array posted like this:
echo $_POST['array1'][0];
php.net/$_POST See the first comment.
When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.
You need to access:
$_POST['array1'][0]
not
$_POST['array1[0]'] // This is incorrect
(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)
Just wondering the best way to set a default value for a checkbox. I want the value to be 0 when checked and 1 when unchecked.
I can't use " input type="hidden" name="chk_name[]" value="1" " because of the array. It doubles my array.
If I try to set it after using isset it doesn't put it into my array.
I have multiple arrays and want to match them so that all the array[0]'s get updated together and array[1]'s etc so a missing value messes it up.
So how can I put a 1 into the array for wherever it isn't checked?
Form:
<?php
require_once ("con.php");
$p=payment();
$result = mysqli_query($mysqli,$p);
while($res=mysqli_fetch_array($result))
{
?>
<tr>
<td class="paid">
<input type="hidden" value="1" name="paid[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="paid[<?php echo $res['name'];?>]"
<?php
if($res["paid"]==0)
{
echo "checked";
}
?>>
</td>
<td class="active">
<input type="hidden" value="1" name="active[<?php echo $res['name'];?>]">
<input type="checkbox" value="0" name="active[<?php echo $res['name'];?>]"
<?php
if($res["active"]==0)
{
echo "checked";
}
?> >
</td>
<input type="hidden" name="ID[<?php echo $res['name'];?>]" value="<?php echo $res['ID']; ?>">
</tr>
<?php } ?>
<tr>
<td>
<input type="submit" name="submit" value="Update">
</td>
</tr>
</table>
</form>
</body>
</html>
php:
$paid=$_POST['paid'];
$active=$_POST['active'];
foreach($_POST as $key=>$value)
{
$ID=$ID[$key];
$paid=$paid[$key];
$active=$active[$key];
$up=updatePayment($paid,$active,$ID);
$r = mysqli_query($mysqli,$up);
echo "Information stored successfully";
}
?>
my function:
function updatePayment($paid,$active,$ID)
{
$uc="UPDATE
`company`
SET
`paid`='$paid',
`active`='$active'
WHERE
`ID`='$ID'";
return $uc;
}
:Updated code:
I can see the arrays are coming out fine for everything now. The hidden method worked.
Thanks for everyone's help!
As soon the hidden field and the checkbox has the same name it will work no matter if the name is array or not.
The simple reason for this is that when is unchecked the checkbox value is not posted.
Try this code and you will see the result
<pre>
<?php
print_r($_POST);
?>
</pre>
<form method="POST">
<input type="hidden" name="fields[1]" value="1"/>
<input type="checkbox" name="fields[1]" value="0"/>
<input type="hidden" name="fields[2]" value="0"/>
<input type="checkbox" name="fields[2]" value="1"/>
<input type="hidden" name="fields[3][something][else]" value="3"/>
<input type="checkbox" name="fields[3][something][else]" value="3"/>
<button type="submit">Submit</button>
</form>
i am having problem in printing the values of the different checkbox with the same name & different values..
PHP
//$infect_type=array();
$infect_type = isset($_POST['infect_type']) ? $_POST['infect_type'] : null;
$values= implode(",",$infect_type);
print_r($values);
HTML
<input type="checkbox" name="infect_type" value="Blood Born" />
<input type="checkbox" name="infect_type" value="Air Born" />
i can only get the value which is selected last before submitting.
Use array notation for checkboxes names:
<input type="checkbox" name="infect_type[]" value="Blood Born" />
<input type="checkbox" name="infect_type[]" value="Air Born" />
In this case $_POST['infect_type'] is going to be an array of checked values.
HTML
<input type="checkbox" name="infect_type[]" value="Blood Born" />
<input type="checkbox" name="infect_type[]" value="Air Born" />
PHP
<?php
$infect_type = $_POST['infect_type'];
foreach ($infect_type as $i) {
echo $i;
//Change the code here
}
?>
This will do.
I have multiple checkboxes on my form:
<input type="checkbox" name="animal" value="Cat" />
<input type="checkbox" name="animal" value="Dog" />
<input type="checkbox" name="animal" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['submit']) {
echo $_POST['animal'];
}
I get "Bear", i.e. the last chosen checkbox value even though I picked all three. How to get all 3?
See the changes I have made in the name:
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
you have to set it up as array.
print_r($_POST['animal']);
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
If I check all three and hit submit, with the following code in the PHP script:
if(isset($_POST['animal'])){
foreach($_POST['animal'] as $animal){
echo $animal;
}
}
use square brackets following the field name
<input type="checkbox" name="animal[]" value="Cat" />
<input type="checkbox" name="animal[]" value="Dog" />
<input type="checkbox" name="animal[]" value="Bear" />
On the PHP side, you can treat it like any other array.