使用 PHP/AJAX 加载动态文件时出现问题
我为我们的客户创建了一个相当安全的登陆页面来下载他们的合同副本。
您点击retrieve.php,就会出现EULA(jQuery 对话框)。签名并接受后,jQuery 对话框就会消失。
AJAX 调用将发送到服务器以获取散列文件名,并生成一个链接以在浏览器中安全地查看 PDF。
问题是,现在公司要我添加JPG版本的合同。合同有 2 页或 3 页,具体取决于他们想要使用我们的服务。
我遇到的问题是如何为 jpg 版本生成正确数量的链接?一切都是在 AJAX 中完成的,我不想从 AJAX 返回图像的数量。
图像名称是(与 pdf 相同的哈希值)-.jpg
有人有动态方法来执行此操作吗?
我在 PHP 中编写了一个函数来执行此操作,但由于我在客户端运行链接生成,因此它不起作用。
我的问题是: 如果你是我,你会通过 JSON 返回所有 3 或 4 个链接(pdf + 无论有多少 jpg)吗?
您会以 json 形式传回页数并动态创建链接吗? 您有更好的解决方案吗?
一些代码: 以下是 AJAX 调用,它将信息发送到服务器,然后创建链接:
$.ajax({ type: 'POST',
url: 'getContractAJAX.php',
data: {
'pin' : pin.val(),
'name' : signature.val().toUpperCase(),
'lead' : '<?php echo $_GET['lead']; ?>'
}, /* end data */
cache: false,
success: function(contractId) {
if (contractId['success'] == true) {
contractExpireDate = new Date(contractId['contractExpDate']);
today = new Date();
// Log the signature.
$.post('log.php', {
signature: $("#electronicSignature").val().toUpperCase(),
pin: $("#eSpin").val(),
lead: '<?php echo $_GET['lead']; ?>',
method: 'SIGNATURE'},
function(log) {
if(log['success'] == true) {
/* DOWNLOAD IMAGES LINKS */
$("#downloadLinks").prepend("<a href='#' onclick='log(\""+contractId['contract']+"\", \"DOWNLOAD\");'><img src='img/btnDownloadPdf.png' alt='Downdload PDF' /><br />Download Contract in Adobe © PDF</a>");
} else {
alert("There was a problem! Please contact customer support.");
showDisclosure();
} /* end else */
}); /* end function(log) */
$("#dialog-confirm").dialog("close");
getContractAJAX.php:
if (isset($_POST['pin']) && isset($_POST['name']) && isset($_POST['lead'])){
$pin = $_POST['pin'];
$name = trim($_POST['name']);
$email = $converter->decode($_POST['lead']);
/*
*
*
* PDO
*
*
*/
$stmt = $dbh->prepare("SELECT contract, contractExpireDate
FROM users
WHERE emailAddress=:email AND
pin=:pin AND
concat(firstName, ' ', lastName) LIKE :name AND
contractExpireDate > NOW()
LIMIT 1");
if ($stmt->bindParam(':email', $email, PDO::PARAM_STR) &&
$stmt->bindParam(':pin', $pin, PDO::PARAM_INT) &&
$stmt->bindParam(':name', $name, PDO::PARAM_STR)) {
$stmt->execute();
$found = 0;
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $el) {
$found++;
$contract = $el['contract'];
$contractExpDate = $el['contractExpireDate'];
}
$stmt = null;
}
if ($found > 0) {
$success = true;
} else {
$success = false;
$error = "NO MATCHES FOUND >>> $email >>> $pin >>> $name";
}
}else{
$success = false;
$error = "NOT ALL PASSED";
}
header('Content-type: application/json');
$json = array("contract" => $contract,
"contractExpDate" => $contractExpDate,
"success" => $success,
"messege" => $error);
echo json_encode($json);
I have created a fairly secure landing page for our clients to download copies of their contracts.
You hit retrieve.php, the EULA (jQuery Dialog) appears. After you sign and accept, the jQuery dialog disappears.
An AJAX call is sent to the server to get the hashed filename and a link is generated to view the PDF securely in your browser.
The problem is, now the company wants me to add a JPG version of the contract. The contract is either 2 or 3 pages, depending on what service they want to use with us.
The problem I am having is how do I generate the correct number of links for the jpg versions? Everything is done in AJAX, and I would prefer not to return the number of images from AJAX.
The image names are (same hash as pdf)-.jpg
Does anybody have a dynamic way to do this?
I wrote a function that does this in PHP, but since I am running the link generation on the client side, it will not work.
My question is:
I you were me, would you return all 3 or 4 links (pdf + however many jpgs there are) back through JSON?
Would you pass the number of pages back in json and create the links dynamically?
Do you have any better solutions?
Some code:
Here is the AJAX Call that gets sends the info to the server then creates the links:
$.ajax({ type: 'POST',
url: 'getContractAJAX.php',
data: {
'pin' : pin.val(),
'name' : signature.val().toUpperCase(),
'lead' : '<?php echo $_GET['lead']; ?>'
}, /* end data */
cache: false,
success: function(contractId) {
if (contractId['success'] == true) {
contractExpireDate = new Date(contractId['contractExpDate']);
today = new Date();
// Log the signature.
$.post('log.php', {
signature: $("#electronicSignature").val().toUpperCase(),
pin: $("#eSpin").val(),
lead: '<?php echo $_GET['lead']; ?>',
method: 'SIGNATURE'},
function(log) {
if(log['success'] == true) {
/* DOWNLOAD IMAGES LINKS */
$("#downloadLinks").prepend("<a href='#' onclick='log(\""+contractId['contract']+"\", \"DOWNLOAD\");'><img src='img/btnDownloadPdf.png' alt='Downdload PDF' /><br />Download Contract in Adobe © PDF</a>");
} else {
alert("There was a problem! Please contact customer support.");
showDisclosure();
} /* end else */
}); /* end function(log) */
$("#dialog-confirm").dialog("close");
getContractAJAX.php:
if (isset($_POST['pin']) && isset($_POST['name']) && isset($_POST['lead'])){
$pin = $_POST['pin'];
$name = trim($_POST['name']);
$email = $converter->decode($_POST['lead']);
/*
*
*
* PDO
*
*
*/
$stmt = $dbh->prepare("SELECT contract, contractExpireDate
FROM users
WHERE emailAddress=:email AND
pin=:pin AND
concat(firstName, ' ', lastName) LIKE :name AND
contractExpireDate > NOW()
LIMIT 1");
if ($stmt->bindParam(':email', $email, PDO::PARAM_STR) &&
$stmt->bindParam(':pin', $pin, PDO::PARAM_INT) &&
$stmt->bindParam(':name', $name, PDO::PARAM_STR)) {
$stmt->execute();
$found = 0;
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $el) {
$found++;
$contract = $el['contract'];
$contractExpDate = $el['contractExpireDate'];
}
$stmt = null;
}
if ($found > 0) {
$success = true;
} else {
$success = false;
$error = "NO MATCHES FOUND >>> $email >>> $pin >>> $name";
}
}else{
$success = false;
$error = "NOT ALL PASSED";
}
header('Content-type: application/json');
$json = array("contract" => $contract,
"contractExpDate" => $contractExpDate,
"success" => $success,
"messege" => $error);
echo json_encode($json);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不返回所有图像的单个压缩文件(tar、zip 等)?这将为您提供一次下载,并使下载速度更快/更小。
Why not return a single compressed file (tar, zip, etc.) of all of the images? That would give you a single download and makes the download faster/smaller.