我正在尝试在HTML文档的JS块中运行PHP函数。函数包括PHP、HTML和JS。
该函数包含一个循环,该循环一直运行到最后一次,并输出以下内容:
";document.getElementById(“代码”).innerHTML += sayThis;
错误消息显示:未捕获SyntaxError:意外的标识符
该函数名为load_offers,它从数据库中检索数据,并将其显示在列表对象中。
它会做它应该做的事情,直到我添加以下内容:
<script type="text/javascript">
var sayThis = "<?php load_offers(); ?>";
document.getElementById("code").innerHTML += sayThis;
</script>
这个函数需要被调用,所以当用户向下滚动到页面末尾时,它会再加载6个列表对象,而不是一次加载所有内容。这就是为什么我需要JS来调用函数。
下面是函数:
function load_offers(){
//Gets variables needed to connect to mysql.
global $servername;
global $username;
global $password;
global $dbname;
global $fetched;
global $amount;
$conn = new mysqli($servername, $username, $password, $dbname);
//Creates a query that loads a limited number of offers.
$query = "SELECT ID, TITLE, IMAGELINK, DATE_ADDED, OLD_PRICE, NEW_PRICE, SAVE_PERCENT FROM erbjudanden WHERE APPROVED = 'Y' ORDER BY DATE_ADDED DESC
LIMIT $fetched, $amount";
$result = $conn->query($query);
//Declares arrays.
$all_offers = array();
$titles = array();
$imagelinks = array();
$dates = array();
$old_prices = array();
$new_prices = array();
$percents = array();
$link = array();
$i = 0;
$bad_chars = array("'", "!", ",", "\"", "(", ")", "[", "]", "#", "&", "+", "=", "@", "$", "%", "|" );
//Gets data from row into arrays.
while($row = $result->fetch_assoc()){
$all_offers[$i] = $row['ID'];
$titles[$i] = $row['TITLE'];
$imagelinks[$i] = $row['IMAGELINK'];
$dates[$i] = $row['DATE_ADDED'];
$old_prices[$i] = $row['OLD_PRICE'];
$new_prices[$i] = $row['NEW_PRICE'];
$percents[$i] = $row['SAVE_PERCENT'];
//Creates a link with only numbers, lowercase letters and - instead of spaces.
$link[$i] = $row['TITLE'];
$link[$i] = strtolower($link[$i]);
$link[$i] = str_replace(" ", "-", $link[$i]);
foreach($bad_chars as $bad){
$link[$i] = str_replace($bad, "", $link[$i]);
}
$link[$i] = str_replace("--", "-", $link[$i]);
$link[$i] = $link[$i] . "-" . $all_offers[$i];
$i++;
}
//Writes out data as listobjects.
$a = 0;
foreach ($all_offers as $offer)
{
$aID = $fetched + $a;
echo'<a href="'.$link[$a].'">
<li class = "listobjekt">
<h4 align = "center" >' . $titles[$a] . '</h4>
<img src="' . $imagelinks[$a] . '" class = "tellme" id = "tiny'. $aID .'">
<table class = "pri">
<tr><td width="50">Förr:</td><td id ="for"> ' . $old_prices[$a] . ' :-</td></tr>
<tr><td>Nu:</td><td id = "bu">' . $new_prices[$a] . ' :-</td></tr>
<tr><td >Spara:</td><td id = "spara">' . $percents[$a] . ' %</td></tr>
</table>
</li></a>
//Makes image fit it's container and align correctly.
<script type="text/javascript">
var file = document.getElementById("tiny'.$aID.'");
if(file.height < file.width){
file.style.width="180px";
padding = file.width - file.height;
padding = padding / 2;
padding = padding+"px";
file.style.marginTop=padding;
file.style.marginBottom=padding;
}
else{
file.style.height="180px";
padding = file.height - file.width;
padding = padding / 2;
padding = padding+"px";
file.style.marginLeft=padding;
file.style.marginRight=padding;
}
</script>
';
$a++;
}
}
希望我说得够清楚了。
发布于 2018-07-28 07:05:46
首先,使用PHP调整您的图像。
其次,确保javascript变量sayThis在一行中。Javascript不支持带单引号的多行。
我在你的电脑上测试过了,它工作了!:)
发布于 2018-07-27 10:21:30
在PHP文档中运行JS可能会更简单
为了能够正常工作,它必须在文档主体中的脚本标记之间。
注意:我在我的机器上测试了它,它工作正常。文件扩展名为.php
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var test = "<?php echo 'test'; ?>";
console.log(test);
</script>
</body>
</html>
编辑:显然这不是最佳实践,最好是这样运行PHP:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
var test = <?= json_encode('test') ?>;
console.log(test);
</script>
</body>
</html>
在评论中支持Phil,因为他帮我解决了这个问题。
https://stackoverflow.com/questions/51549811
复制相似问题