I have been searching on this topic and the more I read the more confused I get. I hope you can help me on this.
My objective is to add checkboxes to a table in order to be able to delete the rows I select from that table. So my code til now is like this:
if ($arch = $pdo->prepare("SELECT name, age FROM table WHERE id = ?)) {
$arch ->execute(array($id));
$data = $arch->fetchAll();
echo '<div class="coolTable" ><table><tr><td>Name</td><td>Age</td><td>Check</td></tr>';
foreach ($data as $row){
echo '<tr>';
foreach ($row as $col){
$col=nl2br($col);
echo '<td>'.$col.'</td>';
}
echo '<td><input type="checkbox" name="checkbox" value="" id="checkbox"></td>';
echo '</tr>';
}
echo '</table></div>';
}
With this, I have all my checkboxes in place. But now, how can I submit the checkboxes with a Post so I can Delete the rows I checked?
I suppose I can use an array to name each checkbox? but don`t know how :-s
Thanks in advance for your help!
Change this line:
echo '<td><input type="checkbox" name="checkbox" value="" id="checkbox"></td>';
with
echo '<td><input type="checkbox" name="checkbox[]" value="" id="checkbox"></td>';
You can access every value of checked checkbox using this array $_POST['checkbox'] if you are using POST method or $_GET['checkbox'] for GET method.
PS: You should give your checkbox's a value.
Related
I have tried searched, but I am missing somthing or not phrasing myself correctly I guess.
php, sql and Codeigniter.
What I have is a list of reports in a database.
Theese have a column that states if they are "active" 1 or 0.
What I want to do is add a checkbox on the list that prints the rows in my view as that I can select and then when I have selected the rows I want to be able to press a button and by sending form data change the selected rows in the database to a 1 or 0.
example of how I am thinking of presenting it:
echo "<form action='' method='POST'><table>";
foreach($reports->result() as $row)
{
echo "<tr>";
echo "<td><input type="checkbox" name=""></td>;
echo "<td>".$row->name."</td>";
echo "<td>".$row->time."</td>";
echo "<td>".$row->spot."</td>";
echo "<td>".$row->priority."</td>";
echo "</tr>";
}
echo "<tr><td colspan='5'><input type='submit'></td></tr>";
echo "</table></form>";
What would be the best is if I could get the checkboxes I select into a array for example that would be lovely.
I have tried with array_push, but I could not find a way to select only the ones I selected to send. Is there some way to do this that I dont know of?
I have found a way to make the selected checkboxes into an array by adding id[] to all checkboxes name like this this:
form_checkbox('id[]',$row->id, FALSE);
I loop out alot of these and might get this for example:
<input type="checkbox" name="id[]" value="12340">
<input type="checkbox" name="id[]" value="12353">
<input type="checkbox" name="id[]" value="12367">
<input type="checkbox" name="id[]" value="12389">
<input type="checkbox" name="id[]" value="12391">
Then in my controller I added this where my form takes action to and loops through the ones that I have checked and take action on them.
$arr = array();
if(!empty($_POST['id'])) {
foreach($_POST['id'] as $selected) {
$this->db->where('id', $selected);
$this->db->update('rapporter' , $data);
}
}else{}
This is working great for me. Now I only update the rows I have checked!
If someone have a more efficient way of doing this I am all ears!
Hello knowledgeable people. I am having trouble retrieving checkbox data from form. I have a site in which user can add checkboxes themselves, so I am writing them out like this:
<table style="padding:10px;">
<?php
$query_boolean = $DB->prepare("SELECT * FROM moduls WHERE type='boolean'") or die(mysql_error());
$query_boolean->execute();
while (($row = $query_boolean->fetch()) != false)
{
?>
<tr>
<td>
<?php echo $row->name ?>:
</td>
<td>
<?php
$s = "";
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="yes">%s', $row->id, Yes);
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="no">%s', $row->id, No);
echo $s;
?>
</td>
</tr>
<?php
}
?>
</table>
Now I have an advanced search in which I have to chech through every checkbox to see what has been selected (ether none, Yes, No, or both). How can I get the info from every checkbox in variables? Thank you so much!
To get POST data from checkboxes they must have attribute
checked="checked"
EDIT:
If you have 2 checkbox as this..
<input type="checkbox" checked="checked" class="textbox" name="boolean_yes" value="yes">
<input type="checkbox" class="textbox" name="boolean_no" value="no">
When you submit your form the checkbox with attribute checked will be sent as POST and the one without checked attribute will not be sent..
if(isset($_POST['search'])) {
$all_checked = array();
foreach($_POST as $key=>$value){
if(strpos($key, "boolean_") > -1){
$all_checked[$key] = $value;
}
}
var_dump($all_checked);
}
This way you will get inside $all_checked array all marked boxes.. All others checboxes are not marked!
if you want to get checkbox value then use checkbox name as array
<input type="checkbox" name="email1[]" value="">
an get it on another page by
<?php
$var2 = $_POST['email1'];
$v=implode(",",$var2);
echo $v;
?>
try it
I am trying to insert some values into a database from a form submitted by the user.
$id = mysql_insert_id();//Gets ID of last value inserted into related table
foreach( $_POST[ 'equipment' ] as $checkBoxIndex => $checkBoxValue ) {
mysql_query("INSERT INTO recipeequipment (recipeid, equipmentid, datetimeentered) VALUES('".$id."', '".$checkBoxValue."', '".$datetime."')");
}
The only thing that needs adding to this insert statement is the quantity, which is held in a similar fashion to the checkboxes, but in textboxes. like this:
<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count[]" id="count[]" size="3" value="" />'
.$rowequipment['description']."<br />";
}?>
The piece i need help with is the second input column. I need to put the values that correspond with the check boxes into the insert statement, but am unsure of how to do this.
Ignoring all the vunrabilities in your code for now, doing something like this should allow you to get a unique id and match the checkbox up to the input:
<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment['.$rowequipment['equipmentid'].']" value="'.$rowequipment['equipmentid'].'"/>
<input type="text" name="count['.$rowequipment['equipmentid'].']" id="count-'.$rowequipment['equipmentid'].'" size="3" value="" />'
.$rowequipment['description']."<br />";
}?>
Then once that form is submitted you would simply loop through each checkbox by doing something like:
foreach ($_POST['equipment'] as $id => $checkboxValue) {
$textInputValue = $_POST['count'][$id];
// do something with the above value here
}
Although, I'm not quite sure why you're using checkboxes like this - is it so you ignore updating the rows that they don't check off or something?
I`m using codeigniter to build small website. I am curious how to populate checkbox from database ?
Let`s say I have the following query;
$this->db->select('status');
$this->db->where('id', 3');
$this->db->get('table);
How to make checkbox to be checked if the result of the query above is 1 ?
You can do something like this. It's just an idea since I can't really see EXACTLY what you're returning. $entry['status'] is the result from your query.
if($entry['status'] == 1){
echo '<input type="checkbox" checked="checked" disabled="disabled"/>';
}
else {
echo '<input type="checkbox" unchecked="unchecked" disabled="disabled"/>';
}
Take a look at the form_checkbox() section of the codeigniter form helper documentation
If you want to have the select box checked if the status value is greater than 1 you could do something like this.
$result = $this->db->select('status')->get_where('table', array('id'=> 3))->row();
if($result->status > 1)
{
print '<input type="checkbox" name="checkbox" checked="checked" value="'.$result->status.'" />';
}else{
print '<input type="checkbox" name="checkbox" value="'.$result->status.'" />';
}
You'll want to check that the row actually returns values or you'll get a var on a non object error. This is just an example.
This works for me and is simple:
<?php foreach($tb as $table_row){ ?>
<input type="checkbox" <?php if ($table_row['reconciled'] == "1" {echo "checked = checked";} ?>
That title probably doesn't mean much but what I have is a form that is generated dynamically. I hooks into a table of products, pulls out there name. I then create a form that displays the product with a checkbox and textbox next to it.
<form id="twitter-feed" name="twitter-feed" action="<?php echo $this->getUrl('tweet/') ?>index/tweet" method="post">
<table><tr>
<?php
$model = Mage::getModel("optimise_twitterfeed/twitterfeed");
$products = $model->getProducts();
foreach ($products as $product){
echo '<tr>';
echo '<td>';
echo '<label for="'. $product .'">' . $product . '</label>';
echo '<br /><input type="text" class="hashtag" name="tags" id="tags" value="#enter, #product, #hastag"';
echo '</td>';
echo '<td><input type="checkbox" name="chk" id="'. $product .'"></td>';
echo '</tr>';
}
?>
<tr><td colspan="2"><input type="submit" name="submit" value="tweet"></td></tr>
</table>
</form>
As you can see there are checkboxes and textfields for each record. When I examine the $_POST data from the form it only retains fields for the last record.
Is there a way to pass all this data back to the action?
Cheers,
Jonesy
Use name="chk[]", then PHP will create an array for you.
Change your name arrtibutes to have an opening and closing square brace like this:
name="tags"
name="chk"
to
name="tags[]"
name="chk[]"
This will turn an array like:
$_POST['tags'][0] = VALUE
$_POST['tags'][1] = VALUE
$_POST['chk'][0] = VALUE
$_POST['chk'][1] = VALUE
Yes you can, set brackets at the end of the name value.
E.g.:
<input type="checkbox" name="chk[]" id="'. $product .'">
Then you get an array as result in $_POST['chk'].
Besides that, ids should always be unique. You can give same names, but you should always use different ids.
All of your fields have the same name, when that happens on any form you end up only seeing the last value because it's overwritten by the other fields.
Instead of <input type="checkbox" name="chk" id="124123"> do something like <input type="checkbox" name="chk[124123]" value='1'>
In your code you'd receive $_POST['chk'] as an array of values, only those values that were checked.
you can use as i have given example below. where i have taken one new variable $i = 0; and then you can use this $i into the foreach loop for displaying all product one-by-one..
i think this may help you.
$i = 0;
foreach ($products as $product){
echo '<td><input type="checkbox" name="chk" id="'. $product[$i] .'"></td>';
}