需要帮助在js中实现php

时间:2016-12-08 18:07:00

标签: javascript php

Hello stackoverflow社区,我正在处理一个项目的问题。 我尝试了一些方法来使这个工作,但最好我认为它将与PHP回应。所以,这是我的方式(第20和21行):

	<script type="text/javascript">
	jQuery(document).ready(function($) {
    	"use strict";
    	var changeTrack = function changeTrack(event) {
	        var current = myPlaylist.current,
	        	playlist = myPlaylist.playlist;       
	        $.each(playlist, function (index, obj) {
	            if (index == current) {
	                $("#nowPlaying .artist-name").html(obj.artist);
	                $("#nowPlaying .track-name").html(obj.title);
	            }
	        });
	    };
	    
		var myPlaylist = new jPlayerPlaylist({
			jPlayer: "#jquery_jplayer_1",
			cssSelectorAncestor: "#jp_container_1",
		}, [
			{
				artist:"<?php echo $artist; ?>",
				title:"<?php echo $track; ?>",
				mp3:"http://<?php echo $ipport; ?>/api/v1/bot/i/<?php echo $inst; ?>/stream/<?php echo $sinusbot->getWebStreamToken($inst); ?>"
			}
		],{
			playlistOptions: {
			    enableRemoveControls: false,
			    autoPlay: true
			},
			swfPath: "assets/jplayer/jplayer",
			supplied: "ogv, m4v, oga, mp3",
			wmode: "window",
			useStateClassSkin: true,
			autoBlur: false,
			smoothPlayBar: false, //removes bar jittery
            ready: changeTrack,
		    play: changeTrack
		});
	});//end .ready
	</script>
此外,我试图将require 'getSong.php';include("getSong.php");的php文件包含在内,但它只是崩溃了网站。这是php文件:

<?php
error_reporting('E_ERROR');
include("sinusbot.class.php");
include("config.php");
$sinusbot = new SinusBot($ipport);
$sinusbot->login($user, $passwd);
$status = $sinusbot->getStatus($inst);
$track = (($status["currentTrack"]["type"] == "url") ? $status["currentTrack"]["tempTitle"] : $status["currentTrack"]["title"]);
$artist = $status["currentTrack"]["tempArtist"];
$name = $track;
$track = preg_replace('^ ^', '+', $track);

if(!empty($artist)) {
echo $name." from ".$artist;
} else {
echo $name;
}
忘了告诉你们这是什么... 我想从这个js自动获得曲目名称/艺术家。

我将在这里放下用于getSong.php的旧方法,js:

<script type="text/javascript">
function loadSong() {
		var xhttp = new XMLHttpRequest();
		xhttp.onreadystatechange = function() {
			if (xhttp.readyState == 4 && xhttp.status == 200) {
				document.getElementById("getSong").innerHTML = "" + xhttp.responseText;
			}
		};
		xhttp.open("GET", "getSong.php", true);
		xhttp.send();
	}	

 function loadImg() {
		var xhttp = new XMLHttpRequest();
		xhttp.onreadystatechange = function() {
			if (xhttp.readyState == 4 && xhttp.status == 200) {
				document.getElementById("getImg").innerHTML = xhttp.responseText;
			}
		};
		xhttp.open("GET", "getImg.php", true);
		xhttp.send();
	}
	
 function loadSearch() {
		var xhttp = new XMLHttpRequest();
		xhttp.onreadystatechange = function() {
			if (xhttp.readyState == 4 && xhttp.status == 200) {
				document.getElementById("search").innerHTML = xhttp.responseText;
			}
		};
		xhttp.open("GET", "search.php", true);
		xhttp.send();
	}
	
setInterval(function() {
	loadImg();
	loadSong();
	loadSearch();
}, 3500); 
</script>
它只是将div ID放在页面的某个位置......

    <div id="getImg">Loading...</div>
	<div id="search">Loading...</div>
	<div id="getSong">Please Wait...</div>

1 个答案:

答案 0 :(得分:0)

尝试这种方式(如果你使用没有框架的PHP):

<强>的index.php

<!DOCTYPE html>
<html>
<head>
    <script src='app_script.php'></script>
</head>
<body>
    <script type="text/javascript">
      document.write(func());
    </script>
</body>
</html>

<强> app_script.php

$str = 'test';
include("app.js");

<强> app.js

var func = function() {
    return "<?php echo $str; ?>";
};