gpt4 book ai didi

javascript - 将PHP放入JavaScript中会导致有关缺少的错误)

转载 作者:行者123 更新时间:2023-12-03 08:32:52 25 4
gpt4 key购买 nike

这是成功的ajax查询响应的内容:

if (data.tx) {

document.cookie = "tx = " + data.tx;

$('#fortune').empty().append('<?php foreach ($out_rows as $out => $rows) { $metadata = json_decode($rows["metadata"], true); $id = json_decode($rows["id"], true); if ($id["id"] == $_COOKIE["tx"]) { $card = $metadata["6770"]["map"][0]["k"]["string"]; $fortune = $metadata["6770"]["map"][0]["v"]["string"]; $direction = $metadata["6770"]["map"][1]["v"]["string"]; $image = $metadata["6770"]["map"][1]["k"]["string"]; if ($direction == "up") { ?> <div class="row"> <div class="col s12 m2"> </div> <div class="col s12 m8 center-align"> <div class="col s12 m12" style="color: #F8F3E5; background-color: #D5302F; padding: 1em; margin-top: 1em; margin-bottom: 1em: "> <h2><?php echo $card; ?></h2> </div> <div class="col s12 m12" style="background-color: #F8F3E5;"> <img src="https://fortunes.coconutpool.com/img/<?php echo $image; ?>" style=" margin: 2em; width: 35%; height: 35%;"> </div> <div class="col s12 m12" style="background-color: #FDEAA7; padding: 1em;"> <p style="font-size: 20px;">Today you may feel.. </p> <p style="font-size: 20px;"><?php echo $fortune; ?></p> <p><button class=\"btn\" onClick=\"window.location.href=window.location.href\">Get Another Fortune</button></p> </div> </div> <div class="col s12 m2"> </div> </div> <?php } if ($direction == "down") { ?> <div class="row"> <div class="col s12 m2"> </div> <div class="col s12 m8 center-align"> <div class="col s12 m12" style="color: #F8F3E5; background-color: #D5302F; padding: 1em; margin-top: 1em; margin-bottom: 1em: "> <h2><?php echo $card; ?></h2> </div> <div class="col s12 m12" style="background-color: #F8F3E5;"> <img src="https://fortunes.coconutpool.com/img/<?php echo $image; ?>" style=" margin: 2em; transform: rotate(180deg); width: 35%; height: 35%;"> </div> <div class="col s12 m12" style="background-color: #FDEAA7; padding: 1em;"> <p style="font-size: 20px;">Today you may feel.. </p> <p style="font-size: 20px;"><?php echo $fortune; ?></p> <p><button class="btn" onClick="window.location.href=window.location.href">Get Another Fortune</button></p> </div> </div> <div class="col s12 m2"> </div> </div> <?php } }}?>');
}
}
我的问题在这里: if ($id["id"] == $_COOKIE["tx"])我要在其中比较cookie值与循环值。如果我只输入$ _COOKIE [“tx”]所在的纯文本字符串,一切正常。
但是用它的方式是我收到错误:

Uncaught SyntaxError: missing ) after argument list


JavaScript对我来说是个谜,因此可以提供任何帮助。谢谢。

最佳答案

您的括号未配对。混合html/js代码的php代码不好。
您可以使用一些流行的前端框架(例如Vue/React)和静态后端框架(例如Laravel/Yii2)并支持json-api。
也许您应该像这样重新组织代码:

<?php
// php code with variables defines and updates
$title = "TO DO LIST";
$linked = "";
$todos = ["Shopping", "Learning html", "Cooking"];
foreach ($todos as $todo) {
$linked .= "<li>".$todo."</li>".PHP_EOL;
}
$addonTask = "Watching NEWS";
// html template
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$title</title>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>
<h1>$title</h1>
<div class="to-do-list">
<ul id="items">
$linked
</ul>
</div>
<script type="text/javascript">
$('#items').append('<li>$addonTask</li>');
</script>
</body>
</html>
HTML;

echo $html;

关于javascript - 将PHP放入JavaScript中会导致有关缺少的错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65262500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com