我想编辑选定列的状态字段 onclick of bootstrap checkbox toggle .....Please Help 示例: 1。如果状态为事件状态,则当用户单击时,它应该在数据库中更新为非事件状态并重新加载页面。 2。如果状态为非事件状态,那么当用户单击时,它应该在数据库中更新为事件状态并重新加载页面。
获取数据库
<?php
require_once 'db_config.php';
$output = array('data' => array());
// do not fetch status 3 because it is deleted
$sql = "SELECT *,emailnotificationstable.id as sid FROM notificationslist";
$query = $connect->query($sql);
$num_rows = mysqli_num_rows($query);
$x = $num_rows;
while ($row = $query->fetch_assoc()) {
// activate button
$activateButton = '';
if ($row['status'] == 1) {
$activateButton =
'<input type="checkbox" id="toggleBtn" name="toggleBtn" checked data-toggle="toggle" data-on="Active" data-off="Inactive" data-onstyle="success" data-offstyle="danger" data-size="mini" value="'.$row['sid'].'" onclick="editMember()">';
} elseif ($row['status'] == 2) {
$activateButton =
'<input type="checkbox" id="toggleBtn" name="toggleBtn" data-toggle="toggle" data-on="Active" data-off="Inactive" data-onstyle="success" data-offstyle="danger" data-size="mini" value="'.$row['sid'].'" onclick="editMember()">';
}
// extra code here
$output['data'][] = array(
$x,
$activateButton,
$row['date'],
$row['notificationname'],
$row['employeename'],
$createdby,
$editedby,
$editeddate,
$deleteButton,
);
$x--;
}
// database connection close
$connect->close();
echo json_encode($output);
Jquery
// global the manage memeber table
var mytable;
$(document).ready(function() {
mytable = $("#mytable").DataTable({
"ajax": "../pages/php_action/salesexe/retriveemailnotifications.php",
"order": [],
"fnDrawCallback": function() {
jQuery('#mytable #adBtn').bootstrapToggle();
}
});
});
function editMember(sid = null) {
if(sid) {
// remove the error
$(".form-group").removeClass('has-error').removeClass('has-success');
$(".text-danger").remove();
// empty the message div
$(".edit-messages").html("");
// remove the id
$("#membersid").remove();
// click on toggle button
$("#adBtn").click(function() {
$.ajax({
url: 'notifstatus.php',
type: 'post',
data: {membersid : sid},
dataType: 'json',
success:function(response) {
if(response.success == true) {
$(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
'<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.messages+
'</div>');
// refresh the table
mytable.ajax.reload(null, false);
} else {
$(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
'<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.messages+
'</div>');
}
}
});
}); // click toggle btn
} else {
alert('Error: Refresh the page again');
}
}
更新
<?php
require_once 'db_config.php';
$output = array('success' => false, 'messages' => array());
$membersId = $_POST['membersid'];
// update record to inactive
$sql = "UPDATE notificationslist SET notfistatus = '2' WHERE id = $membersId";
$query = $connect->query($sql);
if($query === TRUE) {
$output['success'] = true;
$output['messages'] = 'Notification trigger successfullt activated for selected user';
} else {
$output['success'] = false;
$output['messages'] = 'Error while activating notification trigger for selected user,';
}
// close database connection
$connect->close();
echo json_encode($output);
最佳答案
您正在考虑重新加载页面的复杂方法,而不是您应该使用的 API。您只想获取状态 并更新所需的部分,这意味着您需要一种正确的方法来识别表中的行。此外,html id 在页面中必须是唯一的,最好将其删除。
你有几个选择:
下图是后者:
$(function() {
// hooking event only on buttons, can do tr's as well.
$('.toggleBtn').click(function(){
$.ajax({
url: 'notifstatus.php',
type: 'post',
data: {
id : $(this).val(),
status: $(this).prop('checked')
},
dataType: 'json',
success: function(response){
if(response.success){
$(this).bootstrapToggle('enable');
console.log(response.message);
} else {
$(this).bootstrapToggle('disable');
BootstrapDialog.show({
title: 'failed to update status',
message: response.status + response.messages
});
}
},
error: function(xhr, textStatus, errorThrown) {
BootstrapDialog.show({
title: textStatus,
message: errorThrown
});
}
});
});
});
现在对构建的唯一响应如下:
<?php
require_once 'db_config.php';
if(isset($_POST['status']) && $_POST['status']){
// user requests to turn off
$sql = "UPDATE notificationslist SET notfistatus = '2' WHERE id = ?"; // SQL injection, use prepared statements. fix it.
} else {
// user requests to turn on, other query.
}
$success;
$status = 0;
$message = 'Error while activating notification trigger for selected user,';
if($query = $connect->query($sql)){
if($row = $query->fetch_assoc()){
$success = true;
$message = 'Notification trigger successfully activated for selected user';
$status = $row['status'];
}
}
die(json_encode([
'success' => $success,
'messages' => $message,
'status' => $status
]));
?>
应该可以解决问题,无需请求所有数据来更新单个值。
关于php - 更新 Mysql 记录 Onclick Bootstrap Checkbox Toggle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44981196/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).
我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U
我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame
这太简单了,太荒谬了,我在任何地方都找不到关于它的任何信息,包括API文档和Rails源代码:我有一个:belongs_to关联,我开始理解当您没有关联时您在Controller中调用的正常模型方法与您有关联时调用的方法略有不同。例如,我的关联在创建Controller操作时运行良好:@user=current_user@building=Building.new(params[:building])respond_todo|format|if@user.buildings.create(params[:building])#etcetera但我找不到关于更新如何工作的文档:@user
我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr