How to post multiple fields values when clicking over a checkbox - php

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.

Related

Get All Data PHP-HTML (Including all Checkbox)

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

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>

html dynamic table print to php

I need some help...
I've created a form with a dynamic table in it. I would like to learn how to pass it through php and echo/print it to a php page to save the page as pdf.
I am stuck on this part. I've looked everywhere for some answers and I cant seem to be finding anything.
<form action="display_form.php" method="POST">
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
<td><INPUT type="text" name="step" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
</table>
<INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
<INPUT type="submit" value="Send"/>
I have called the php file display_form.php where I would like to display the dynamic table data in the. The function to save it as a pdf is in there as well. But I am mainly after the printing of the form to the php file. I am having no luck there at all!
I'm not worried on posting the table to mysql. I have other ways where i don't need it in mysql
I would really appreciate the help
2 things
you have not closed tr
Next
Make all input elements as array. Then you can fetch them in your php file
<td style="width:20px;"><INPUT type="checkbox" name="chk[]" /></td>
<td><INPUT type="text" name="step[]" style="width:160px;" autocomplete="on" placeholder="step" required/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" autocomplete="on" placeholder="url" required/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" autocomplete="on" placeholder="process" required/></td>
<td>
<SELECT name="pass-fail[]" style="width:100px;">
<OPTION value="Pass">one</OPTION>
<OPTION value="Fail">two</OPTION>
</SELECT>
</td>
<td><INPUT type="text" name="comment[]" style="width:190px;" autocomplete="on" placeholder="Comment" required/></td>
In your php file, use foreach loop to get all the values.
$i = 0;
$array = array();
foreach ($_POST['step'] as $row) {
$array[$i]['step'] = $row;
$array[$i]['chk'] = isset($_POST['ckh'][$i]) ? 1 : 0;
$array[$i]['url'] = $_POST['url'][$i];
$i++;
//...//so on
}
print_R($array);
As I can see, the javascript addRow function should add another tr-row in the table (the closing is missing by the way).
Now you have in every table row inputs with the same names. That won't work.
You need to say, that they are arrays, to access them via $_POST['step'][0] and so on.
Just give them name="step[]" and the form will automatically count the index for you.
Use name='step[]',name='url[]',name='process[]' as input type for your add row functionality,
Now when you submit form you are getting array then store that array as below.
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
Now use foreach loop.
foreach($step as $key => $row){
echo $step[$key];
echo $url[$key];
echo $process[$key];
}
Now format your data in html then generate pdf as per below link.
http://phptopdf.com/
if (isset($_POST)) {
$step = $_REQUEST['step'];
$url = $_REQUEST['url'];
$process = $_REQUEST['process'];
$pass_fail = $_REQUEST['pass-fail'];
$comment = $_REQUEST['comment'];
foreach ($step as $key => $row) {
{
?>
<table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
<tr>
<td><INPUT type="text" name="step[]" style="width:160px;" value="<?php echo $step[$key]; ?>"/></td>
<td><INPUT type="text" name="url[]" style="width:62px;" value="<?php echo $url[$key]; ?>"/></td>
<td><INPUT type="text" name="process[]" style="width:63px;" value="<?php echo $process[$key]; ?>"/>
</td>
<td><input name="pass-fail[]" style="width:100px;" value="<?php echo $pass_fail[$key]; ?>"/></td>
<td><INPUT type="text" name="comment[]" style="width:190px;" value="<?php echo $comment[$key]; ?>"/>
</td>
</tr>
</table>
<?php }
}
}
Working just fine now!!
here the table is displaying correctly

How to use associate arrays to get the id from the checkbox and appropreate value from the text field

Hi everyone, i'm new to programming. I need to develop a rating system with check boxes and text-fields where user clicks the subjects from the list and add his rating/experience in the text field. All these subjects and ratings are added to the database with the subject id and the rating. So the issue is, I don't know how to write the associate array to get the selected subjects and their appropriate rating to insert into the database. Can anyone please provide me some codes or samples similar to this. So, I can get some idea how to do this. Thanks in advance :)
This is the HTML sample code
<form action="" method="post">
<table width="372" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="24"><input type="checkbox" name="subid1" value="1" id="subid1"></td>
<td width="203">Maths</td>`enter code here`
<td width="145"><input type="text" name="sub1" id="sub1"></td>
</tr>
<tr>
<td><input type="checkbox" name="subid2" value="2" id="subid2" /></td>
<td>Physics</td>
<td><input type="text" name="subid2" id="subid2" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid3" value="3" id="subid3" /></td>
<td>Computing</td>
<td><input type="text" name="subid3" id="subid3" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid4" value="4" id="subid4" /></td>
<td>Chemistry</td>
<td><input type="text" name="subid4" id="subid4" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid5" value="5" id="subid5" /></td>
<td>Biology</td>
<td><input type="text" name="subid5" id="subid5" /></td>
</tr>
<tr>
<td><input type="checkbox" name="subid7" value="6" id="subid7" /></td>
<td>Human Biology</td>
<td><input type="text" name="subid6" id="subid6" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Submit" /></td>
<td> </td>
</tr>
</table>
</form>
This will do the job on client side:
<input type="checkbox" name="cb[myID1]" value="1" />
<input type="text" name="t[myID1]" value="" />
<input type="checkbox" name="cb[myID2]" value="1" />
<input type="text" name="t[myID2]" value="" />
and this can be used on server side:
$usedTexts = array();
if ( array_key_exists("t", $_POST) && is_array($_POST["t"])
&& array_key_exists("cb", $_POST) && is_array($_POST["cb"])
)
{
$usedTexts = array_intersect_key($_POST["t"], $_POST["cb"]);
}
see manual for server side: http://us3.php.net/array_intersect_key
edit: fixed to POST; added array_key_exists() and is_array()

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