I need to insert more than one values in checkbox but now it is seemed to be only the last value am entering is inserting to database.
this is my HTML code
<input id="option5" type="checkbox" name="product[]" value="<?php echo $result["software"];?>">
<label for="option5"><span><span></span></span><?php echo $result["software"];?></label>
and code for insertion is
$array = array($_POST['product']);
$value = implode(',', $array);
echo $value;
If you want to save multiple values in database then you need to create multiple checkboxes in html and then save the values with implode() method as below:-
<input id="option1" type="checkbox" name="product[]" value="value1">
<label for="option1">Value1</label>
<input id="option2" type="checkbox" name="product[]" value="value2">
<label for="option2">Value2</label>
<input id="option3" type="checkbox" name="product[]" value="value3">
<label for="option3">Value3</label>
now to store in database you can use implode() method.
$values=implode(",",$_POST['product']);
now store the $values in the database.
If i understand your problem you are looking for something like this:-
$array= array(
'0' => 123,
'1' => 456,
'2' => 789,
); // this is your post product array oryou want to get last value of that arrray
-----Or----
$array = $_POST['product']; // your post data after submission
end($array); // move the internal pointer to the end of the array
$key = key($array); // fetches the key of the element pointed to by the internal pointer
echo $array[$key]; // 789
Hope it helps!
Your problem is likely:
You have multiple checkboxes.
And you want to insert all of them into database.
You need to loop your posted checkboxes.
Your problem is likely that you are not looping over the $_POST['product'], so, only, the last product is inserting into database.
So, your checkboxes:
<input id="option5" type="checkbox" name="product[]" value="<?php echo $result["software"];?>">
<label for="option5"><span><span></span></span><?php echo $result["software"];?></label>
The code for posted file:
if (! empty($_POST['product'])) {
foreach ($_POST['product'] as $product) {
// Here, you insert $product into database.
}
}
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 have three inputs type text in an HTML page and a button which if clicked duplicate each text box (Javascript) making them 6.
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
<button>Add more</button>
Which generate same inputs again:
<input type="text" name="category[]">
<input type="text" name="quantity[]">
<input type="text" name="amount[]">
A piece of code in Cakephp I have been trying:
$data = $this->request->data;
foreach($data['category'] as $index => $value){
$this->ModelName->save($value);
}
Trying to get two rows inserted at once with quantity, category and amount as columns. But it is not inserting and not giving any error.
Is there a way I can achieve this?
Thanks.
I'm not sure how your model works in cakephp, but you should be able to get a complete grouping of data like:
foreach($data['category'] as $index => $value){
$category = $value
$quantity = $data['quantity'][$index];
$amount = $data['amount'][$index];
// use the above 3 variables however you need to to persist the model
//$this->ModelName->save($value);
}
On a side note, you may want to consider reordering your html inputs to be like:
<input type="text" name="item[0][category]">
<input type="text" name="item[0][quantity]">
<input type="text" name="item[0][amount]">
And then maintain the next index, incrementing the numeric index of item for each additional group
This will allow you to iterate like:
foreach($data['item'] as $index => $group){
//$group['category'];
//$group['quantity'];
//$group['amount'];
}
I am storing content into a database table. One table column is called attrbutes and has a list of values such as (ex: 1, 3, 5) based on the checkboxes that were checked.
<form>
<input type="checkbox" name="attribute" value="1">Attr 1<br>
<input type="checkbox" name="attribute" value="2">Attr 2<br>
<input type="checkbox" name="attribute" value="3">Attr 3<br>
<input type="checkbox" name="attribute" value="4">Attr 4<br>
<input type="checkbox" name="attribute" value="5">Attr 5<br>
<form>
Couple of questions on how to integrate checkboxes with PHP...
1) How do I check to see if at least 1 checkbox is checked on form submit?
2) How do I turn the checked checkboxes into a list like 1, 3, 5 if checkboxes 1, 3, and 5 are selected.
3) As a reverse to #2, on page load I need to figure out how to check each checkbox that's value is listed in the database column. if 1, 3, 5 is listed in table column, I need checkboxes 1 3 and 5 checked on page load.
I know how to code the basic queries for inserting, updating, and removing etc...but I've never worked with checkboxes and storing values from checkboxes using php before.
Change you html:
<input type="checkbox" name="attribute[]" value="1">Attr 1<br>
<input type="checkbox" name="attribute[]" value="2">Attr 2<br>
<input type="checkbox" name="attribute[]" value="3">Attr 3<br>
<input type="checkbox" name="attribute[]" value="4">Attr 4<br>
<input type="checkbox" name="attribute[]" value="5">Attr 5<br>
1)
$checkedAttr = $_POST['attribute'];
if(count($checkedAttr) > 0)
echo "At least one checkbox is selected";
2)
$checkboxList = implode(',', $checkedAttr);
3)
$checkedAttr = explode(',', $yourQueryResultStringContainingTheCheckedList);
<input type="checkbox" name="attribute[]" value="1" <?php if(in_array('1', $checkedAttr)) echo 'checked=\"checked\"'; ?>Attr 1<br>
...
1, 2) You can treat form elements as arrays with name="attribute[]" then loop through the posted values in your php as an array.
For example:
<?php
$attributes = $_POST['attribute'];
if(empty($attributes)) {
echo "No attributes selected";
} else {
// echo whole array
print_r($attributes);
// loop through array
foreach($attributes as $attribute) {
echo $attribute." ";
}
// create list as one whole string
$list = implode(',', $attributes);
}
?>
3) When you are building the form (using php) you can check each value in a loop. Note that I also made your labels proper labels so they will also activate the checkbox if clicked.
<?php
// need some code to get db values into array
$attributes = array(1,3,5); // your list
// loop through the amount of checkboxes you want
for($i=1; $i <= 5; $i++) {
if(in_array($i, $attributs) { // check for a match with current checkbox
$checked = " checked";
} else {
$checked = "";
}
echo'<input type="checkbox" name="attribute[]" id="attribute'.$i.'" value="'.$i.'"'.$checked.'><label for="attribute'.$i.'">Attr 1</label><br>'
}
?>
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"
My array of checkbox inputs is displaying and working perfect:
<input name="seminar[<?php echo $a; ?>]" type="checkbox" id="seminar_<?php echo $a; ?> "value="" <?php echo $checked; ?>>
This is my MySQL insert data ($s):
if ($seminar) {
foreach ($seminar as $s )
$interest .= "$s ";
}
My earlier for loop that processes the checkbox inputs above, does not store the data in $s. And I am trying to find a way to store those values to pass to that foreach.
<input type="checkbox" name="apple[]" value="1">
<input type="checkbox" name="apple[]" value="2">
<input type="checkbox" name="apple[]" value="3">
your checkbox is something like this...............ok.
then in php you just post the variable
<?php $apple=$_POST['apple'];
?>
now the $apple is an array of your selected values.
is it ok for your project.....................