通过PHP将MySQL数据作为字符串传递给javascript
我正在尝试创建一个社交媒体网站。 在第四行中,通过PHP将MySQL数据作为字符串传递给javascript,javascript,php,html,Javascript,Php,Html,我正在尝试创建一个社交媒体网站。 在第四行中,onclick='func\u cmnt\u open(.$row['post\u ID'])应显示每个条目唯一的div。这似乎是一个语法错误,但如果它是在PHP或javascript中,我无法缩小它的范围 ... onclick='func_cmnt_open(".$row['post_ID'].")'... 在第6行中,id属性被设置为注释“$row['post_id']”。以使每个div在while循环迭代中唯一 if (mysqli_nu
onclick='func\u cmnt\u open(.$row['post\u ID'])
应显示每个条目唯一的div。这似乎是一个语法错误,但如果它是在PHP或javascript中,我无法缩小它的范围
... onclick='func_cmnt_open(".$row['post_ID'].")'...
在第6行中,id属性被设置为注释“$row['post_id']”。
以使每个div在while循环迭代中唯一
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h4>".$row["username"]." said ".$row["cm_text"]." at ".$row["time"]." comment".$row['post_ID']."</h4>";
echo "<button class='w3-button w3-teal w3-xlarge' onclick='func_cmnt_open(".$row['post_ID'].")'>☰</button>";
echo "<button class='w3-button w3-teal w3-xlarge' onclick='func_like()' id='like'>☰</button>";
echo "<div class='w3-black' id='comment".$row['post_ID']."' style='display: none'>";
echo " <div class='w3-container''>";
echo " <h4>";
echo " ///code was here ";
echo " </h4>";
echo " <button class='w3-button w3-grey w3-xlarge' onclick='document.getElementById('id02').style.display='block''>☰</button>";
echo " <div id='id02' class='w3-modal'>";
echo " <div class='w3-modal-content w3-card-4'>";
echo " <header class='w3-container w3-teal'> ";
echo " <span onclick='document.getElementById('id02').style.display='none'' class='w3-button w3-display-topright'>×</span>";
echo " ///code was here";
echo " </header>";
echo " <div class='w3-container'>";
echo " <form class='w3-container' action='home.php' method='GET'>";
echo " <p><input type='text' class='w3-input' name='post_comment' placeholder='Whats happening there...'/></p>";
echo " <p><button class='w3-button w3-black'>Post</button></p>";
echo " </form>";
echo " </div>";
echo " <footer class='w3-container w3-teal'>";
echo " <p>nothing here</p>";
echo " </footer>";
echo " </div>";
echo " </div>";
echo " </div>";
echo "</div>";
}
}
输出:
我不确定这是最有效的方法。如果有任何其他方法也可以实现相同的输出,请给出建议。如果
post\u ID
不是严格意义上的字符串,则Javascript中会出现语法错误
... onclick='func_cmnt_open(".$row['post_ID'].")'...
考虑上述值1
将在页面源代码中创建此值
... onclick='func_cmnt_open(1)'...
现在考虑这个值<代码> POST 1/代码>
... onclick='func_cmnt_open(post1)'...
在第二种情况下,应该是
... onclick='func_cmnt_open("$row['post_ID']")'...
要在PHP中以echo的方式实现这一点,必须对双引号进行转义,否则属性中的“
也会导致问题。所以像这样:
... onclick='func_cmnt_open(\"".$row['post_ID']."\")'...
还有一些其他的引用问题,onclick='document.getElementById('id02').style.display='block''>,
,我不知道是否都有,但应该更好
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h4>{$row["username"]} said {$row["cm_text"]} at {$row["time"]} comment{$row['post_ID']}</h4>
<button class='w3-button w3-teal w3-xlarge' onclick='func_cmnt_open(\"{$row['post_ID']}\")'>☰</button>
<button class='w3-button w3-teal w3-xlarge' onclick='func_like()' id='like'>☰</button>
<div class='w3-black' id='comment{$row['post_ID']}' style='display: none'>
<div class='w3-container''>
<h4>
///code was here
</h4>
<button class='w3-button w3-grey w3-xlarge' onclick='document.getElementById(\"id02\").style.display=\"block\"'>☰</button>
<div id='id02' class='w3-modal'>
<div class='w3-modal-content w3-card-4'>
<header class='w3-container w3-teal'>
<span onclick='document.getElementById(\"id02\").style.display=\"none\"' class='w3-button w3-display-topright'>×</span>
///code was here
</header>
<div class='w3-container'>
<form class='w3-container' action='home.php' method='GET'>
<p><input type='text' class='w3-input' name='post_comment' placeholder='Whats happening there...'/></p>
<p><button class='w3-button w3-black'>Post</button></p>
</form>
</div>
<footer class='w3-container w3-teal'>
<p>nothing here</p>
</footer>
</div>
</div>
</div>
</div>";
}
}
if(mysqli\u num\u行($result)>0){
while($row=mysqli\u fetch\u assoc($result)){
echo“{$row[“username”]}在{$row[“time”]}注释{$row['post_ID']}处说{$row[“cm_text”]}
☰
☰
如果post\u ID
不是严格意义上的字符串,则Javascript中会出现语法错误
... onclick='func_cmnt_open(".$row['post_ID'].")'...
考虑上述值1
将在页面源代码中创建此值
... onclick='func_cmnt_open(1)'...
现在考虑这个值<代码> POST 1/代码>
... onclick='func_cmnt_open(post1)'...
在第二种情况下,应该是
... onclick='func_cmnt_open("$row['post_ID']")'...
要在PHP中以echo的方式实现这一点,必须对双引号进行转义,否则属性中的“
也会导致问题。如下所示:
... onclick='func_cmnt_open(\"".$row['post_ID']."\")'...
还有一些其他的引用问题,onclick='document.getElementById('id02').style.display='block''>,
,我不知道是否都有,但应该更好
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<h4>{$row["username"]} said {$row["cm_text"]} at {$row["time"]} comment{$row['post_ID']}</h4>
<button class='w3-button w3-teal w3-xlarge' onclick='func_cmnt_open(\"{$row['post_ID']}\")'>☰</button>
<button class='w3-button w3-teal w3-xlarge' onclick='func_like()' id='like'>☰</button>
<div class='w3-black' id='comment{$row['post_ID']}' style='display: none'>
<div class='w3-container''>
<h4>
///code was here
</h4>
<button class='w3-button w3-grey w3-xlarge' onclick='document.getElementById(\"id02\").style.display=\"block\"'>☰</button>
<div id='id02' class='w3-modal'>
<div class='w3-modal-content w3-card-4'>
<header class='w3-container w3-teal'>
<span onclick='document.getElementById(\"id02\").style.display=\"none\"' class='w3-button w3-display-topright'>×</span>
///code was here
</header>
<div class='w3-container'>
<form class='w3-container' action='home.php' method='GET'>
<p><input type='text' class='w3-input' name='post_comment' placeholder='Whats happening there...'/></p>
<p><button class='w3-button w3-black'>Post</button></p>
</form>
</div>
<footer class='w3-container w3-teal'>
<p>nothing here</p>
</footer>
</div>
</div>
</div>
</div>";
}
}
if(mysqli\u num\u行($result)>0){
while($row=mysqli\u fetch\u assoc($result)){
echo“{$row[“username”]}在{$row[“time”]}注释{$row['post_ID']}处说{$row[“cm_text”]}
☰
☰
你知道php可以处理多行字符串,对吗?这意味着你可以一次调用echo
。JavaScript不能,但php可以。post ID是数字还是字符串?post_ID是整数。从数据库中。它是自动递增的。@ArtisticPhoenix我是php新手。我不知道。当我尝试输入警报(num)时它显示了不同的post_ID。当我手动输入ID时(例如comment2或comment3)它提供了所需的输出,但只针对一个div。请检查图像文件,您可能会得到更好的想法。我相信是GetElementbyID语法出了问题。您知道php可以处理多行字符串,对吗?这意味着您可以一次调用echo
。JavaScript不能,但php可以。post ID是数字还是字符串?post_ID是一个整数。从数据库中。它是自动递增的。@ArtisticPhoenix我是PHP新手。我不知道。当我尝试输入警报(num)时,它显示了不同的post_ID。当我手动输入ID时(例如,comment2或comment3)它提供了所需的输出,但只针对一个div。请检查图像文件,您可能会得到更好的想法。我相信是GetElementbyID语法出了问题。非常感谢它的工作。您是否可以帮助清理代码混乱。代码看起来有点太大太多了。您是否可以帮助清理代码mess也是。代码看起来有点太庞大了