提问人:jon 提问时间:12/12/2020 最后编辑:Or Assayagjon 更新时间:12/12/2020 访问量:70
将PHP放在JavaScript中会给出一个关于缺少的错误:)
putting PHP within JavaScript gives an error about a missing )
问:
这是来自成功的 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 } }}?>');
}
}
我的问题就在这里:我想将 cookie 值与循环值进行比较。如果我只输入一个纯文本字符串,其中 $_COOKIE[“tx”] 一切正常。if ($id["id"] == $_COOKIE["tx"])
但是有了它,我得到了错误:
未捕获的 SyntaxError:参数列表后缺少 )
JavaScript 对我来说是一个谜,所以任何帮助都是值得赞赏的。谢谢。
答:
0赞
raoyc
12/12/2020
#1
您的括号未配对。对于代码混合的代码来说,这不利于此。html/js
php
您可以使用一些流行的前端框架,例如 restful 后端框架,例如支持 json-api。Vue/React
Laravel/Yii2
也许你应该像这样重新组织你的代码:
<?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;
评论