Delete multiple rows using checkbox in Codeigniter - php

I have a table, which is fetching data from database correctly, Now I want to add a checkbox in front of each row in the table and submit button above the table. When I check on some checkboxes, I want to delete those rows, I was trying this code to get the ids of the selected rows, but I get nothing when I press the submit, Can you please help me out?
My View file:
<?= form_open('user/set_archive'); ?>
<input type="submit" value="Submit">
<table>
<thead>
<td>Action</td>
<td>From</td>
<td>Subject</td>
<td>Date</td>
</thead>
<tbody>
<?php foreach ($emails as $email) : ?>
<tr class="read">
<td>
<input type="checkbox" name='archiveval[]' id="ch<?= $email['id']; ?>" value="<?= $email['id']; ?>" >
<label for="ch<?= $email['id']; ?>"></label>
</td>
<td>....</td>
<td>....</td>
<td>....</td>
</tr>
<?php endforeach; ?>
<?= form_close(); ?>
</tbody>
</table>
My Controller user/set_archive
public function set_archive()
{
$data1 = $this->input->post('archiveval');
echo "<pre>";
print_r($data1);
}

I believe your assignment to id is incorrect. You can get ids of the rows selected like this
<input type="checkbox" name='archiveval[]' id="archiveval[]" value="<?= $email['id']; ?>" >
Give it a shot

Related

Why I do not get radio button value with php?

I´m passing a value of a radio button in a page with a form and I want to get the value of the selected option in another php file, but I can´t get that value.
First page:
<form class="itemSelection" action="../php/itemSelection1Save.php"><br><br>
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" id="selectedItem" required="" value="<?php echo $row[0] ?>"></td>
<td><?php
echo row[0]</td>
</tr>
<?php
}
?>
</tbody>
</table><br>
<input type="submit" value="Next"/>
</form>
Second file:
$_SESSION['selectedItem1'] = filter_input(INPUT_POST, 'selectedItem');
filter_input(INPUT_POST, 'selectedItem')
returns nothing
You form is with method get (the default method if not specified):
<form class="itemSelection" action="../php/itemSelection1Save.php">
insert method='post':
<form class="itemSelection" action="../php/itemSelection1Save.php" method='post'>
Make the following changes in HTML part , add method="post"
<form class="itemSelection" action="../php/itemSelection1Save.php" method="post">
Make the following change in PHP part (optional):
Also add filter_option in filter_input. filter the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering.
filter_input(INPUT_POST, 'selectedItem',filter_option)
You Need To Add The Form Method you can try my code i simply code for you hopefully it will work!
<form class="itemSelection" method="post" action="../php/itemSelection1Save.php">
<h1>Recommended Item(s)</h1>
<table class="itemTableRecom">
<thead>
<tr>
<th>Selected Item Type</th>
<th>Recommended Item Type</th>
</tr>
</thead>
<tbody>
<?php
foreach ($_SESSION['itemsInfo'] as $row) {
?>
<tr>
<td><input type="radio" name="selectedItem" value="<?php echo $row[0]; ?>"></td>
<td><?php echo $row[0]; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input type="submit" name="submit" value="Next">
</form>

Trying to update a row (table) using codeigniter

Im trying without success to update a row in a table. I mean, when i click on "update" button it goes to another page and i got the previous "values" so i can edit them and finally click on "save".
But it is not working in my program :/..When i click on "edit" button it goes to another page and i only have the previous values of a single field (Field subject). AND when i click on save it does not update anything, it creates a new row.
Here is my code:
The index file, here i put the button "edit".
<a href='".site_url('Home/editar')."/$record->id'>
<button type='button' class='btn btn-primary'>EDIT</button></a>
My controller file, with the function "editar" and "saveupdate":
public function editar($id){
$data['carreras'] = $this->Crudmodel->get_carreras();
$data['record']=$this->Crudmodel->get_id_row($id);
$this->load->view('editar',$data);
}
public function saveupdate(){
$id=$this->input->post("txtid");
$txtcarr=$this->input->post("txtcarr");
$txtmat=$this->input->post("txtmat");
$txtdesc=$this->input->post("txtdesc");
$txtcarga=$this->input->post("txtcarga");
$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga);
$this->db->where("id", $id);
redirect('Home/index');
}
The Crudmodel with the function "saveup"
public function saveup($txtcarr, $txtmat, $txtdesc, $txtcarga){
$data=array(
'carrera_id'=>$txtcarr,
'nombre'=>$txtmat,
'descripcion'=>$txtdesc,
'carga_horaria'=>$txtcarga
);
$this->db->update('materias', $data);
}
Here is the get_id_row from crudmodel:
Function
Finally, the "editar" file which contains all the fields.
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">UPDATE SUBJECTS</h2>
<form method="post" action='<?php echo site_url('Home/saveupdate'); ?>'>
<tr>
<td><input type="text" hidden name="txtid" value="<?php echo $record->id ?>"/></td>
</tr>
<tr>
<td>
<select name="txtcarr">
<?php foreach($carreras as $item):?>
<option value="<?php echo $item->id;?>"><?php echo $item->nombre;?></option>
<?php endforeach;?>
</select>
</td>
</tr>
<tr>
<td>Subject : </td>
<td><input type="text" name="txtmat" value="<?php echo $record->nombre ?>"/></td>
</tr>
<tr>
<td>Description : </td>
<td><textarea name="txtdesc" value="<?php echo $record->descripcion ?>"></textarea></td>
</tr>
<tr>
<td>Hours : </td>
<td><input type="text" name="txtcarga"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Save"/></td>
</tr>
<table class="table table-hover" align="center" border="0" cellspacing="0" cellpadding="0" width="300">
</table>
</div>
</div>
</div>
Do not understand what should i do :S
I think Rushabh thinks you should pass your $id with your data to the model like this:
$this->Crudmodel->saveup($txtcarr,$txtmat,$txtdesc,$txtcarga, $id);
and in your model you can:
$this->db->where("id", $id);
$this->db->update('materias', $data);
and don't forget to remove your where condition in the controller;
Edit:
your get_id_row() function needs to be:
public function get_id_row($id){
$query = $this->db->get_where('your_table_name', array('id' => $id));
return $query->row();
}

