我有一个项目,我有一个PHP代码,在这里我必须回显HTML,它有JavaScript onclick函数()只接受双引号。问题是echo使用双引号,因此我的代码中存在语法错误,而且我是PHP的新手,所以我不知道如何解决这个问题。
以下是代码:
echo "
<span class = 'username'>".$row['username']."</span>
<form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
<input name = 'm-maker' type = 'number' class='price' min='5' max='100' value='5'/>
<div class = 'review'>
<p>REVIEW</p>
</div>
<button id ='send' type = 'button' onclick="Confirm.render('yes','no')">Send Challenge</button>
</form>
";
echo "<br>";
我知道这是一个语法错误,我不知道如何解决它。任何帮助将不胜感激。
答案 0 :(得分:2)
echo "
<span class = 'username'>".$row['username']."</span>
<form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
<input name = 'm-maker' type = 'number' class='price' min='5' max='100' value='5'/>
<div class = 'review'>
<p>REVIEW</p>
</div>
<button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
</form>
";
echo "<br>";
}
只需用\“
转义即可答案 1 :(得分:1)
您只需使用\
运算符将其转义即可。这告诉PHP不要将其视为PHP中的引用,而是将其视为字符串的一部分。一个例子
echo "<input name=\"send\" />";
会输出
<input name="send" />
因此,在您的特定情况下,onclick="Confirm.render('yes','no')"
需要对其双引号进行转义,例如:onclick=\"Confirm.render('yes','no')\"
您也可以在解析HTML时退出PHP并回显特定的PHP元素,这样可以使代码更简单,更清晰
<?php /*begin PHP here, and exit it */?>
<span class="username"><?php echo $row['username']; ?></span>
<form name "matchcreator" ><!-- rest of form -->
<?php // continue PHP...
答案 2 :(得分:0)
echo "
<span class = 'username'>".$row['username']."</span>
<form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
<input name = 'm-maker' type = 'number' class='price' min='5' max='100' value='5'/>
<div class = 'review'>
<p>REVIEW</p>
</div>
<button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
</form>
";
echo "<br>";
使用反斜杠double quote
\
答案 3 :(得分:0)
将onclick=\"Confirm.render('yes','no')\"
放在下面
<button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
答案 4 :(得分:0)
用双斜线划掉双引号。
echo "
<span class = 'username'>".$row['username']."</span>
<form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
<input name = 'm-maker' type = 'number' class='price' min='5' max='100' value='5'/>
<div class = 'review'>
<p>REVIEW</p>
</div>
<button id ='send' type = 'button' onclick=\"Confirm.render('yes','no')\">Send Challenge</button>
</form>
";
echo "<br>";
这适合我。
答案 5 :(得分:0)
使用它像这样:
echo '<span class = "username">'.$row['username'].'</span>
<form name = "matchcreator" class="amount" action="arena.php" method ="post">
<input name = "m-maker" type = "number" class="price" min="5" max="100" value="5" />
<div class = "review">
<p>REVIEW</p>
</div>
<button id ="send" type = "button" onclick="Confirm.render(yes,no)">Send Challenge</button>
</form>
';
echo '<br>';
答案 6 :(得分:0)
以下是您更正的更简洁的代码clean code。现在您只需要更改确认对话框按钮(是和否)。
?>
<span class = 'username'><?php echo $row['username']; ?></span>
<form name = 'matchcreator' class='amount' action='arena.php' method='post'>
<input name = 'm-maker' type = 'number' class='price' min='5' max='100' value='5'/>
<div class = 'review'>
<p>
REVIEW
</p>
</div>
<button id ='send' type = 'button' onclick="return confirm('are you sure?')">Send Challenge</button>
</form>
<br>
<?php
//rest of the code
?>
希望这会有所帮助。
答案 7 :(得分:0)
您也可以这样做:只需在{}
中编写代码即可echo "<span class = 'username'>{$row['username']}</span>
<form name = 'matchcreator' class='amount' action='arena.php' method ='post'>
<input name = 'm-maker' type = 'number' class='price' min='5' max='100' value='5'/>
<div class = 'review'>
<p>REVIEW</p>
</div>
<button id ='send' type = 'button' onclick={Confirm.render('yes','no')}>Send Challenge</button>
</form> <br>";
?>
答案 8 :(得分:0)
试试这个: -
{{1}}