- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对服务器端脚本和 ajax 还很陌生。我想知道的是,我可以对同一个文件进行多个 ajax 调用,但通过我的 ajax 调用,只读取该文件中的某个脚本来执行该特定的 ajax 调用吗?我想知道这是因为当我进行 ajax 调用时,它会解析我的脚本的整个代码,它也会进行 ajax 调用,而不是我的其他 ajax 调用脚本中的特定代码。
现在,我可以通过简单地创建一个新文件并将我的 ajax 调用指向单独的文件来完全解决这个问题,但这不会被认为是糟糕的架构吗?例如,对 editusers.js 进行 1 个 ajax 调用,对 deleteusers.js 进行另一个 ajax 调用,为什么不简单地将所有不同的 ajax 调用放在一个文件中?
这是我的代码,我使用引导数据表和服务器端渲染 PDO 而不是 mysqli。第一个 ajax 调用是查询表中的所有记录,第二个 ajax 调用是用于编辑表本身中的用户记录的功能。但是,我停止了编辑用户记录功能,因为我想知道我是否可以实现这一目标?
HTML 和 JavaScript
<script type="text/javascript">
$(document).ready(function() {
$('#userDetails').DataTable({
"scrollY": "400px",
"scrollCollapse": true,
"processing": true,
"serverSide": true,
"ajax": {
"url": "../user-actions-script.php",
"type": "post",
},
"deferRender": true,
oLanguage: {sProcessing: "<i class='fas fa-spinner fa-3x' id='loader'></i>"},
});
$(document).on('click', 'i.fas.fa-edit', function(e){
$.ajax({
url: '../user-actions-script.php',
success: function(){
console.log('works');
}
})
});
});
</script>
<body>
<div class="fluid-container">
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"></nav>
</header>
<div class="container">
<h2>User Actions</h2>
<div class="userdetailsContainer">
<table class="table table-dark table-striped table-bordered table-hover" id="userDetails">
<thead>
<!-- adding this in later -->
</thead>
</table>
</div>
<!-- Edit Actions Modal -->
<!-- Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="editHeader">
<i class="fas fa-edit fa-2x"></i>
<span>Edit User</span>
</div>
<div class="form-group">
<form
method="POST"
>
<label for="email">Email:</label>
<input
type="email"
class="form-control"
name="email"
>
<label for="fullName">Full Name:</label>
<input
type="text"
class="form-control"
name="fullName"
>
<label for="area">area:</label>
<select
class="form-control"
name="area"
id="area"
>
<?php include ("_global/includes/login_select_options-approved.php") ;?>
</select>
<label for="requestor">Requestor:</label>
<select
class="form-control"
name="requestor"
id="requestor"
>
<?php include ("_global/includes/select_options.php") ;?>
</select>
</div>
</form>
<button
type="submit"
class="btn btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
PHP PDO/Ajax 脚本
<?php
include "_global/includes/config.php";
// Set all fetch requests as an object by default
$dbo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
// Custom Usergroups Inputs
$admin = 'ADMIN';
$emptyValues = '';
$adminLive = 'ADMIN-live';
$adminType = 'ADMIN_US_Type';
$customusDomestic = 'US_domestic';
$adminChina = 'China_admin';
$adminIndia = 'India_admin';
$adminCustom = 'ADMIN_custom';
$customusaustinCulver = 'US_Austin_Culver';
$customuschinaTokyo = 'US_China_Tokyo';
$customcorkuaeBayarea = 'Cork_UAE_BayArea';
$customchinashanghaiBeijing = 'China_Shanghai_Beijing';
$adminselfMoves = 'ADMIN_selfmoves';
$adminmilanVienna = 'ADMIN-milan-vienna';
$custombayareaSeattle = 'Bayarea_Seattle_admin';
$custombayareaCulver = 'ADMIN_Culver_Bay';
$customculverSingapore = 'ADMIN_Culver_Singapore';
$customcanada = 'ADMIN_canada';
// Datatable serverside processing script
$request = $_REQUEST;
$columns = array(
0 => 'name',
1 => 'email',
2 => 'usergroup',
3 => 'user',
4 => 'DateAdded'
);
// Prepare the query
$sqlAll = 'SELECT mem_id, email, name, usergroup, user, DateAdded
FROM plus_signuptestdata
WHERE usergroup NOT IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
GROUP BY email
ORDER BY dateAdded DESC';
$stmt = $dbo->prepare($sqlAll);
// Execute the query
$stmt->execute([$admin,$emptyValues,$adminLive,$adminType,$customusDomestic,$adminChina,$adminIndia,$adminCustom,$customusaustinCulver,$customuschinaTokyo,$customcorkuaeBayarea,$customchinashanghaiBeijing,$adminselfMoves,$adminmilanVienna,$custombayareaSeattle,$custombayareaCulver,$customculverSingapore,$customcanada]);
// Count Rows
$rowCount = $stmt->rowCount();
// Fetch the query
$totalData = $stmt->fetchAll(PDO::FETCH_ASSOC);
$totalFiltered = $rowCount;
$sqlAll = "SELECT mem_id, email, name, usergroup, user, DateAdded ";
$sqlAll .= " FROM plus_signuptestdata ";
$sqlAll .= " WHERE usergroup NOT IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
if(!empty($request['search']['value'])){
$sqlAll .= " AND name LIKE '%".$request['search']['value']."%' ";
$sqlAll .= " OR email LIKE '%".$request['search']['value']."%'";
}
// Order by with empty or without empty user string
$sqlAll .=" ORDER BY ". $columns[$request['order'][0]['column']]." ".$request['order'][0]['dir']." LIMIT ".$request['start']." ,".$request['length']." ";
$stmt = $dbo->prepare($sqlAll);
// Execute the query
$stmt->execute([$admin,$emptyValues,$adminLive,$adminType,$customusDomestic,$adminChina,$adminIndia,$adminCustom,$customusaustinCulver,$customuschinaTokyo,$customcorkuaeBayarea,$customchinashanghaiBeijing,$adminselfMoves,$adminmilanVienna,$custombayareaSeattle,$custombayareaCulver,$customculverSingapore,$customcanada]);
$totalData = $stmt->fetchAll(PDO::FETCH_ASSOC);
$newData = array();
// Convert all the data to UTF-8 unicode format.
foreach($totalData as $item) {
$nestedData=array();
$nestedData[] = mb_convert_encoding($item['name'], 'UTF-8', 'UTF-8');
$nestedData[] = mb_convert_encoding($item['email'], 'UTF-8', 'UTF-8');
$nestedData[] = mb_convert_encoding($item['usergroup'], 'UTF-8', 'UTF-8');
$nestedData[] = mb_convert_encoding($item['user'], 'UTF-8', 'UTF-8');
$nestedData[] = mb_convert_encoding($item['DateAdded'], 'UTF-8', 'UTF-8');
$nestedData[] = '<i class="fas fa-edit" data-toggle="modal" data-target="#editModal" id="'.$item['mem_id'].'"></i> <i class="fas fa-trash" id="'.$item['mem_id'].'"></i>';
$newData[] = $nestedData;
}
$json_data = array(
"draw" => intval( $request['draw']),
"recordsTotal" => intval( $rowCount ),
"recordsFiltered" => intval( $totalFiltered ),
"data" => $newData
);
echo json_encode($json_data);
// This is fine but what happens when I need to make another ajax call to this file? It'll read all the contents of this script how can I make it to where it reads only certain code within the same file?
最佳答案
您可以使用 if block 条件来执行特定的 ajax 调用,假设您想执行第一个 ajax 调用中的第一个 if block ,那么您必须使用 ajax 传递参数,确保第一个 if block 条件为真,并且方式相同对于所有 ajax 调用。
PHP 代码
<?php
if($_POST['DATA']=='FIRST'){
all statements are here.
}else if($_POST['DATA']=='SECOND'){
all statements of second block is here.
}else if($_POST['DATA']=='THIRD'){
all statements of the third block are here.
} so on
?>
AJAX 调用
// First AJAX CALL for FIRST IF BLOCK
$.ajax({
url: 'please give php file url',
data:{ DATA: 'FIRST' },
success: function(){
console.log('works');
}
});
// Second AJAX CALL for SECOND IF BLOCK
$.ajax({
url: 'please give php file url',
data:{ DATA: 'SECOND' },
success: function(){
console.log('works');
}
});
// so on
我想,我走在正确的道路上:)
关于javascript - 多个ajax调用同一个文件来执行同一个文件中的不同代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52562855/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!