【发布时间】:2020-11-11 03:16:32
【问题描述】:
我有一个将 HTML 代码附加到某个页面的 ajax 函数。如果用户没有初始化会话,我只想显示部分代码。使用 append 方法时,PHP 代码在浏览器中被注释掉 ()。
这是我的代码:
parentEl.append("<div class='forum-reply-thread' id='forum_reply_thread_"+res[i][0]+"'>"+
"<p>"+res[i][4]+"</p>"+
"<small><a href='profile.php?userid="+res[i][2]+"'>"+res[i][3]+"</a> at "+res[i][6]+"</small>"+
"<?php if(isset($_SESSION['ID'])){ ?><button id='forum_reply_button_reply_"+res[i][0]+"'>-></button>"+
"<form class='forum-reply-to-reply-form' id='forum_reply_to_reply_form_"+res[i][0]+"'>"+
"<input type='text' id='forum_reply_content_reply_"+res[i][0]+"'>"+
"<button id='forum_submit_reply_to_reply_"+res[i][0]+"'>Reply</button>"+
"</form> <?php } ?>"
+"</div>");
这是浏览器得到的:
<!--?php if(isset($_SESSION['ID'])){ ?--> <br>
这里发生了什么?所有 cmets 都有帮助,即使它们不能解决我的问题。感谢您的宝贵时间。
【问题讨论】:
-
你不能像这样通过 JavaScript 运行 PHP。
-
这是输出之前 PHP 页面上的内联 JS 还是外部 JS 文件?无论如何,输出后无法通过 JS 输出 PHP 代码。
-
你们有什么克服这个问题的建议吗?