Please someone help me..i want to delete multiple items with check box selection in code igniter..
what i did: My view page looks like this
<script type="text/javascript" src="<?php echo base_url(); ?>/js/jquery_1_8_1_min.js"></script>
<script language="javascript">
$(function(){
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
</script>
<?php
foreach($query as $row)
{
?>
<tr><td><input name="checkbox[]" class="case" type="checkbox" value="<?php print $row->reg_id; ?>"></td></tr>
<?php
}
?>
My controller code:
function del($reg_id) //delete content
{
$this->load->helper(array('form','url'));
$this->load->model('registration');
$query=$this->registration->delete($reg_id);
$data['query']=$this->registration->cr_getall();
$this->load->view('vw_register',$data);
}
Here my model:
public function del($reg_id)
{
$this->load->database();
$del=$this->db->delete('tbl_contctform',array('reg_id'=>$reg_id));
if($del)
{
return $del;
}
else
{
return false;
}
}
I know i done some mistakes.but i dont knw how to clear that.
is it possible to use ajax .?Please help me.
Try this,
$(".case").on('click',function(){
var chk=$('.case:checked').length ? true : false;// if length is > 0 then return true
$('#selectall').prop('checked',chk);// if chk true then check the selectAll checkbox
});
Take a look on fiddle
You need to create a form submit and call the function of controller on form submit
# Your Controller Function
function del($reg_id) //delete content
{
$this->load->helper(array('form','url'));
$this->load->model('registration');
$checkbox=$this->input->post('checkbox');# Using Form POST method you can use whatever you want like GET
$reg_id=$this->input->post('reg_id');# Using Form POST method
$regId_array=array();
foreach($checkbox as $key=>$checked)
{
if($checked!==FALSE)
$regId_array[]=$reg_id[$key];
}
if(!empty($regId_array))# array of checked reg_id to delete
{
$query=$this->registration->del($checkbox,$id);# use del here not delete as your model says
}
else{
die('No checkbox selected!!!');
}
$data['query']=$this->registration->cr_getall();
$this->load->view('vw_register',$data);
}
# Your Model Function
public function del($reg_id)
{
$this->load->database();
$this->db->where_in('reg_id', $reg_id);# delete all reg_id in $reg_id array variable
$this->db->delete('tbl_contctform');
if($del)
{
return $del;
}
else
{
return false;
}
}
And your View should be like,
<?php
foreach($query as $row)
{
?>
<tr><td>
<input name="checkbox[]" class="case" type="checkbox" />
<input name="reg_id[]" type="hidden" value="<?php print $row->reg_id; ?>">
</td></tr>
<?php
}
?>
I changed my View,Controller,Model like this..now it working properly
View:
<form name="forms" action="delete_checkbox/" method="post">
foreach($query as $row)
{?>
<tr>
<td><input type="checkbox" name="forms[]" id="forms[]"
value="<?php print $row->reg_id; ?>"/>
</tr?
<?php } ?>
Controller:
function delete_checkbox() {
$dat = $this->input->post('forms');
for ($i = 0; $i < sizeof($dat); $i++) {
print_r($dat[$i]);
$this->load->model('registration');
$this->registration->delete_check($dat[$i]);
}
redirect('viewreg/showall', 'refresh');
}
It works!!
Related
In my controller I have the code:
<?php
class Cat_cntrl extends CI_controller{
public function __construct(){
parent::__construct();
$this->load->model('cat_model');
$this->load->helper('form','url');
}
public function index(){
$get=$this->cat_model->fetchcatdata();
$data['result']=$get;
$this->load->view('cat_view',$data);
}
public function subcatselect(){
$cate=$this->input->post('cate');
$get=$this->cat_model->getsubo($cate);
$data['result']=$get;
echo json_encode(array('result'=>$data['result']));
}
}
?>
in model I've code:
<?php
class Cat_model extends CI_model{
public function fetchcatdata(){
$this->db->select()->from('cat');
$query=$this->db->get();
if($query->num_rows() > 0){
return $query->result();
}
else {
echo "error";
}
}
public function getsubo($cate){
$this->db->select($cate)->from('sub_cat');
$query=$this->db->get();
//echo $this->db->last_query();
if($query->num_rows() > 0){
return $query->result();
}
else {
echo "error";
}
}
}
In view the code i have:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <title></title>
</head>
<body>
<form method="post" action="">
<table>
<tr>
<td>Category</td>
<td><select id="cate">
<option>
None Selected
</option>
<?php foreach ($result as $value) {
?>
<option id="val">
<?php echo $value->category; ?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Sub Category</td>
<td><select id="myselect">
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="" id="sub">
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
$('#sub').click(function(e){
e.preventDefault();
var cate=$( "#cate option:selected").val();
var url="<?php echo base_url();?>cat_cntrl/subcatselect";
$.ajax({
type:'post',
url:url,
data:{cate :cate},
success:function(response){
}
})
})
})
</script>
The problem is how to auto populate the select through ajax in this code. What to use after success function? I'm not getting exact answer from Google for my situation.
Inside your success function your just have to loop through array which you receive as response and append option to select
$("#your_select_id").append($('<option>', {
value: 'somevalue',
text: 'some text'
}));
For example something like below
dataType: "json",
success:function(response){
$.each(response.result,function(i, item){
$("#myselect").append($('<option>', {
value: item[cate],
text: item[cate]
}));
});
}
Say if your response json looks like below, below should work, you have to decide how you send data to browser and manipulate it, below one is example for your to start with, on click it creates dropdown option, same way you can implement with ajax
var response = {
result: [{
value: 'val1',
text: 'text1'
},
{
value: 'val2',
text: 'text2'
}
]
}
$('#btn').click(function() {
$.each(response.result, function(i, item) {
$('#myselect').append($('<option>', {
value: item.value,
text: item.text
}));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="myselect"></select>
<input type="button" value="click me to create option" id="btn" />
Also value attribute is missing
<?php foreach ($result as $value):?>
<option value="<?php echo $value->category; ?>">
<?php echo $value->category; ?>
</option>
<?php endforeach;?>
i have a problem with checkbox . i want to check all when the first checkbox is checked. this is my code
<tr>
<td><label for="">Pilih Pendidikan</label></td>
<td style="text-align:left;">
<input type="checkbox" id="pilih_semua_pdk">Pilih Semua
<?php foreach ($query2 as $row2) {?>
<input class="form-control required check_pdk" type="checkbox" name="pendidikan[]" value="<?php echo $row2[id_pendidikan]; ?>"
<?php
$pendidikan = $_POST[pendidikan];
if ($pendidikan !=NULL) {
foreach($pendidikan as $pendidikan){
if ($pendidikan == $row2[id_pendidikan]) {
echo "checked";
}
}
}
?>
><?php echo $row2[nama_pendidikan]; ?>
<?php } ?>
</td>
</tr>
i try this code but doesnt work
<script>
$("#pilih_semua_pdk").click(function () {
$(".check_pdk").prop('checked', $(this).prop('checked'));
</script>
Try this, you need to bind an click event on first checkbox. On click of the first checkbox check if that is checked!. If yes then find all checkbox to get going.
$('#pilih_semua_pdk').on('click', function(){
if( $(this).is(':checked'))
{
//find parent td and its all checkboxes excluding first
$(this).parent().find('input[type=checkbox]').not('#pilih_semua_pdk').each(function(){
$(this).prop('checked', true);
});
}
});
jQuery makes this a piece of cake.
$(document).ready(function(){
// DOM has been loaded, attach click event for first checkbox
$('#pilih_semua_pdk').on('click', function(){
// verify if it is checked
if($(this).is(':checked'))
{
// check/tick the remaining checkboxes of the same td
$(this).siblings('.check_pdk').prop('checked', 'checked');
}
else
{
// do something when the first checkbox is unchecked
}
});
});
how can i one to one change the value of the next input?
this code doing check all or unchec all, but i want to check or uncheck one to one,
jQuery(document).ready(function() {
jQuery('#topluekle').click(function() {
if(jQuery(this).attr('checked')) {
jQuery('input:checkbox').attr('checked',true);
jQuery('input:text').attr('value','E');
} else {
jQuery('input:checkbox').attr('checked',false);
jQuery('input:text').attr('value','H');
}
});
});
Sample code:
<form>
<? for($i=1;$i<=5;$i++) { ?>
<input type="checkbox" id="pgun[]" name="pgun[]">
<input size="1" type="text" name="degerler[]" id="degerler[]" value="H">
<br />
<? } ?>
<label class="checkbox">
<input type="checkbox" value="E" name="topluekle" id="topluekle">
Check / Uncheck All *
</label>
</form>
Try
jQuery(function ($) {
$('#topluekle').click(function () {
$('input[name="pgun[]"]').prop('checked', this.checked);
$('input[name="degerler[]"]').val(this.checked ? 'E' : 'H');
});
});
Use val() like,
jQuery('input:text').val('H');
and use prop() in place of attr() like
jQuery(document).ready(function() {
jQuery('#topluekle').click(function() {
if(jQuery(this).prop('checked')) {
jQuery('input:checkbox').prop('checked',true);
jQuery('input:text').val('E');
} else {
jQuery('input:checkbox').prop('checked',false);
jQuery('input:text').val('H');
}
});
});
If you want to change value for each checkbox click then you can try,
jQuery(document).ready(function() {
jQuery('input[name="pgun[]"]').click(function() {
var newVal=$(this).next('input:text').val()=='E' ? 'H' : 'E';
$(this).next('input:text').val(newVal);
});
});
It will change corresponding text box's value of checkbox
jQuery(document).ready(function() {
var txtObj = $('input:text');
jQuery('input:checkbox').each(function(i){
jQuery(this).click(function(){
if(jQuery(this).attr('checked')) {
$(txtObj[i]).val("E");
} else {
$(txtObj[i]).val("H");
}
});
});
});
I want to make live search using codeigniter and jquery and mysql
but when i type something the result's not showing
here is my model code :
<?php
class artikel_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function cari_artikel($q)
{
$this->db->select('judul,contain');
$this->db->like('judul', $q);
$this->db->or_like('contain', $q);
$query = $this->db->get('artikel');
return $query->result_array();
}
}
?>
and here is my controller code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('artikel_model');
}
public function index()
{
if(isset($_GET['q']) && $_GET['q']){
$q = $this->input->get('q');
$this->data['data']=$this->artikel_model->cari_artikel($q);
}
else{
$this->data['data']=array_fill_keys(array('judul', 'contain'), NULL);
}
$this->data['body']='dashboard';
$this->load->view('welcome_message',$this->data);
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
this is my view code
<?php $this->load->helper('html');?>
<div class="row">
<div class="span12 offset3">
<form class="form-inline" name="form1" method="get" action="">
Search : <input type="text" class=span5 name="q" id="q"/> <label for="mySubmit" class="btn btn-primary"><i class="icon-search icon-white"></i></label> <input id="mySubmit" type="submit" value="Go" class="hidden" />
</form>
</div>
</div>
<?php echo br(15); ?>
<div id="result"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
var allow = true;
$(document).ready(function(){
$("#q").keypress(function(e){
if(e.which == '13'){
e.preventDefault();
loadData();
}else if($(this).val().length >= 2){
loadData();
}
});
});
function loadData(){
if(allow){
allow = false;
$("#result").html('loading...');
$.ajax({
url:'http://localhost/helpdesk?q='+escape($("#q").val()),
success: function (data){
$("#result").html(data);
allow = true;
}
});
}
}
</script>
<?php
foreach($data as $row){ ?>
<h3><?php echo $row['judul']; ?></h3>
<?php } ?>
The live search is not working.
when i type something the output just "loading..."
and the result is not showing..
If i press enter, the result will show. but that is just the usual searching method. Not live searching.
Make a new function say "live_search" that will only return the result with the ajax search and not the "index" function. This new function will return the result with the HTML markup.
public function live_search(){
if($this->input->post(null)){
//put your search code here and just "echo" it out
}
}
$.ajax({
url:'http://localhost/domain/controller/live_search',
type : 'POST',
data : { 'q' : escape($("#q").val()) },
success: function (data){
$("#result").html(data);
allow = true;
}
});
Hope it helps you.
I want to alert user if they haven't checked any checkbox, and return to the form
After they checked at least one checkbox, proceed into the next page
How can I do that ?
<input type="checkbox" name="pay[]" value="
<?php
echo $DateArr[$record_count].";";
echo $invoiceArr[$record_count].";";
echo $TotalArr[$record_count].";";
echo $amount_dueArr[$record_count];
?>
" onClick="checkTotal()"/>
<td ALIGN="right" ><input type="submit" name="submit" value="Continue" onMouseOver="CheckBoxStatus()"></td>
javascript :
function CheckBoxStatus()
{
var checkbox_status = document.getElementsByName("pay[]").value;
if (checkbox_status = ";;;")
{
alert("Please select at least one to proceed!");
return false;
}
else
{
alert("Continue!");
return true;
}
}
If user hasn't checked, at least one checkbox, alert the message and return to form
Then
<form name="payform" method="POST" action="payment.php" onSubmit=" CheckBoxStatus()">
How can I stay in the form if user is not checking any checkboxes ?
May be like this :
function CheckBoxStatus()
{
if (document.getElementsByName("pay[]").checked == true)
{
document.forms["payform"].submit();
return true;
}
else
{
alert("Please select at least one invoice to proceed!");
document.forms["payform"].reset();
return false;
}
}
can i do like this :
`
function CheckBoxStatus()
{
var status=$("pay[]").attr('checked');
if (status=true)
{
//if(document.getElementsByName("pay[]").checked) {
return true;
}
else {
alert("Please select at least one to proceed!");
return false;
}
}`
With jQuery
var $checked = $("input:checked");
if($checked.length == 0) {
alert("Please select at least one to proceed!");
}
That HTML is a bit of a mess. A couple of things...
Get rid of the <td> tag containing the button. It doesn't look like it has a purpose.
To call the JavaScript you need to use onsubmit not onMouseOver.
To check if the checkbox is checked change your function to the following.
function checkBoxStatus {
if(document.getElementsByName("pay[]").checked) {
return true;
}
else {
alert("Please select at least one to proceed!");
return false;
}
}
EDIT; forgot the alert
Check if you have included jquery. :|
Try this:
<input id="ipay" type="checkbox" name="pay[]" value="
<?php
echo $DateArr[$record_count].";";
echo $invoiceArr[$record_count].";";
echo $TotalArr[$record_count].";";
echo $amount_dueArr[$record_count];
?>"
/>
<td ALIGN="right" ><input type="submit" name="submit" value="Continue"></td>
<form id="pform" name="payform" method="POST" action="payment.php">
Javascript:
$(document).ready(function() {
$('#pform').submit(function(e) {
var paycheck = $('#ipay').attr('checked');
if(paycheck != true or paycheck != 'checked') {
alert('Please check at least one option.');
e.preventDefault();
}
});
});
You can do something like this:
<html>
<head>
<title>Verify Checkbox</title>
<script type="text/javascript">
function verifyCheckboxSelected(form) {
var theForm = document.forms[form];
var valid = false;
for( var i=0; i < theForm.length; i++ ) {
if(theForm.elements[i].checked) {
valid = true;
}
}
if (!valid) {
alert('You must select at least one checkbox')
return false;
}
}
</script>
</head>
<body>
<form name="myForm" onSubmit="return verifyCheckboxSelected(this.name)">
<input type="checkbox" name="myCheckBoxes[]"/>Checkbox A</br>
<input type="checkbox" name="myCheckBoxes[]"/>Checkbox B</br>
<input type="checkbox" name="myCheckBoxes[]"/>Checkbox C</br>
<input type="submit" value="submit" />
</form>
</body>
</html>