javascript - 当我试图从 php 文件中检索一个值到 javascript
问题描述
我试图从 php 文件中获取一个值,php 文件从 ORACLE DATABASE 检索数据,到脚本 js 这是 php 文件:
<?php
include("../../config.php");
if(isset($_POST['songId'])) {
$songId = $_POST['songId'];
$query = oci_parse($con, "SELECT s.song_path, s.song_id, s.TITLE, r.Artist_Name, a.artworkpath FROM songs s, artists r, albums a WHERE s.artist_id = r.artist_id AND a.album_id = s.album_id AND song_id='$songId'");
oci_execute($query);
$resultArray = oci_fetch_array($query);
echo json_encode($resultArray,JSON_FORCE_OBJECT);
}
?>
它工作并成功地从表中检索数据。
使用数据的脚本:
<?php
$songQuery = oci_parse($con, "SELECT song_id from songs order by dbms_random.value");
oci_execute($songQuery);
$resultArray = array();
while(($row = oci_fetch_array($songQuery, OCI_BOTH)) != false) {
array_push($resultArray, $row['SONG_ID']);
}
$jsonArray = json_encode($resultArray, JSON_FORCE_OBJECT);
?>
<script>
$(document).ready(function() {
currentPlaylist = <?php echo $jsonArray; ?>;
audioElement = new Audio();
setTrack(currentPlaylist[0], currentPlaylist, false);
});
function setTrack(trackId, newPlaylist, play) {
$.post("includes/handlers/ajax/getSongJson.php", { songId: trackId }, function(data) {
var track = JSON.parse(data);
$(".trackName span").text(track.TITLE);
$(".artistName span").text(track.ARTIST_NAME);
$(".albumLink img").attr("src", track.ARTWORKPATH);
audioElement.setTrack(track.SONG_PATH);
audioElement.audio.play();
});
if(play == true) {
audioElement.audio.play();
}
}
function playSong() {
if(audioElement.audio.currentTime == 0) {
$.post("includes/handlers/ajax/updatePlays.php", { songId: audioElement.audio.currentlyPlaying.SONG_ID});
}
$(".controlButton.play").hide();
$(".controlButton.pause").show();
audioElement.audio.play();}
function pauseSong() {
$(".controlButton.play").show();
$(".controlButton.pause").hide();
audioElement.audio.pause();
}
</script>
所有功能都运行良好,除了
(songId: audioElement.audio.currentlyPlaying.SONG_ID)
它无法识别 SONG_ID,它是在 php 文件 ( ajax/getSongJson.php
)中检索到的数据库中的一列
PS: ( ajax/updatePlays.php
) 文件是一个 php 文件,当用户在当前时间 = 0 时使用 playSong 函数时用于更新数据 (PLAYS),因此它将数据库中的 PLAY 列增加 1
更新php文件
<?php
include("../../config.php");
if(isset($_POST['songId'])) {
$songId = $_POST['songId'];
$query = OCI_parse($con, "UPDATE songs SET plays = plays + 1 WHERE song_id='$songId'");
oci_execute($query);
}
?>
这是我创建音频类时的脚本文件,在脚本中使用:
var currentPlaylist = [];
var audioElement;
function Audio() {
this.currentlyPlaying;
this.audio = document.createElement('audio');
this.setTrack = function(track) {
this.currentlyPlaying = track;
this.audio.src = track.SONG_PATH;
}
this.play = function() {
this.play();
}
this.pause = function() {
this.pause();
}
}
错误说:
未捕获的类型错误:无法
在 HTMLButtonElement.onclick 的 playSong((索引):140)
处读取未定义的属性“SONG_ID”
解决方案
推荐阅读
- excel - Excel日期格式和数值不一致
- ios - 有人知道如何使用 ionic 和 webrct cordova-iosrtc-plugin 在 ios 上显示视频流吗?
- python - Pyspark RDD“列表索引超出范围”错误
- visual-studio-2017 - ngrok - vs2017 - 尝试启动 ngrok 隧道时遇到问题
- maven - 无法下载maven帮助插件
- json - 如何使用ansible从列表中获取值
- r - 通过将两列分组并将其保存到新列中来应用函数
- excel - 删除标题另存为 PDF 的示例 Excel 文件
- xml - SSRS 开发:Visual Studio 2017 (VS2017) 安装的最低配置
- php - php中两个单词的比较总是返回true