Phoegap/Cordova 在 AJAX GET 之后加载特定的数据库行
我正在使用 PHoneGap 作为编译器构建应用程序,因此使用 HTML5、CSS、JQuery、AJAX 等。我设法让 AJAX 完美地从数据库中获取所有行,因为我必须在文件上使用 .HTML 扩展名,所以我很难链接到特定的数据库记录。我可以在 PHP 中完美地做到这一点。我正在努力解决 HTML 部分。
这是我的 AJAX 加载器,用于从数据库中获取所有行
var inProcessVideos = false;//Just to make sure that the last ajax call is not in process
setTimeout( function () {
if (inProcessVideos) {
return false;//Another request is active, decline timer call ...
}
inProcessVideos = true;//make it burn ;)
jQuery.ajax({
url: 'https://MY-URL.COM/videos-mysql.php', //Define your script url here ...
data: '', //Pass some data if you need to
method: 'POST', //Makes sense only if you passing data
success: function(answer) {
jQuery('#videos-modules').html(answer);//update your div with new content, yey ....
inProcessVideos = false;//Queue is free, guys ;)
},
error: function() {
//unknown error occorupted
inProcessVideos = false;//Queue is free, guys ;)
}
});
}, 500 );
这是呈现数据库中所有结果的 PHP 文件的内容。这部分内容显示得很完美。
<?php
include ("../config/mysqli_connect.php");
$sql = ("SELECT * FROM videos");
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "
<a href='" . $row["id"]. "'>
<div class='video-module'>
<div class='video-thumb'><img src='https://MY-URL.COM/thumbs/" . $row["video_thumb"]. "'></div>
<div class='video-thumb-details'>
<div class='video-thumb-title'> " . $row["id"]. " - " . $row["video_title"]. "</div>
" . $row["publisher_name"]. "
</div>
</div></a>
";
}
} else {
echo "0 results";
}
?>
在 ECHO 语句之后,我通常会放置类似 video-Profile.php?id=$id 的内容,然后它会转到该页面并从数据库中提取该记录。
但是现在我只需要用 HTML 来做这件事,而且我假设使用 AJAX,我该如何实现这一点呢。
这里是 PHP 和 MYSQL 查询,用于从数据库获取特定记录。它目前在 MYSQL 中,一旦我让它工作并理解它,我就会将它转换为 MYSQLi。
<?php
// Use the URL 'id' variable to set who we want to query info about
$id = ereg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
echo "Missing Data to Run";
exit();
}
//Connect to the database through our include
include_once "../config/connect_to_mysql.php";
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM videos WHERE id='$id' LIMIT 1");
$count = mysql_num_rows($sql);
if ($count > 1) {
echo "There is no user with that id here.";
exit();
}
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$video_title = $row["video_title"];
$video_thumb = $row["video_thumb"];
$publisher_name = $row["publisher_name"];
$video_directory = $row["video_directory"];
$video_path = $row["video_path"];
$upload_date = $row["upload_date"];
$video_views = $row["video_views"];
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<?php echo ("$id");?> - <?php echo ("$video_thumb");?>
</body>
</html>
我知道如果我运行 PHP 文件并且我的服务器设置为 PHPv5.3,这会起作用。但在我让它上线之前,它会被分类到 MYSQLi 并在 PHP7 上运行???
我正在寻找灵感,让它通过仅 HTML 文件运行。
谢谢大家的帮助。
这是一种非常粗暴的做法 - 通常您会从 PHP 返回
JSON
或类似内容,然后将其处理为 JS 中的 HTML 元素。但在这种情况下,您可以这样做:
//within call
success: function(answer) {
var contents = jQuery(answer); // You have now converted the HTML into a jquery model
contents.filter(function(element){
return $(element).attr('id') === id
}) // This allows you to search your child elements and pick them based on criteria
jQuery('#videos-modules').html(contents); // now assign the remaining elements into your html as before
},
我尝试过这个,但我似乎无法找到如何运行控制台日志,因为它目前在 iOS iPad 上运行。无法让它在浏览器中呈现。
var inProcessVideos = false;//Just to make sure that the last ajax call is not in process
setTimeout( function () {
if (inProcessVideos) {
return false;//Another request is active, decline timer call
}
inProcessVideos = true;//make it burn ;)
jQuery.ajax({
url: 'https://MYURL.COM/appFiles/tablet/video-profile.php', //Define your script url here ...
data: '', //Pass some data if you need to
method: 'GET', //Makes sense only if you passing data
success: function(answer) {
var contents = jQuery(answer); // You have now converted the HTML into a jquery model
contents.filter(function(element){
return $(element).attr('id') === id;
});
jQuery('#videoProfile').html(answer);//update your div with new content, yey ....
inProcessVideos = false;//Queue is free, guys ;)
},
});
}, 500 );
为此而苦恼,我查看了所有我能找到的 JQuery、AJAX MySQL 网站,包括 W3Schools、Jquery.com 和许多其他网站。就是无法让它将 ID 传递给 PHP 文件以通过 AJAX 从 DB 获取记录。
第一个 JQuery AJAX 调用中的链接是:
<a href='video-Profile.html' data='".$row["id"]."' value='".$row["id"]." '>Some HTML STUFF/Images/Text etc</a>