当我尝试从php文件中检索值到javascript时

时间:2018-11-23 12:01:15

标签: javascript php json ajax oci

我正在尝试从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 < / p>

更新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();
    }

}

错误提示:

  

未捕获的TypeError:无法读取未定义的属性'SONG_ID'
  在播放歌曲((索引):140)
  在HTMLButtonElement.onclick

0 个答案:

没有答案