i want to retrieve selected row data in textboxes in html using jquery

i have used html table to display data from database using PHP. I have id,client name ,staff name and matter columns in database and i am displaying only client name, staff name and matter columns in the html table.i have three textbox for client name ,staff name, matter .now, i want to display the selected row's data in the three textbox respectively.
$(document).ready(function () {
$('#tableresult tr').click(function (event) {
alert(this.id); //trying to alert id of the clicked row
});
});
i tried this ,still not working-
$('#editclientname').val($(this).attr('client_name'));
html markups-
<label for="Client Name">Client Name</label><br />
<input type="text" id="editclientname" name="editclientname" />
</div>
<div class="col-md-2">
<label for="Staff Name">Staff Name</label><br />
<input type="text" id="editstaffname" name="editstaffname" />
</div>
<div class="col-md-2">
<label for="Matter">Matter</label><br />
<input type="text" id="editmatter" name="editmatter" />
</div>
table and php code-
<table class="footable" data-filter="#filter" id="tableresult">
<thead>
<tr>
<th>Client Name</th>
<th>Staff Name</th>
<th>Matter</th>
</tr>
</thead>
<tbody>
<?php
include_once 'db.php';
$sql = "SELECT * FROM newdata";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)):; ?>
<tr>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[4]; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
I would recommend to use .each() with the index as parameter as:
$('#tableresult tr').click(function(event) {
$('td', this).each(function(i) {
$('.inputWrapper input').eq(i).val(this.textContent);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='inputWrapper'>
<input type='text'>
<input type='text'>
<input type='text'>
</div>
<table id='tableresult'>
<tr>
<td>foo</td>
<td>bar</td>
<td>baz</td>
</tr>
</table>
Another alternative to the answer:
$('#tableresult tr').click(function(event) {
$(this).find("td").each(function(index) {
$($("input").get(index)).val($(this).text());
});
});
Here is the JSFiddle demo :D

How to pass a variable to another page in php using GET or POST method?

I am making an editing form.
There are 2 forms, one form called edit1.php and another form called edit.php. This second one should get the id generated from the edit1.php and use it to generate further queries. The following is the code for the 2 files:
edit1.php:
<table>
<tr>
<td align="center"><h4><b>EDIT DATA</b></h4></td>
</tr>
<tr>
<td>
<table border="3">
<form action="edit.php" method="get">
<?php
$vendors = "SELECT `vendor`.`v_id`,`vendor`.`v_name` FROM `stfood`.`vendor`";
$result = mysqli_query($con, $vendors);
while ($row = mysqli_fetch_array($result)){
echo ("<tr><td>'".$row["v_id"]."'</td>");
echo ("<td>'".$row["v_name"]."'</td>");
echo ("<td>Edit</td></tr>");
}
?>
</form>
</table>
edit.php:
<?php
require 'init.php';
require 'functions.php';
?>
<html>
<head>
<title>Form Edit Data</title>
</head>
<body>
<table border=1>
<tr>
<td align=center>Form Edit vendor Data</td>
</tr>
<tr>
<td>
<table>
<?php
echo $_REQUEST['v_id'];
$vendor = "SELECT `vendor`.`v_name`, `vendor`.`v_email`,`vendor`.`v_desc`,`vendor`.`cont_id` FROM `stfood`.`vendor` WHERE `vendor`.`v_id` = '".$v_id."'";
$vendor_result = mysqli_query($con, $vendor);
$row = mysqli_fetch_array($vendor_result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo $row["v_id"];?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name" size="20" value="<?php echo $row["v_name"]; ?>">
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="text" name="email" size="50" value="<?php echo $row["v_email"];?>">
</td>
</tr>
<tr>
<td>Vendor Description</td>
<td>
<input type="text" name="description" size="100" value="<?php echo $row["v_desc"];?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>
When I run the code, the first form displays all the relevant data, and when I click on the edit link the page gets redirected and I can see the v_id passed in the URL, but nothing comes into the edit.php file. When I do a var_dump($row['v_id']); I get NULL.
Why is v_id not set in edit.php?
Since your hyperlink looks like this:
Edit
Your edit.php should use
echo $_GET['id'];
// or something like this
$received_id = $_GET['id'];
// do validation whether you received a good id or not and then move on
...
...
<input type="hidden" name="id" value="<?php echo $received_id;?>">
on your edit.php page you have:
Edit
now on your edit1.php you should first have a isset() and then as follow:
if(isset($_GET['id']){
$id = $_GET['id'];
//carry on blah3 ...
In the edit1.php you have not used form elements. The value $row['v_Id'] should be the value of form element input like this inside form
<input type='hidden' name='v_I'd value ='<?php echo $row['v_id'] ?>'>
Try using $_GET['id'] instead of $_REQUEST['v_id'] on edit.php
See example #1 here
Also, have you defined the $v_id before using it in the query?
Like this.

Checkbox array returning NULL

I want a batch deletion feature in the billing system I am developing, however the checkboxes, when var_dumped yields a NULL value when checked.
Here is my markup (edited):
<form method="post" name="form-countries-del">
<div id="deleteBox">
<span class="middleAlign"><b>No. of Records:</b> <span class="red"><?= $qtotal; ?></span></span>
<span class="rightAlign">
<input type="submit" value="Delete" name="submit-countries-del" class="del-submit" />
</span>
</div>
<table border="1" id="src-table">
<tr class="header">
<td><input type="checkbox" id="toggleChecks"></td>
<td class="_hide">CountryID</td>
<td>Country Code</td>
<td>Country Name</td>
<td>Telco Rate</td>
</tr>
<?php while($row = $pdo->fetch()) : ?>
<tr class="<?=($c++%2==1) ? 'even' : 'odd' ?>" title="Double click to edit">
<td><input type="checkbox" name="toDelete[]" id="toDelete[]" class="toDelete" value="<?= $row['CountryID']; ?>" /></td>
<td class="_hide"><?= $row['CountryID']; ?></td>
<td><?= highlight($_POST['search'], $row['CountryCode']); ?></td>
<td><?= highlight($_POST['search'], $row['CountryName']); ?></td>
<td><?= $row['Trate']; ?></td>
</tr>
<?php endwhile; ?>
</table>
</form>
The generated form (Google Chrome) is like this:
It just yields NULL unfortunately. I wonder what I've been missing. I've been scanning a whole lot of similar articles regarding my problem but none has worked thus far.
I hope you folks can help me!
Cheers!
Until you check the checkbox you will get the value as NULL, Once you checked the checkbox and try with post method you will retrieve the value.
You have bad html.
Move the <form method="post" name="form-countries-del"> to before <div id="deleteBox">
Your form close(</form>) not defined in properly.
Your html code will generate like this, <span> <form> ......(without ending form) </span>
The Html parse the If any unclosed tags presents it will automatically added closing tag inbetween.
like this,
<span >
<form>
<!-- Submit button -->
</form> <!-- added automatically -->
</span>
<!-- Check box elements goes here -->
Some MVC frameworks mess around with the request variables such as POST. It's possible this is the case with you. As your submit button is posting ok, it could be just a problem with POSTing arrays. Try this to get around that problem.
<input type="checkbox" name="toDelete-<?= $row['CountryID']; ?>" id="toDelete[]" class="toDelete" value="<?= $row['CountryID']; ?>" /></td>
<td class="_hide"><?= $row['CountryID']; ?>
You can access the values like so:
$toDelete = array();
foreach($_POST as $p){
$ex = explode('-',$p);
if($ex[0]=='toDelete'){
$toDelete[] = $ex[1];
}
}

Categories