I m trying to store checkbox value subject_id but when i store this value to database it just store the last selected checkbox inside the database... please help me to sort out the code so I can get all selected values.
I want to save all the checkbox values I selected,but if I check more then one checkbox it always save the last checkbox.
function get_class_section($day='',$section='',$class='')
{
print_r($section);
$sections = $this->db->get_where('class_routine' , array(
'day' => $day,'section_id'=>$section,'class_id'=>$class
))->result_array();
foreach ($sections as $row) {
echo "<table class=\"table table-bordered datatable\" id=\"table_export\" >";
echo "<thead>";
echo "<th>Subject</th><th colspan=\"2\">status</th>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
echo "<td>". $this->crud_model->get_subject_name_by_id($row['subject_id'],"<input type=\"text\" name=\"subject_id\" value=\"subject_id\" >");
echo "<input type=\"checkbox\" name=\"types[]\" value=\"".$row['subject_id']."\" set_checkbox('types[]', 'subject_id')>";
echo "</td>";
echo "<td>present <input type=\"checkbox\" value=\"1\" ></td>"
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>";
echo "</tr>";
echo "</tbody>";
echo "</table>";
}
}
how to save all the check-boxes that i selected
You need foreach loop to get all checked checkboxes
Try This:
if(isset($_REQUEST['types']) && $_REQUEST['types']!=array())
{
foreach ($_REQUEST['types'] as $key => $value) {
// in $value,you will get checkbox value
}
}
You should use hidden element for all checkbox
Try this
. "<td>Absent <input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>
<input type=\"hidden\" name=\"types[]\" value=\"0\"> ";
Related
I need to create an order form that has a checkbox for each product in a table and have a quantity box next to each product.
I need to process the products that have been checked and update an orders table with the checked products and the quantity entered.
I don't know how to link the product checkbox to the quantity field.
$sql="select * from tproducts";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table>";
echo "<tr>";
echo "<th>Product Names</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=welcome1.php method=post>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "</tr>";
}
}
echo "</table>";
echo '<input type="submit" name="submit" value="submit" />';
echo "</form>";
echo "</html>";
welcome1.php
<?php
echo "materialcode<br>";
print_r($_POST['materialcode']);
echo "<br>";
echo "qty<br>";
print_r($_POST['qty']);
?>
PHP will keep the key name you supply in the fields html.
$productIndex = 0;
while ($row = mysqli_fetch_array($result) )
{
echo "<tr>";
echo '<td><input type="checkbox" value="' . $row['intProductID'] . '" name="materialcode[$productIndex]">' . $row['strProductName'] . "</td>";
echo "<td>Enter Quantity </td>";
echo "<td><input type='text' name='qty[$productIndex]' value='qty' size=5></td>";
echo "</tr>";
$productIndex++;
}
This example uses numeric indexes but you could probably use $row['intProductId'] as well.
In your php:
$_POST['materialCode'][0] will be the analog to $_POST['qty'][0] but only the checked checkboxes will be supplied. You might end up with a $_POST['materialCode'] array that has index 0, 5, and 7 if those were the only ones checked.
So use a foreach loop instead of a for loop.
foreach( $_POST['materialCode'] as $key => $value ){
$productId = $value;
$quantity = $_POST['qty'][$key];
}
I try to figure out how to handle two nested forms. The first one sends data from select-option dropdown to another_file.php. The nested one should send data from check boxes to current file and should be handled by isset($_POST['...']). Here is my simplified code:
if (isset($_POST['a']))
{
// do something
}
if (isset($_POST['b']))
{
// do something else
}
echo "<form action='another_file.php' method='post'>"; // begin of first form
echo "<table class ='table table-hover table-condensed table-striped table-bordered'>";
echo "<thead>";
echo "<th>ID</th><th>Jídlo</th><th>Množství</th><th>por_cislo</th><th>Odebrat</th>";
echo "</thead>";
while($rows = $stmt->fetch()){
echo "<tr><td>" . $rows['id'] . "</td><td>".$rows['jidlo'];
echo "<select name =".$rows['id']."_".$rows['por_cislo']."> ";
$stmt2 = $db->query($q2);
echo "<option value ='nic'> (vyberte potravinu) </option>";
$max_por_cislo = $rows['por_cislo'];
while($rows2 = $stmt2->fetch())
{
echo '<option value="'.$rows2['id'].'">'.$rows2['jidlo'].'</option>';
};
echo "</select>";
echo "</td> <td>" . $rows['mnozstvi'] . "g <input name = '".$rows['por_cislo']."' type='text' value = '-'></td><td>".$rows['por_cislo']."</td>";
echo "<td><form action ='this_file.php' method = 'post'>"; // begin of nested form
echo "<input type='checkbox' id='atur_peg' name='idecko[]' value=".$rows['id']."*".$rows['por_cislo']." /></td></tr> ";
};
echo "<input type='hidden' name='pc' value=".$plan_cislo.">";
echo "<tr><td colspan='2'><input name = 'go' type='submit' value='OK'/></td><td colspan='2'><input type = 'submit' name ='a' value='ADD'/></td>";
echo "<td><input type = 'submit' name ='b' value='DELETE checked'/></form></td></tr>"; // end of nested form
echo "</table>";
echo "</form>"; // end of first form
Is
there any way to do this correctly?
Instead of nested forms, use jquery to detect checkbox being checked and then change the form action url. Something on this line.. You will have to modify the same to suit your needs.
$(document).ready(function(){
$("#formname").on("change", "input:checkbox", function(){
if( $(this).is(":checked") ) {
$('#formName').attr('action', 'this_file.php');
$("#formname").submit();
}
});
});
I'm having a problem adjusting the values being passed into $_POST from the table I have set up.
Here's the code that's being used to set up the table:
print "<form action=\"updateitem.php\" method=\"post\">\n";
print "<table border cellpadding = 3>\n";
print "<tr>\n";
print "<th>Menu item id</th>\n";
print "<th>Menu item name</th>\n";
print "<th>Menu</th>\n";
print "<th>Menu item price</th>\n";
print "</tr>\n";
while($info = mysql_fetch_array($results)){
$mi = explode("_",$info['meta_key']);
if ($mi[2] == "food")
$mi[2] = "takeout";
print "<tr>\n";
print "<td id=\"meta_id\">".$info['meta_id']."<input type=\"hidden\" name=\"meta_id".$i."\" value=\"".$info['meta_id']."\" /></td>\n";
print "<td>".$info['post_title']."</td>\n";
print "<td>".$mi[2]."</td>\n";
print "<td id=\"price\" contenteditable=\"true\">".$info['meta_value']."<input type=\"hidden\" name=\"meta_value".$i."\" value=\"".$info['meta_value']."\" /></td>\n";
print "</tr>\n";
$i++;
}
print "</table>\n";
print "<input type=\"Submit\" value=\"Update\" />\n";
print "</form>\n";
What I'm trying to do is that when someone changes the price cell, I want the updated price to be passed in the $_POST for PHP. Is that possible?
The contenteditable attribute is not going to help you here.
You have to use javascript to notice when that is changed and do something with it.
The simple answer is, you need to make the <input> field available to the user. So change the type="hidden" to type="text" or add another field that is not hidden for the user to interact with, and then the value will be passed back to your script in $_POST.
Suggested changes
print "<form action=\"updateitem.php\" method=\"post\">\n";
print "<table border cellpadding = 3>\n";
print "<tr>\n";
print "<th>Menu item id</th>\n";
print "<th>Menu item name</th>\n";
print "<th>Menu</th>\n";
print "<th>Menu item price</th>\n";
print "</tr>\n";
while($info = mysql_fetch_array($results)){
$mi = explode("_",$info['meta_key']);
if ($mi[2] == "food")
$mi[2] = "takeout";
print "<tr>\n";
print '<td id="meta_id">' . $info['meta_id'] . '<input type="hidden" name="meta_id[]" value="' . $info['meta_id'] . '" /></td>';
print '<td>'.$info['post_title'].'</td>';
print '<td>'.$mi[2].'</td>';
print '<td id="price">';
print '<input type="hidden" name="original_price[]" value="' . $info['meta_value'] . '" />';
print '<input type="text" name="price[]" value="' . $info['meta_value'] . '" />';
print '</td>';
print "</tr>\n";
$i++;
}
print "</table>\n";
print "<input type=\"Submit\" value=\"Update\" />\n";
print "</form>\n";
I took the liberty of changing the names ( name="name[]" ) to use arrays instead of ( name="name" . $i )
This will return the fields as arrays in the $_POST array
So $_POST[price] will itself be an array like so:
$_POST['price][0]
$_POST['price][1] // and so on
So you can process the changed prices as
foreach ( $_POST['price'] as $i => $price ) {
if ( $price != $_POST['original_price'][$i]) {
// We have a price change lets update the database
// using $_POST['meta_id'][$i] as the key to the database table ? I assume ??
}
}
Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}
I'm trying to pass a text value field over to the next page for every checkbox selected, but I'm only getting the last text fields value, example:
checkbox textfield
selected ABCD
selected ABCDE
I am only getting back the ABCDE every time
page1.php
echo "<td width='10px'><input name='question[$rowid][]' type='checkbox' value='1' /></td>";
echo "<td width='230px'><input name='newname' type='text' value='$certn'/></td>";
page2.php
foreach ($_POST['question'] as $key => $ans) {
$nn = $_POST['newname'];
echo $key . $nn;
echo "</br>";
}
Help will be greatly appreciated
It's a little hard to work out exactly what you're doing here but I think your statement I'm only getting the last text fields value indicates your problem - you have multiple fields with the same name. If you do this and don't make them into an array ([]), you will only get the last value on the page.
I think you want something more like this:
Page 1:
echo "<td width='10px'><input name='question[$rowid]' type='checkbox' value='1' /></td>";
echo "<td width='230px'><input name='newname[$rowid]' type='text' value='$certn'/></td>";
Page 2:
foreach ($_POST['question'] as $key => $ans) {
// $_POST['newname'] is now also an array, and the keys should correspond to
// those in the $_POST['question'] array
$nn = $_POST['newname'][$key];
echo $key . $nn;
echo "</br>";
}
The line:
echo "<td width='10px'><input name='question[$rowid][]' type='checkbox' value='1' /></td>";
will not be correctly interpreted. You must change it to:
echo "<td width='10px'><input name='" . $question[$rowid][] . "' type='checkbox' value='1' /></td>";
Arrays are not substitued inside a string.