首页 > 解决方案 > 当我试图从 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”

标签: javascriptphpjsonajaxoracle-call-interface

解决方案


推荐阅读