【发布时间】:2024-01-07 10:12:01
【问题描述】:
我的网站有一些 PHP 生成的内容与 HTML 元素相呼应。其中一些元素响应 javascript 事件...对于一个输入元素,相关事件是 onmouseout,但我似乎无法正确转义。
$sql = mysqli_query($cxn, "SELECT stuff1, stuff2, stuff100, tags FROM myTable WHERE user_id = 'myID'");
while ($row = mysqli_fetch_assoc($sql)) {
$Tagstring = $row['tags'];
//lots of code
echo "<div class='myClass1 myClass2'>
<input type='text' name='myInput' value='".$Tagstring."' onmouseout='ajaxFunction(\"myString\", this.value.trim().replace(/,\s|\s,/g, ","))'>
</div>";
//more code
}
$Tagstring 是一个逗号分隔的文本子字符串。如果用户编辑他/她的标签,我试图阻止以下情况:
$Tagstring = 'tag1,tag2'; //from database table
//User edits to 'tag1, tag2';
//Pointless ajax call and access of server, since if user input = original $Tagstring, this will return false as I have set up the ajax call, but if user input !== $Tagstring, then ajax function proceeds
我对 PHP 和 Javascript 并不陌生,所以我知道 PHP 中的 str_replace 或在 "," 上分解用户输入,然后修剪分解数组的每个成员。或者,我可以使用 Javascript 在 "," 上拆分,然后在 for 循环中修剪片段(或类似的东西)。
谁能告诉我如何在我的回显函数调用中正确转义以下参数?
this.value.trim().replace(/,\s|\s,/g, ",")
【问题讨论】:
-
对于大的
echo,关闭然后重新打开PHP标签更简单。 -
$TagString与$Tagstring不同。 -
好的...错字已修正...我的错误
-
现在出现语法错误。你关闭字符串太早了。
-
@Blackhole....很难做到这一点,因为 div 非常大且复杂(未显示)并且会根据用户输入更改 HTML、文本和类