There are multiple checkbox about 50+ for user access, checkbox are like follows;
<input name="PREFIX_1" type="checkbox">
<input name="PREFIX_2" type="checkbox">
<input name="PREFIX_3" type="checkbox">
...........
I have multiple checkbox with its respective values which I loop through to add/update the values in the database, which is like follows:
foreach ($_POST as $field => $value ) {
if ( preg_match('/^PREFIX_/', $field) ) {
$access = isset($value)?'1':'0';
$file = substr($field, 4);
$this->db->query('UPDATE_DESIRED_TABLE');
}
}
The problem is when I add/update the values in the database by using loop foreach ($_POST as $field => $value ) and checking the prefix, I only get values of checkbox that are checked and the values of data that are not checked are not in the $_POST data.
And, I don't get any values which are not checked, which is not even the name of that checkbox. In other words only those values are set which are checked.
Now I have to change the value of all the data in the database by comparing both the data in the database and from the post data.
How can I solve this?
You would need to send hidden inputs along with the checkboxes like this:
<form action="" method="post">
<input type="hidden" id="PREFIX_1_" name="PREFIX_1" value="0">
<input type="checkbox" id="PREFIX_1" name="PREFIX_1" value="1" />
<input type="hidden" id="PREFIX_2_" name="PREFIX_2" value="0">
<input type="checkbox" id="PREFIX_2" name="PREFIX_2" value="1" />
<input type="hidden" id="PREFIX_3_" name="PREFIX_3" value="0">
<input type="checkbox" id="PREFIX_3" name="PREFIX_3" value="1" />
<input type="submit" name="sub" value="Go" />
</form>
However you cant use the isset anymore with this, change PHP like this:
if(isset($_POST['sub']))
{
foreach ($_POST as $field => $value )
{
if ( preg_match('/^PREFIX_/', $field) )
{
$access = $value;
$file = substr($field, 4);
$this->db->query('UPDATE_DESIRED_TABLE');
}
}
}
Considering you know the number of checkbox, stored in example in $checkBoxCount, you might use this kind of loop :
for ($i = 1; $i <= $checkBoxCount; $i++)
{
$access = ((isset($_POST["PREFIX_" . $i])) ? ('1') : ('0')));
//manage DB
}
Related
I can't get this to work. I need to update many records in 1 column, based on what's checked and filled out. I tried different combinations of checking and unchecking and having the text fields blank or not blank, and in this php post code i tried many different things, but can't figure out the correct combinations and if/elses or issets or empties, etc.
the values in the checkboxes correspond to record/row IDs. all the text boxes will be prefilled with prices. all the checkboxes will be dynamically checked or unchecked. a person can undo checked checkboxes if they want or check checkboxes that are not checked. on post, all the records that are checked should get the matching text box value.
the problem is i can't get the 2 arrays to match in my post. for example, in this sample set of fields, let's say i check the 2nd checkbox and the 4th checkbox. the records that should update and the values that should save into the column should be as follows...
2 -> 17.67
4 -> 19.84
but instead i get:
2 -> 16.95
4 -> 17.67
or this (if i remove the values from 1st and 3rd text boxes):
2 -> empty
4 -> 17.67
or this (2nd checkbox id and value missing completely)
4 -> 17.67
what am i doing wrong?
if (isset($_POST["savelist"]) && !empty($_POST["savelist"])) {
$productidcheckboxes = isset($_POST['productid']) ? $_POST['productid'] : array();
$listprices = isset($_POST['listprice']) ? $_POST['listprice'] : array();
//other things i tried
//$listprices = (empty($_POST['listprice'])) ? $_POST['listprice'] : array();
//$listprices = (!empty($_POST['listprice'])) ? $_POST['listprice'] : array();
//$productidcheckboxes = $_POST['productid'];
//$listprices = $_POST['listprice'];
$new = array();
for ($i=0; $i<count($productidcheckboxes); $i++) {
$new[] = $productidcheckboxes[$i];
$new[] = $listprices[$i];
}
$k=0;
foreach ($new as $value) {
$k++;
if($k==1){
$theid = $value;
}
if($k==2){
$thelistprice = $value;
//different ifs i tried
//if ($theid<>"")
//if ($value<>"")
//if ($theid<>"" && $thelistprice<>"")
//if ($theid<>"" && $value<>"")
if ($thelistprice<>"")
{
echo $theid.": ";
echo $thelistprice."<br>";
//update table with the list prices
//mysql_query("UPDATE table_name SET mylistprices = '$thelistprice' WHERE id = $theid");
}
$theid = "";
$thelistprice = "";
$k=0;
}
}
}
form looks like this
<form action="" method="post">
<input type="checkbox" value="1" name="productid[]">
<input type="text" value="16.95" name="listprice[]">
<input type="checkbox" value="2" name="productid[]">
<input type="text" value="17.67" name="listprice[]">
<input type="checkbox" value="3" name="productid[]">
<input type="text" value="18.81" name="listprice[]">
<input type="checkbox" value="4" name="productid[]">
<input type="text" value="19.84" name="listprice[]">
<input type="checkbox" value="5" name="productid[]">
<input type="text" value="16.85" name="listprice[]">
<input type="submit" value="Save List" name="savelist">
</form>
by the way, by uneven i mean all the checkboxes will have values so correct rows will be updated, but the text boxes may or may not be filled. i would like it if i didn't have to clear any values in checkboxes or text inputs. it should just update records that are checked with it's corresponding values, and ignore non-checked checkboxes and the non-checked checkboxes corresponding values. but in the end, i may have to change how it's done, but i can't solve this one.
Add hardcoded numeric values to the form names so they match up in your processing page. Right now they are random:
<form action="" method="post">
<input type="checkbox" value="1" name="productid[1]">
<input type="text" value="16.95" name="listprice[1]">
<input type="checkbox" value="2" name="productid[2]">
<input type="text" value="17.67" name="listprice[2]">
<input type="checkbox" value="3" name="productid[3]">
<input type="text" value="18.81" name="listprice[3]">
<input type="checkbox" value="4" name="productid[4]">
<input type="text" value="19.84" name="listprice[4]">
<input type="checkbox" value="5" name="productid[5]">
<input type="text" value="16.85" name="listprice[5]">
<input type="submit" value="Save List" name="savelist">
</form>
Now you know if the user checks product[4], it really is product[4]. When you leave your keys blank like productid[], that is just an anonymous spot in the array and makes it impossible to track when dealing with checkboxes that have no value unless checked.
If you check off productid[2] and productid[4] you know that the values in the listprice array are the values that go with what you have checked off:
Array
(
[listprice] => Array
(
[1] => 16.95
[2] => 17.67
[3] => 18.81
[4] => 19.84
[5] => 16.85
)
[productid] => Array
(
[2] => 2
[4] => 4
)
)
To access the values, loop through the productid but access the listprice:
foreach($_POST['productid'] as $key => $value){
echo $_POST['listprice'][$value].'<br />';
}
I want to store multiple checkbox value in database ,and my checkbox name is differnt,how can save ??.I have three column like Called them,Called you,toured, how i can store each value in data base.
HTML Code
<input type="checkbox" name="Called_them[]" value="1">1<br>
<input type="checkbox" name="Called_them[]" value="2">2<br>
<input type="checkbox" name="Called_you[]" value="1">1<br>
<input type="checkbox" name="Called_you[]" value="2">2<br>
PHP Code:-
if(isset($_POST['submits']) )
{
$checked = $_POST['alled_them'];
$checked1 = isset($_POST['alled_them']);
for($i=0; $i < count($checked); $i++){
$tellfriend=new MemberEmails;
$tellfriend->called_them = isset($checked[$i])==0?'':$checked[i];
$tellfriend->save();
}
if(isset($_POST["submit"])) {
foreach($_POST['called_them'] as $called_them) {
//do things
}
foreach($_POST['called_you'] as $called_you) {
// do things
}
}
$array_called_them = $_POST['called_them'];
$array_called_them = $_POST['called_you];
You now have both checkbox groups as arrays. if you want the value from the array just use foreach on each of the arrays.
Iam writing a program where i have a form with two fields and a 'PLUS BUTTON' upon clicking it two more fields will appear. By clicking PLUS Button again two more fields will generate and it continues as many times we click the PLUS BUTTON. Here's my program.
<form action="project_values/action_nowproject.php" method="post"enctype="multipart/form-data"><div id="item"><input type="text" name="add_qty" id="add_vender" class="ttexbox" required="required"><input type="text" name="add_name" class="ttexbox"><input onClick="addRowv(this.form);" type="button"style="cursor:pointer" class="addround" /></div></form>
in Javascript
<script type="text/javascript"> var rowNum = 0; function addRowv(frm) { rowNum ++;
var row = '<p id="rowNum'+rowNum+'"><span class="ftext">Item quantity:</span> <input type="text" name="m_name[]" value="'+frm.add_qty.value+'"><br> <span class="ftext">Item name: </span><input type="text" name="mi_name[]" value="'+frm.add_name.value+'"><br><br /> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';
jQuery('#itemRowsv').append(row); frm.add_qty.value = ''; frm.add_name.value = ''; } function removeRow(rnum) { jQuery('#rowNum'+rnum).remove(); } </script>
Now I have to fetch the values of extra fields appeared by clicking the plus button and send them to database.
How to fetch the values multiple array's genereated? heres my sql query insert statement.
$mp = "INSERT INTO pm_manr(name,item_nm)VALUES ('$add_qty','$item_name')";
$updata = mysql_query($mp);
How to get values in $add_qty,$item_name ? Some one pls help me.
Lets asume you have something like this:
line 1 <input name="foo[]" /> <input name="bar[]" />
line 2 <input name="foo[]" /> <input name="bar[]" />
line Y <input name="foo[]" /> <input name="bar[]" />
line Z <input name="foo[]" /> <input name="bar[]" />
You can loop trough them both by using the key from a foreach on the other values:
foreach($_POST['foo'] as $key =>$value){
echo $_POST['foo'][$key]; // the same as echo $value
echo $_POST['bar'][$key]; // the corresponding value of $_POST['bar']
// This is where you add your query
}
Use array_combine() which creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
Try this:
$arr = array_combine($_POST['m_name'],$_POST['mi_name']); // combines both arrays
foreach($arr as $key => $value){
$mp = "INSERT INTO pm_manr(name,item_nm)VALUES ('$key','$value')";
$updata = mysql_query($mp);
}
Is it possible to populate a checkboxlist from the database values? If so, please suggest how to do this.
$query2 = "select * from Products where CategoryID = '$CategoryName' ";
mysql_query($query2) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($query2)){
$array[] = $row;}
foreach($array as $val)
{
if($val =='the checkbox value')
<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>
These are the few steps you have to do.
1.Fetch the values from database.
2. Covert the values in array as they are stored as string by (,) separated.
3. Then
foreach($values as $val)
{
if($val =='Bike')
{
$bikeflag = '1';
}
if($val =='Car')
{
$carflag = '1';
}
}
<form>
<input type="checkbox" name="vehicle" value="Bike" <?php if(isset($bikeflag ) =='1'){ ?>checked = checked <?php } ?>/> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" <?php if(isset($carflag) =='1'){ ?>checked = checked <?php } ?>/> I have a car
</form>
Hopefully this will help you.
Get values from database with the appropriate SQL query
Loop through results echoing out a checkbox element
Caveats
Make sure each one has a unique name unless you want them to be an array. Then use array syntax for their name (e.g. name[])
Make sure to give each one a unique value
Yes, its very much possible:
Fetch the records to be populated with the structure:
Id and value
Provide datasource of the checkedbox list as the fetched structured list as mentioned above.
Now define the value field of the checkboxlist as "Id" and text as "value"
The key code is:
while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{ $id=$row[id];
$html=<<<html
<tr><td>
<input style="float:left" type="checkbox" name="mycheckbox[]" value="$id">
<span style="float:left">$row[content]</span>
<span style="color:black;float:right">$row[submitter]</span></td></tr>
html;
echo $html;
}
There are many checkboxes.
How to receive these checkbox values in another PHP file?
You know, you need to name these checkboxes so a PHP file can receive these values on the other side. But how to name these checkboxes? If I name 1,2, 3,...,how can I associate them with $row[id]?
You need to give them names - you can either do it like this:
<input style="float:left" type="checkbox" id="$id" name="$id" value="true">
or like this to get an array:
<input style="float:left" type="checkbox" id="$id" name="myBoxes[$id]" value="true">
You can then check isset($_POST[$id]) or isset($_POST['myBoxes'][$id])
Name your checkboxes so you can easily identify them.
eg
$CheckBoxHTML = "<input type='checkbox' name='check_$row[id]' value='YES'>Check This";
then in your recieving php file you can find all checkboxes by
foreach ($_POST as $key => $value)
{
if (strpos($key,'check_') !== false)
{
list($tmp, $ID) = split('_', $key);
$CheckedValues[] = $ID;
}
}
That will pull out all your checked ids.