Get All Data PHP-HTML (Including all Checkbox) - php

I have array of HTML data
<tr><input type="checkbox" name="checkbox[]" value="0"> <input type="text" name="char[]" value="A"></tr>
<tr><input type="checkbox" name="checkbox[]" value="1"> <input type="text" name="char[]" value="B"></tr>
<tr><input type="checkbox" name="checkbox[]" value="2"> <input type="text" name="char[]" value="C"></tr>
How can I get all of of item in PHP Post? I need to get all data and check if the checkbox were check or not.
Like so:
<?php
foreach($_POST["char"] as $data) {
//If Checkboxes Checked:
// DO INSERT CHECKED ROW
//Else If Not Checked:
// ECHO VALUE NOT CHECKED ROW
}
?>
If I'm trying, the data were different from what I has check or only the checked value that sent.
Thank you.

Use indexes in your field names:
<form method="POST">
<table>
<tr>
<td><input type="checkbox" name="checkbox[0]" value="0"></td>
<td><input type="text" name="char[0]" value="A"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox[1]" value="0"></td>
<td><input type="text" name="char[1]" value="B"></td>
</tr>
<tr>
<td><input type="checkbox" name="checkbox[2]" value="0"></td>
<td><input type="text" name="char[2]" value="C"></td>
</tr>
</table>
<input type="submit" name="submit">
</form>
Then you can use a loop counter:
for ($i = 0; $i<count($_POST['char'];$i++) {
if (isset($_POST['checkbox'][$i]) {
// Do something if checkbox is set using $_POST['char'][$i]
} else {
// Do something if checkbox is not set
}
}

Related

How to fix form data in array that is not submitting properly

I'm building an admin form that allows other teams to update aspects of their own data so they don't have to call me to do it for them.
They update the data via checkboxes or text fields. There is also a requirement that they be able to edit multiple rows at once. So the form required them to select which rows they will be updating or have updated. so far so good.
My problem is thus: If the first record has one checkbox selected and the second record has 3 boxes checked, when the form is submitted and the data echoed back out, the first record shows 3 values and the second record shows 1 indicating that record 1 has somehow taken the values from record 2 for itself. I know that's not the case but can't figure out why that data is not staying with record 2.
I've beat around this and searched and found numerous examples that should work but they are just using a single text field and not checkboxes. So, I'm hoping someone here can help me sort it out.
<?php
if(isset($_POST['updateRecord'])) {
// echo "Update Record Outer Check<br>";
if ((strtolower($_POST['updateRecord']) == "update")) {
foreach ($_POST['update'] as $key => $update) {
$id = $_POST['id'][$key];
$regOK = (
isset($_POST['regOK'][$key]) ?
$_POST['regOK'][$key] : false
);
$paidOK = (
isset($_POST['paidOK'][$key]) ?
$_POST['paidOK'][$key] : false
);
$ebdOK = (
isset($_POST['ebdOK'][$key]) ?
$_POST['ebdOK'][$key] : false
);
$appOK = (
isset($_POST['appOK'][$key]) ?
$_POST['appOK'][$key] : false
);
echo "RECORD ID: ".$id."<br>";
echo " REG: ".$regOK."<br>";
echo "PAID: ".$paidOK."<br>";
echo " EBD: ".$ebdOK."<br>";
echo " APP: ".$appOK."<br><br>";
}
}
}
echo "<br><br>";
?>
<style type="text/css">
th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
<table cellpadding="2" cellspacing="0" class="resultsTable">
<tr>
<th>REG</th>
<th>PAID</th>
<th>EBD</th>
<th>APP</th>
<th>Update Record</th>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[]" id="update" value="1">
<input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="1" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[]" id="update" value="2">
<input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="2" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1"></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" name="update[]" id="update" value="3">
<input type="hidden" name="updateRecord" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="3" />
</td>
</tr>
<tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
</table>
</form>
The first two checkboxes are checked. Leave row 1 alone and check the second checkbox for record 2, select the update checkbox for the first two rows and then submit the form. What I expect to see is:
RECORD ID: 1
REG: 1
PAID:
EBD:
APP:
RECORD ID: 2
REG: 1
PAID: 1
EBD:
APP:
What I'm getting in this example is:
RECORD ID: 1
REG: 1
PAID: 1
EBD:
APP:
RECORD ID: 2
REG: 1
PAID:
EBD:
APP:
You can see where the first record shows the data from row two.
Check this. What i did here:
Preventing code coupling and human mistake by writing code just once and rendering the html inside a loop.
Assigning the id directly to the form data and making debug step easier
More readable and prettier code and clearing unnecessary lines
<?php
$ids = range(1, 3); // [1, 2, 3];
$true = "true";
$false = "false";
if(isset($_POST['update'])) {
foreach ($_POST['update'] as $id => $update) {
if ($_POST['update'][$id]) {
$regOK = isset($_POST['regOK'][$id]) ? $true : $false;
$paidOK = isset($_POST['paidOK'][$id]) ? $true : $false;
$ebdOK = isset($_POST['ebdOK'][$id]) ? $true : $false;
$appOK = isset($_POST['appOK'][$id]) ? $true : $false;
echo "RECORD ID: ".$id."<br>";
echo " REG: ".$regOK."<br>";
echo "PAID: ".$paidOK."<br>";
echo " EBD: ".$ebdOK."<br>";
echo " APP: ".$appOK."<br><br>";
}
}
}
echo "<br><br>";
?>
<style type="text/css">
th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
<table cellpadding="2" cellspacing="0" class="resultsTable">
<tr>
<th>REG</th>
<th>PAID</th>
<th>EBD</th>
<th>APP</th>
<th>Update Record</th>
</tr>
<?php foreach($ids as $id) { ?>
<tr>
<td><input type="checkbox" name="regOK[<?=$id?>]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[<?=$id?>]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[<?=$id?>]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[<?=$id?>]" id="appOK" value="1"></td>
<td><input type="checkbox" class="test" name="update[<?=$id?>]" id="update" value="1"></td>
</tr>
<?php } ?>
<tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
</table>
</form>
Bug in your code is indexing, handling form with index will solve it
<?php
if(isset($_POST['updateRecord'])) {
// echo "Update Record Outer Check<br>";
if ((strtolower($_POST['updateRecord']) == "update")) {
foreach ($_POST['update'] as $key => $update) {
$id = $_POST['id'][$key];
$regOK = (
isset($_POST['regOK'][$key]) ?
$_POST['regOK'][$key] : false
);
$paidOK = (
isset($_POST['paidOK'][$key]) ?
$_POST['paidOK'][$key] : false
);
$ebdOK = (
isset($_POST['ebdOK'][$key]) ?
$_POST['ebdOK'][$key] : false
);
$appOK = (
isset($_POST['appOK'][$key]) ?
$_POST['appOK'][$key] : false
);
echo "RECORD ID: ".$id."<br>";
echo " REG: ".$regOK."<br>";
echo "PAID: ".$paidOK."<br>";
echo " EBD: ".$ebdOK."<br>";
echo " APP: ".$appOK."<br><br>";
}
}
}
echo "<br><br>";
?>
<style type="text/css">
th,td {text-align:center;}
</style>
<form action="" method="post" name="theForm">
<table cellpadding="2" cellspacing="0" class="resultsTable">
<tr>
<th>REG</th>
<th>PAID</th>
<th>EBD</th>
<th>APP</th>
<th>Update Record</th>
</tr>
<tr>
<td><input type="checkbox" name="regOK[]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[]" id="update" value="1">
<input type="hidden" name="updateRecord[]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="1" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[1]" id="regOK" value="1" checked></td>
<td><input type="checkbox" name="paidOK[1]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[1]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[1]" id="appOK" value="1"></td>
<td>
<input type="checkbox" class="test" name="update[1]" id="update" value="2">
<input type="hidden" name="updateRecord[1]" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="2" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="regOK[2]" id="regOK" value="1"></td>
<td><input type="checkbox" name="paidOK[2]" id="paidOK" value="1"></td>
<td><input type="checkbox" name="ebdOK[2]" id="ebdOK" value="1"></td>
<td><input type="checkbox" name="appOK[2]" id="appOK" value="1"></td>
<td>
<input type="checkbox" name="update[2]" id="update" value="3">
<input type="hidden" name="updateRecord" id="updateRecord" value="update">
<input type="hidden" name="id[]" id="id" value="3" />
</td>
</tr>
<tr><td colspan="5" ><input type="submit" name="updateFormData" id="updateFormData" value="Update All Records"></td></tr>
</table>
</form>

How to post multiple fields values when clicking over a checkbox

I am working in a small project and I have a code to do a multiple post of fields values when clicking over a check box. I have a table which lists classes and time. I need to know how to click over a checkbox and send class and time values at same time using a loop.
This is driving me crazy. I have spent hours and I can't figure out how to send multiple values at same time. When I check all the values, it works perfect but if I click one or two values, I always get a wrong time value shown.
Here is the PHP code, I am using:
if(isset($_POST['submit'])){
if((!empty($_POST['class'])) && (!empty($_POST['time']))) {
$checked_count = count($_POST['class']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
for ($i=0; $i < $checked_count; $i++) {
echo $_POST['class'][$i]. " ";
echo $_POST['time'][$i]. "<br>";
}
}
}
else{
echo "<b>Please Select At least One Option.</b>";
}
Here is my HTML CODE:
<form action="checkclass.php" method="post">
<label class="heading">Select Your Technical Exposure:</label><br />
<tr>
<td><input type="checkbox" name="class[]" value="C/C++"><label>C/C++</label></td>
<td><input type="text" name="time[]" value="1" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="Java"><label>Java</label></td>
<td><input type="text" name="time[]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="PHP"><label>PHP</label></td>
<td><input type="text" name="time[]" value="3" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="HTML/CSS"><label>HTML/CSS</label></td>
<td><input type="text" name="time[]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="LINUX"><label>UNIX/LINUX</label></td>
<td><input type="text" name="time[]" value="3" width="15"><br /></td>
</tr>
<input type="submit" name="submit" Value="Submit"/>
</form>
If I check all the values. I get the following result:
Class Time
C/C++, 1
Java, 2
PHP, 3
HTML/CSS, 2
UNIX/LINUX, 3
If I only check the UNIX/LINUX class I get:
Class Time
UNIX/LINUX, 1
I get 1 for LInux time when it should be 3. I think the time field takes the first position in the array when click just one class.
How do I relate time and class values so that whole values in a row are posted at same time. I really want to do a multiple insert but I think solving this will help me.
I will appreciate any help with this matter.
Try this:
PHP Code:
<?php
if(isset($_POST['submit'])){
if((count($_POST['class']) > 0) && (count($_POST['time']) > 0)) {
$checked_count = count($_POST['class']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['class'] as $k => $v) {
echo $_POST['class'][$k]. " ";
echo $_POST['time'][$k]. "<br>";
}
} else {
echo "<b>Please Select At least One Option and the Number of Years of Technical Exposure.</b>";
}
}
?>
HTML Code:
<form action="checkclass.php" method="post">
<label class="heading">Select Your Technical Exposure:</label><br />
<tr>
<td><input type="checkbox" name="class[0]" value="C/C++"><label>C/C++</label></td>
<td><input type="text" name="time[0]" value="1" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[1]" value="Java"><label>Java</label></td>
<td><input type="text" name="time[1]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[2]" value="PHP"><label>PHP</label></td>
<td><input type="text" name="time[2]" value="3" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[3]" value="HTML/CSS"><label>HTML/CSS</label></td>
<td><input type="text" name="time[3]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[4]" value="LINUX"><label>UNIX/LINUX</label></td>
<td><input type="text" name="time[4]" value="3" width="15"><br /></td>
</tr>
<input type="submit" name="submit" Value="Submit"/>
</form>
It worked for me. Let me know if it worked for you.

Post table row of checkboxed column

I have a table like this
<form method="post" action="url">
<table>
<tr>
<td><input type="checkbox" name="cbox[]" value="1"></td>
<td><input type="text" name="name[]" value="first_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="2"></td>
<td><input type="text" name="name[]" value="second_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="3"></td>
<td><input type="text" name="name[]" value="third_name"></td>
</tr>
</table>
<input type="submit" value="send" />
</form>
then, when I post this form, I want to send only cbox[] and name[] of rows that have checkbox selected
Change your html to
<form id ='myform' method="post" action="url">
<input type='hidden' id='checkboxs' name='checkboxs'>
<input type='hidden' id='names' name='names'>
<table>
<tr>
<td><input type="checkbox" name="cbox[]" value="1"></td>
<td><input type="text" name="name1" value="first_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="2"></td>
<td><input type="text" name="name2" value="second_name"></td>
</tr>
<tr>
<td><input type="checkbox" name="cbox[]" value="3"></td>
<td><input type="text" name="name3" value="third_name"></td>
</tr>
</table>
<input type="button" value="send" onclick="submitValue()"/>
</form>
and add function
function submitValue()
{
var checkboxs = [];
var names = [];
$(':checkbox:checked').each(function(i){
checkboxs[i] = $(this).val();
names[i] = $('input[name=name'+(i+1)+']').val();
});
console.log(checkboxs);
console.log(names);
$('#checkboxs').val(checkboxs);
$('#names').val(names);
$('#myform').submit();
}
your value in checkboxs and names
Hope this help

Calling PHP name from multiple checkboxes

I'm making check list for a form that will send an email with to myself letting me know what items have been checked.
I have successful done this so far with just single inputs:
HTML:
<input type="text" name="visitorfirst" size="35" />
PHP:
From: $visitorfirst \n
This sends their name from the form to my email fine.
What I can't manage to do is have x amount of check boxes that all have the same name e.g. col with seperate values and send each one selected to my email if selected.
This is what I've tried:
HTML:
<form action="">
<table class="checks">
<tr>
<td><input type="checkbox" name="col" value="Desktop">Desktop</td>
<td><input type="checkbox" name="col" value="Laptop">laptop</td>
<td><input type="checkbox" name="col" value="Monitor">Monitor</td>
</tr>
<tr>
<td><input type="checkbox" name="col" value="Server">Server</td>
<td><input type="checkbox" name="col" value="Networking">Networking</td>
<td><input type="checkbox" name="col" value="Smartphone">Smartphone</td>
</tr>
<tr>
<td><input type="checkbox" name="col" value="Tablet">Tablet</td>
<td><input type="checkbox" name="col" value="Printer">Printer</td>
<td><input type="checkbox" name="col" value="Telephone">Telephone</td>
</tr>
</table>
</form>
PHP:
What hardware to collect: $col \n
How can I get the form to send multiple "checks"?
you need to create a checkbox group you do this by adding []
<td><input type="checkbox" name="col[]" value="Server">Server</td>
<td><input type="checkbox" name="col[]" value="Networking">Networking</td>
<td><input type="checkbox" name="col[]" value="Smartphone">Smartphone</td>
Your post return now returns an array so $_POST['col'] is an array you can then foreach loop through it or implode it to a csv or serialise it or json encode it or do whatever you like with it.
(In your case probably for each to generate your email code)

how can I get data in the table cell that is checked after submit

can someone give me help, please.
here's my basic html
<form action="addSomething.php" method="POST">
<table>
<tr>
<th>Add Data</th>
<th>Description</th>
<th>Quantity</th>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Newbie</td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Pro</td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1"/> </td>
<td class="desc" > Master </td>
<td>1</td>
</tr>
<br/>
<input type="submit" name="add" value="SUBMIT"/>
.....
how can I get the one with the class "desc" and the column for quantity that is checked after submitting the form
because the only I can add when querying in mysql is the value of checkbox but I want also the value of the data in "Description" column and "Quantity" column
In my addSomething.php the code I have is
if(isset($_POST['add']))
{
foreach($_POST['data'] as $value)
{
$sql = "INSERT INTO tablename (column1) VALUES('$value');"
//query stuff
}
}
what I will do , Any hints guys?
you can give your checkboxes different values
<input type="checkbox" name="data[]" value="newbie" />
<input type="checkbox" name="data[]" value="pro" />
<input type="checkbox" name="data[]" value="master" />
and then in addSomething.php define an array
$names = array('newbie'=>'Newbie', 'pro'=>'Pro', 'master'=>'Master');
and use it in your sql
if(isset($_POST['add']))
{
foreach($_POST['data'] as $value)
{
$sql = "INSERT INTO tablename (column1) VALUES ('".$names[$value]."');";
}
}
this applies only if you don't want users to edit the decription and quantity on the frontend. if you do, you need to put inputs in there and give them unique names.
I'd advise something along the lines of:
<form action="addSomething.php" method="POST">
<table>
<tr>
<th>Add Data</th>
<th>Description</th>
<th>Quantity</th>
</tr>
<tr>
<td><input type="checkbox" name="data[0]" value="sample1" /> </td>
<td class="desc">Newbie<input type="checkbox" name="desc[0]" value="Newbie" /></td>
<td>2<input type="checkbox" name="quan[0]" value="2" /></td>
</tr>
<tr>
<td><input type="checkbox" name="data[1]" value="sample1" /> </td>
<td class="desc">Pro<input type="checkbox" name="desc[1]" value="Pro" /></td>
<td>1<input type="checkbox" name="quan[1]" value="1" /></td>
</tr>
<tr>
<td><input type="checkbox" name="data[2]" value="sample1"/> </td>
<td class="desc" > Master <input type="checkbox" name="desc[2]" value="Master" /></td>
<td>1<input type="checkbox" name="quan[2]" value="1" /></td>
</tr>
<br/>
<input type="submit" name="add" value="SUBMIT"/>
...
with php
if(isset($_POST['add']))
{
foreach($_POST['data'] as $i => $value) {
$desc = mysql_real_escape_string($_POST['desc'][$i]);
$quan = mysql_real_escape_string($_POST['quan'][$i]);
$sql = "INSERT INTO tablename (desc,quan) VALUES('$desc','$quan');"
//query stuff
}
}
You have to explicitly number your fields in html to preserve associacion of data with checkboxes.
What you could do is write Javascript that triggers an AJAX request to your server.
Otherwise, you won't be able to get values in prior submit.
That, is by far, very inefficient. Executing a query for each field in your table.
You will either have to put the description in a hidden field or read the description from the database.
I suggest the later option.
You'll need to include these values in a hidden input
<input type="hidden" name="whatever" value="whatever>"
as an example
I think the best way for your code is you create a javascript function and call it on submit of form :
<form action="addSomething.php" method="POST" onsubmit="return postVars()">
when you submit the form, this function will be called. create a 2 hidden forms like this :
<input type="hidden" name="description" id="description">
<input type="hidden" name="quantity" id="quantity">
give a unique id to your td s and pass the id to your js function. fill these inputs in your js function :
document.getElementById('description').value = document.getElementById('td_' + id + '_description').innerHTML;
document.getElementById('quantity').value = document.getElementById('td_' + id + '_quantity').innerHTML;
and in your server side just get description and quantity from $_POST.
hope it's been helpful ;)

Categories