I have some multi-value inputs and there are some checkboxes among them. When I submit the form, if a checkbox is not checked, it shifts up the rest of the data and misplaces inputs in other rows.
For example, if those two checkboxes are not checked in the middle, when I use a foreach loop, it uses the fourth hazardous input in the second row and data is misplaced in database.
Here is my code:
<td>
<input type="text" name="description_of_goods_fa[]"
class="form-control">
</td>
<td>
<input type="text" name="hs_code[]" required
title="HS Code needs to be a 8 digit number"
class="form-control" pattern="[0-9]{8}">
</td>
<td>
<input type="checkbox" name="stackable[]" class="form-control"
value="1" checked>
</td>
<td>
<input type="checkbox" name="hazardous[]" class="form-control"
onchange="check_hazardous(this);" value="1">
</td>
<td>
<input type="number" name="un[]" min="0" max="9999" step="1"
class="form-control" readonly>
</td>
<td>
<input type="number" name="imco_class[]" min="1" max="9.9"
step="0.1" class="form-control" readonly>
</td>
And I use a foreach loop to go through $_POST in php:
foreach($_POST['hs_code'] as $key => $hs_code)
{
$description_of_goods_en = $mysqli->real_escape_string($_POST['description_of_goods_en'][$key]);
$description_of_goods_fa = $mysqli->real_escape_string($_POST['description_of_goods_fa'][$key]);
$hs_code = $mysqli->real_escape_string($_POST['hs_code'][$key]);
$hs_code = $mysqli->real_escape_string($_POST['hs_code'][$key]);
$stackable = (isset($_POST['stackable']) ? '1' : '0');
$hazardous = (isset($_POST['hazardous'][$key]) ? '1' : '0');
$un = (isset($_POST['un'][$key]) ? $mysqli->real_escape_string($_POST['un'][$key]) : "");
$imco_class = (isset($_POST['imco_class'][$key]) ? $mysqli->real_escape_string($_POST['imco_class'][$key]) : "");
$no_of_unit = $mysqli->real_escape_string($_POST['no_of_unit'][$key]);
$unit_price = $mysqli->real_escape_string($_POST['unit_price'][$key]);
$total_price = $mysqli->real_escape_string($_POST['total_price'][$key]);
$no_of_packages = $mysqli->real_escape_string($_POST['no_of_packages'][$key]);
$kind_of_packages = $mysqli->real_escape_string($_POST['kind_of_packages'][$key]);
$pkg_length = $mysqli->real_escape_string($_POST['pkg_length'][$key]);
$pkg_width = $mysqli->real_escape_string($_POST['pkg_width'][$key]);
$pkg_height = $mysqli->real_escape_string($_POST['pkg_height'][$key]);
$pkg_volume = $mysqli->real_escape_string($_POST['pkg_volume'][$key]);
$total_volume = $mysqli->real_escape_string($_POST['total_volume'][$key]);
$pkg_net_weight = $mysqli->real_escape_string($_POST['pkg_net_weight'][$key]);
$pkg_gross_weight = $mysqli->real_escape_string($_POST['pkg_gross_weight'][$key]);
$total_net_weight = $mysqli->real_escape_string($_POST['total_net_weight'][$key]);
$total_gross_weight = $mysqli->real_escape_string($_POST['total_gross_weight'][$key]);
$chargeable_weight = (isset($_POST['chargeable_weight'][$key]) ? $mysqli->real_escape_string($_POST['chargeable_weight'][$key]) : "");
if($hs_code != '' || $no_of_packages != '' || $description_of_goods_en != '' || $volume != '')
{
$sql = "
INSERT INTO `inquery_cargo` SET
`id_inquery`='$id_inquiry',
`description_of_goods_en`='$description_of_goods_en',
`description_of_goods_fa`='$description_of_goods_fa',
`hs_code`='$hs_code',
`stackable`='$stackable',
`hazardous`='$hazardous',
`un`='$un',
`imco_class`='$imco_class',
`no_of_unit`='$no_of_unit',
`unit_price`='$unit_price',
`total_price` = '$total_price',
`no_of_packages`='$no_of_packages',
`kind_of_packages`='$kind_of_packages',
`pkg_length`='$pkg_length',
`pkg_width`='$pkg_width',
`pkg_height`='$pkg_height',
`pkg_volume`='$pkg_volume',
`total_volume`='$total_volume',
`pkg_net_weight`='$pkg_net_weight',
`pkg_gross_weight`='$pkg_gross_weight',
`total_net_weight`='$total_net_weight',
`total_gross_weight`='$total_gross_weight',
`chargeable_weight`='$chargeable_weight'
";
$res = $mysqli->query($sql);
if(!$res)
{
$text = "An error occurred during the process.";
$error = true;
}
}
}
How can I submit those checkboxes that are not checked?
There is at least two solutions
1) You should pass an index of row to form field name for each row. For example
<tr>
...
<input type="text" name="description_of_goods_fa[1]"
class="form-control">
...
</tr>
<tr>
...
<input type="text" name="description_of_goods_fa[2]"
class="form-control">
...
</tr>
So in the PHP code you can check in the loop for the existence of index as
$hazardous = isset($_POST['hazardous'][$index]) ? $_POST['hazardous'][$index] : null;
2) You can place a hidden input with name hazardous in addition to checkbox
<input type="checkbox" class="form-control hazardous-checkbox"
onclick="changeHazardousInput(this)" value="1">
<input type="hidden" name="hazardous[]"
onchange="check_hazardous(this);">
So then you just need to write a 'changeHazardousInput' function that changes sibling hidden input's value
Example of the changeHazardousInput function:
function changeHazardousInput(checkbox) {
checkbox.nextElementSibling.value = checkbox.checked ? checkbox.value : '';
}
There is a good way to solve your task and also to improve code readability. Just change the HTML structure to root both checkboxes under same row number. You still not receive unticked checkboxes, but since the data is grouped under line number it isn't a problem anymore.
<tr>
<td>
<input type="text" name="line[1][description_of_goods_fa]" class="form-control">
</td>
<td>
<input type="text" name="line[1][hs_code]">
</td>
<td>
<input type="checkbox" name="line[1][stackable]" value="1" checked>
</td>
<td>
<input type="checkbox" name="line[1][hazardous]" onchange="check_hazardous(this);" value="1">
</td>
</tr>
<tr>
<td>
<input type="text" name="line[2][description_of_goods_fa]" class="form-control">
</td>
<td>
<input type="text" name="line[2][hs_code]">
</td>
<td>
<input type="checkbox" name="line[2][stackable]" value="1" checked>
</td>
<td>
<input type="checkbox" name="line[2][hazardous]" onchange="check_hazardous(this);" value="1">
</td>
</tr>
Please see below code for html. for loop value of i will be dynamic or your definedd
<?php for($i=0;$i<5;$i++):?>
<tr>
<td>
<input type="checkbox" name="stackable[]" class="form-control"
value="1" checked>
</td>
<td>
<input type="checkbox" name="hazardous[]" class="form-control"
value="1">
</td>
</tr>
<?php endfor;?>
<input type="hidden" name="row" value="<?php echo $i?>">
and see below code for php file where you submit data
$row=$_POST['row'];
$stack=$_POST["stackable"];
$hazar=$_POST["hazardous"];
for($i=0;$i<$row;$i++){
$stackable=(isset($stack[$i]))?1:0;
$hazardous=(isset($hazar[$i]))?1:0;
}
Its will be help full for you.
Related
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)
I have created the checkbox as an array. Now I want to separate the checkbox values based on the user selection of checkbox and then I want to show the form.
After using foreach function I got the value like this class1,class6,class8.
If it is class1 I have show the form fields.
If it is class6 I have to show the form fields.
How to separate the checkbox and match the with if condition.
My form:
<form name="frm" method="post">
<table align="center">
<tr>
<td>Name</td>
<td><input type="text" name="txt" id="txt" required ></td><td></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" id="pass" required></td><td></td>
</tr>
<tr>
<td>Product</td>
<td>
<input type="checkbox" name="chk[]" value="class1">class1
<input type="checkbox" name="chk[]" value="class6">class6
<input type="checkbox" name="chk[]" value="class8">class8
<input type="checkbox" name="chk[]" value="class10">class10
<input type="checkbox" name="chk[]" value="class12">class12
<input type="checkbox" name="chk[]" value="engineering">engineering
<input type="checkbox" name="chk[]" value="technology">technology
</td><td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Save"></td><td></td>
</tr>
</table>
My PHP values:
<?php
if (isset($_POST['submit']))
{
$name = $_POST['txt'];
$pass = $_POST['pass'];
$product = $_POST['chk'];
//print_r($product);
foreach ($product as $result => $v)
{
echo "The item has" . $v . "<br/>";
}
}
?>
if($v=="class1" || $v=="class6" || $v=="class8" || $v=="class10")
{
?>
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
<?php
}
?>
<?php
if($v=="class10" || $v=="class12" )
{
?>
<p>class12 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="phyiscs">phyiscs
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
<?php
}
?>
Here is my class1 form
<p>class1 subject form</p>
<form name="f1" action="" method="post">
<input type="checkbox" name="chk[]" value="allsubject">allsubject
<input type="checkbox" name="chk[]" value="science">science
<input type="checkbox" name="chk[]" value="maths">maths
</form>
</p>
I hope every one understand my question
It seems you are checking the value of $v outside the foreach loop, which does not make sense -- $v is just one of the possibly many values in the array, so you can't use it to draw conclusions safely.
You could test with in_array instead:
if (in_array("class1", $product) || in_array("class6", $product) || ... ) {
// show some elements
}
Of course that gets cumbersome if you want to check for a lot of possibilities. In that case you could use array_intersect and introduce some variables for readability:
$class1SubjectFormValues = ['class1', 'class6', 'class8', ... ];
$class12SubjectFormValues = ['class10', 'class12', 'class8', ... ];
if (array_intersect($product, $class1SubjectFormValues)) {
// class1 subject form
}
if (array_intersect($product, $class12SubjectFormValues)) {
// class12 subject form
}
I have made this code and I can't get the $txt2 to be inserted into mysql!
BUT the $tele2 is inserted, also when having multi rows from the dynamic form!.
But the $txt2 will not be inserted into DB.
Can someone see what the problem is. I think that it is some place in the foreach
This is the action post page!!
<?php if(isset($_POST)==true && empty($_POST)==false):
$day = $_POST['day'];
$month = $_POST['month'];
$text=$_POST['txt'];
$tele=$_POST['tele'];
include("db.php");
foreach($tele as $a => $b){
$a+1;
$txt2 = $_POST['text'][$a];
$tele2 = $_POST['tele'][$a];
$sql_ins="INSERT INTO SmsSend (`PHONENUM`, `TEXT`) VALUES
('".$tele2."','".$txt2."')";
$res_ins1=mysql_query($sql_ins);
}
?>
Sending my info from this dynamic form:
<fieldset class="row2">
<legend>Besked og bruger info</legend>
<p>
<input type="button" value="Tilføj bruger" onClick="addRow('dataTable')" />
<input type="button" value="Fjern bruger" onClick="deleteRow('dataTable')" />
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<td><input type="checkbox" required="required" name="chk[]" checked="checked" /></td>
<td>
<label for="txt">text</label>
<input type="text" required="required" maxlength="160" name="txt[]">
</td>
<td>
<label for="tele">TELE</label>
<input type="text" required="required" maxlength="8" name="tele[]">
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
You have $_POST['text'][$a] in your action
but in html markup you have
<input type="text" required="required" maxlength="160" name="txt[]">
So you need either change name of html input to text or use $_POST['txt'][$a]
Other thing is that your code is vulnerable to SQL Injection. You should use prepared statements.
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">
The issue is I have a page with the following checboxes listed for a particular question.When I select one of the boxes and go to the next page and comeback then i find,none of the checkboxes appear to be checked.I have checked it on the back end and i was able to see that the checkboxes were indeed checked,but i was not able to view them as checked.I am not able to figure out as to why they dont appear to be checked.Any help regarding this would be appreciated.Thanks in Advance. The following is the code which i have in that page.
<td>
<input type="checkbox" name="test_na" value="N/A" <?=$test_na?> id="test_na">
<label for="test_na">NA</label>
</td>
<td>
<input type="checkbox" name="test_y" value="Y" <?=$test_y?> id="test_y">
<label for="test_y">Yes</label>
</td>
<td>
<input type="checkbox" name="test_n" value="N" <?=$test_n?> id="test_n">
<label for="test_n">No</label>
</td>
Test the value of the checkoxes and echo checked if value matches.
<td>
<input type="checkbox" name="test_na" value="N/A" <?php echo (isset($test_na) && $test_na == 'N/A' ? 'checked' : ''); ?> id="test_na">
<label for="test_na">NA</label>
</td>
<td>
<input type="checkbox" name="test_y" value="Y" <?php echo (isset($test_y) && $test_y == 'Y' ? 'checked' : ''); ?> id="test_y">
<label for="test_y">Yes</label>
</td>
<td>
<input type="checkbox" name="test_n" value="N" <?php echo (isset($test_n) && $test_n == 'N' ? 'checked' : ''); ?> id="test_n">
<label for="test_n">No</label>
</td>
See page source. What in your variables? It should be checked="checked" or checked="yes" or checked="1"