- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 PHP 和 JQuery 构建一个轻量级管理实用程序。该实用程序的目的是允许管理员从数据库中提取图像,查看附加到每个图像的标签,并根据需要添加新标签。整体功能相当简单,应用程序以当前形式运行。
我的问题是这样的:信息应该如何在 PHP 和 JQuery 之间来回传递,特别是考虑到 Javascript 可用的所有信息对于 DOM 上的用户也是可见的?
目前,程序流程如下:index.php 查询数据库并检索与某个事件相关的所有图片的信息;该数组的格式设置为在图像轮播中显示,并且还作为页面底部的隐藏表格包含在内(这是我知道的错误)。 index.php 需要 display.html,它向用户显示轮播和标记表单。每当用户单击“更新”按钮时,就会触发 ajax 查询,序列化更新表单中的信息并将其发送到 updateTags.php,后者执行更新查询并添加标签。
对于这个项目,我使用 Owl Carousel使用延迟加载来显示图像。
这是index.php
<?php
// Displays picture data as HTML in order to allow javascript to traverse
// Rows are indexed using the image's location in the array, which will
// correspond to that image's location in the carousel
function FormatAsHTMLTable ($data) {
$dataTable = "<table id='data' class='table'><tbody>";
foreach ($data as $i => $member) {
$dataRow = "<tr class='" . $i . "'>";
foreach ($member as $key => $value) {
$dataRow .= "<td class='" . $key . "'>";
if (is_array($value)) {
$i = 0;
foreach ($value as $item) {
if (!$i++) {
$dataRow .= $item;
} else {
$dataRow .= ", " . $item;
}
}
} else {
$dataRow .= $value;
}
$dataRow .= "</td>";
}
$dataRow .= "</tr>";
$dataTable .= $dataRow;
}
$dataTable .= "</tbody></table>";
return $dataTable;
}
function FormatAsCarouselData ($data) {
// Formats array for display in the owl carousel
return $carouselData;
}
// This array would normally be retrieved from a query to the mySQL db;
// this is provided as an example
$pics = array(
array(
"id" => 5,
"tags" => array("foo", "bar"),
"url" => "img/demo-slides/owl1.jpg"
),
array(
"id" => 6,
"tags" => array("boo", "ya"),
"url" => "img/demo-slides/owl2.jpg"
),
array(
"id" => 7,
"tags" => array("I", "am", "second"),
"url" => "img/demo-slides/owl3.jpg"
),
array(
"id" => 8,
"tags" => array("bird", "is", "word"),
"url" => "img/demo-slides/owl4.jpg"
),
...
);
$dataTable = FormatAsHTMLTable ($pics);
$carouselData = FormatAsCarouselData ($pics);
require('display.html');
?>
这是 display.html,因为它会在插入数据表和轮播图像后查看:
<html>
<body>
...
<div class="row">
<div class="col">
<div id="imageContainer" class="owl-carousel">
<div class='item'>
<img class='lazyOwl' data-src='img/demo-slides/owl1.jpg' alt='Photo # 0'>
</div>
<div class='item'>
<img class='lazyOwl' data-src='img/demo-slides/owl2.jpg' alt='Photo # 1'>
</div>
<div class='item'>
<img class='lazyOwl' data-src='img/demo-slides/owl3.jpg' alt='Photo # 2'>
</div>
<div class='item'>
<img class='lazyOwl' data-src='img/demo-slides/owl4.jpg' alt='Photo # 3'>
</div>
...
</div>
</div>
<form method="post" action="lib/updateTags.php" id="updateForm">
<div class="col">
<input type="text" name="tag1" id="tag1" placeholder="Tag 1"/>
<br>
<input type="text" name="tag2" id="tag2" placeholder="Tag 2"/>
<br>
<input type="text" name="tag3" id="tag3" placeholder="Tag 3"/>
<br>
<input type="text" name="tag4" id="tag4" placeholder="Tag 4"/>
<br>
<input type="text" name="tag5" id="tag5" placeholder="Tag 5"/>
<br>
<button type="submit" id="updateButton">Update</button>
</div>
</div>
<input type="hidden" id="imageIdInput" name="imageIdInput" value="">
</form>
<table id='data' class='table' style='display:none'>
<tbody>
<tr class='0'>
<td class='num'>0</td><td class='id'>5</td><td class='tags'>foo, bar</td><td class='url'>img/demo-slides/owl1.jpg</td>
</tr>
<tr class='1'>
<td class='num'>1</td><td class='id'>6</td><td class='tags'>boo, ya</td><td class='url'>img/demo-slides/owl2.jpg</td>
</tr>
<tr class='2'>
<td class='num'>2</td><td class='id'>7</td><td class='tags'>I, am, second</td><td class='url'>img/demo-slides/owl3.jpg</td>
</tr>
<tr class='3'>
<td class='num'>3</td><td class='id'>8</td><td class='tags'>bird, is, word</td><td class='url'>img/demo-slides/owl4.jpg</td>
</tr>
...
</tbody>
</table>
</body>
</html>
显示.js:
$(document).ready(function() {
var updateForm = $("#updateForm"),
data = $("#data");
$("#imageContainer").owlCarousel({
items : 1,
lazyLoad : true,
navigation : true,
pagination : false,
afterMove : afterMove,
afterInit : afterInit
});
owl = $("#imageContainer").data('owlCarousel');
var form = $('#updateForm');
form.submit(function (e) {
e.preventDefault();
$.ajax( {
type: "POST",
url: form.attr( 'action' ),
data: form.serialize(),
success: function( response ) {
console.log( response );
}
} );
});
function updateInfo(that){
// Get the current position in the carousel
var current = parseInt(that.owl.currentItem);
// Get image information from data table based on carousel location
var num = data.find("." + current).find(".num").html(),
id = data.find("." + current).find(".id").html();
//Display image information to user
// update hidden form input
imageIdInput.value=id;
console.log("imageIdInput updated to: " + id);
}
function afterInit(){ // Called once the carousel is set up
console.log("init callback called");
var that = this;
updateInfo(that);
}
function afterMove(){ // Called when the carousel moves
console.log("Carousel moved");
var that = this;
updateInfo(that);
}
});
很明显,将所有图像信息显示为 HTML 表格是错误的方法。即使显示设置为无,快速浏览一下页面源代码也会显示所有这些信息。因为这是一个管理实用程序,永远不会面向客户,所以目前有效;但它很业余而且很老套。所以我的问题是:如何向 JQuery 提供特定于图像的信息,而不使其对用户完全透明?或者至少不使用单独的 html 表?我的假设是每次轮播移动时都会涉及一个单独的 $.ajax 调用;但即便如此,我仍然需要保留一些识别信息,以便准确地知道我正在查看哪个图像。
这个项目有几个独特的考虑因素,这使得我特别难以弄清楚:1)不能假设图像 ID 是连续的;因此,我无法从图像在轮播中的位置推断出图像 ID。 2)有时向用户显示图像的子集,而不是每个相关图像;在这种情况下,仍然有必要让用户知道他相对于全套图像的位置(例如,全套图像中可能有 8000 张图像;正在向用户显示 1000-2000 张图像;用户必须仍然显示与全套相关的正确图像编号,例如“图像# 1500”)。
抱歉文字墙;我试图排除任何不相关的内容,同时仍然明确我要问的内容以及我当前的解决方案是什么。
预先感谢您的帮助!
最佳答案
更详细地阅读您的问题后:
您可以让用户更难看到此信息。
没有必要(或高效)生成 html 内容只是为了导航 DOM 并提取内容以用作 Javascript 中的数据 - 您可以跳过整个步骤,并通过 AJAX 调用返回所有图像的 PHP 页面数据采用良好的 JSON 格式(或者您想要的任何格式)。这将简化您的整个应用程序,可能会提高性能,并使用户更难以查看数据。这真是帽子戏法!
关于javascript - $.ajax 和隐藏字段,还是 PHP 表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204365/
有人有 Comet 应用程序 .net 的任何样本吗? 我需要一个示例如何在服务器中保持客户端的连接? 最佳答案 这里也有一些不错的: http://www.frozenmountain.com/we
我想知道是否有 Yii2 专家可以帮助我了解如何最好地使用 ajax 表单与 Yii ajax 验证相结合。我想我可以在不带您阅读我所有代码的情况下解释这个问题。 我正在处理一个促销代码输入表单,用户
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
f:ajax 和 a4j:ajax 标记之间有什么显着差异吗? 我知道 Richfaces 4 中的 a4j:ajax 基于 native f:ajax JSF2 标记,添加了一些 f:ajax 中未
我已经尝试过这样但无法获取数组列表。它返回“null” var data=[]; data[0] = '1'; data[1] = '2'; $.ajax({
在教程中可以看到 jQuery.ajax 和 $.ajax 喜欢这里 http://www.thekludge.com/form-auto-save-with-jquery-serialize/ jQ
过度使用 AJAX 会影响性能吗?在大型 Web 应用程序的上下文中,您如何处理 AJAX 请求以控制异步请求? 最佳答案 过度使用任何东西都会降低性能;在必要时使用 AJAX 将提高性能,特别是如果
似乎我无法使用 Ext.Ajax.request 进行跨域 ajax 调用。看起来 ScriptTag: True 没有任何效果。 这是我的代码: {
我正在使用 Bottle 微框架(但我怀疑我的问题来自它) 首先,如果我定义了一个从/test_redirect 到/x 的简单重定向,它会起作用。所以 Bottle redirect() 在简单的情
任何人都可以指出各种 AJAX 库的统一比较吗?我已经阅读了大约十几种不同的书,我即将开始一个项目,但我对自己是否已经探索了可能性的空间没有信心。 请注意,我不是在要求“我认为 XXX 很棒”——我正
似乎使用 AJAX 的站点和应用程序正在迅速增长。使用 AJAX 的主要原因之一可能是增强用户体验。我担心的是,仅仅因为项目可以使用 AJAX,并不意味着它应该。 可能是为了 UX,AJAX 向站点/
假设我有一个可以通过 Javascript 自定义的“报告”页面。假设我有可以更改的 start_date、end_date 和类型(“简单”或“完整”)。现在 我希望地址栏始终包含当前(自定义) V
我一直在阅读 Ajax 并且希望从 stackoverflow 社区看到我是否正确理解所有内容。 因此,正常的客户端服务器交互是用户在 url 中拉出 Web 浏览器类型,并将 HTTP 请求发送到服
这可能有点牵强,但让我们假设我们需要它以这种方式工作: 我在服务器的 web 根目录中有一个 index.html 文件。该文件中的 javascript 需要向/secure/ajax.php 发出
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 去年关闭。 Improve this
我希望ajax post成功进入主页。由于某种原因,我一直做错事。知道我应该做什么来解决这个问题吗? window.APP_ROOT_URL = ""; Ajax $.ajax({ url: '#{a
我在 2 个不同的函数中有 2 个 ajax 调用。我想用.click来调用这2个函数。 func1 将数据插入数据库,然后 func2 检索数据,所以我的问题是如何等到 func1 完全完成然后只执
我试图在单击按钮后禁用该按钮。我尝试过: $("#ajaxStart").click(function() { $("#ajaxStart").attr("disabled", true);
我试图在每个 Ajax 请求上显示加载动画/微调器 我的 application.js $(document).on("turbolinks:load", function() { window.
我正在显示使用jQplot监视数据的图形。 为了刷新保存该图的div,我每5秒调用一次ajax调用(请参见下面的JavaScript摘录)。 在服务器上,PHP脚本从数据库中检索数据。 成功后,将在5
我是一名优秀的程序员,十分优秀!