尝试在 PHP 中 echo JS 函数

发布于 2024-12-18 08:09:17 字数 568 浏览 2 评论 0原文

我正在尝试在 PHP echo 中回显 HTML 字符串中的 js 函数。

我不明白:

$MY_JS_FUNCTION = '<script type="text/javascript">item + getLastField2()</script>';

if($row2->type == 'text') {
    echo "<li id='item-".$row2->rank."' class='list_item'>";
    echo "<textarea rows='2' id='".$row2->single_id."' cols='90' name='field[".$MY_JS_FUNCTION."]' data-kind='text' >".$row2->content."</textarea>";
    echo "</li>";
    echo '<br />';
}

有什么想法可以完成这项工作吗?我想我有很多引言或类似的东西......

任何帮助将非常非常感谢,谢谢!

I'm trying to echo a js function in HTML string inside PHP echo.

And I can't figure it out :

$MY_JS_FUNCTION = '<script type="text/javascript">item + getLastField2()</script>';

if($row2->type == 'text') {
    echo "<li id='item-".$row2->rank."' class='list_item'>";
    echo "<textarea rows='2' id='".$row2->single_id."' cols='90' name='field[".$MY_JS_FUNCTION."]' data-kind='text' >".$row2->content."</textarea>";
    echo "</li>";
    echo '<br />';
}

Any ideas to get this work? I think I have so much quotes in it or something like that...

Any help would be very very appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

維他命╮ 2024-12-25 08:09:17

我建议也将名称存储在数据库中。
然后你可以使用 $row2->name 插入正确的名称

I'd reccommend storing the name in the database as well.
Then you can use $row2->name to insert the right name

灯下孤影 2024-12-25 08:09:17

您的变量 $MY_JS_FUNCTION 包含一个 HTML

<textarea ... name='field[<script type="text/javascript">item + getLastField2()</script>]' ... >...</textarea>

This isdefinitely not valid HTML。还有你的问题...

Your variable $MY_JS_FUNCTION contains an HTML <script> tag with some (strange) JavaScript code (missing a semi-colon). Based on your code the echo on line 5 results in this HTML:

<textarea ... name='field[<script type="text/javascript">item + getLastField2()</script>]' ... >...</textarea>

This is definitively not valid HTML. And there is your problem...

櫻之舞 2024-12-25 08:09:17

看来您的意图是回显该 JS,以便在页面加载时,JS 实际上设置该文本区域的名称字段的值。如果是这种情况,一种更简单的方法可能是这样的:

$MY_JS_FUNCTION = '<script type="text/javascript">document.getElementById("myTextArea").name = item + getLastField2()</script>';

if($row2->type == 'text') {
    echo "<li id='item-".$row2->rank."' class='list_item'>";
    echo "<textarea rows='2' id='".$row2->single_id."' cols='90' id='myTextArea' data-kind='text' >".$row2->content."</textarea>";
    echo $MY_JS_FUNCTION;
    echo "</li>";
    echo '<br />';
}

这将生成有效的 HTML。一旦到达该行,JS 函数就会触发,并将“name”值更新为函数的结果。请务必添加“id”字段,以便 JS 知道要定位哪个元素。

It appears your intent is to echo that JS so that when the page loads, the JS actually sets the value of the name field for that textarea. If that's the case, a simpler way might be something like this:

$MY_JS_FUNCTION = '<script type="text/javascript">document.getElementById("myTextArea").name = item + getLastField2()</script>';

if($row2->type == 'text') {
    echo "<li id='item-".$row2->rank."' class='list_item'>";
    echo "<textarea rows='2' id='".$row2->single_id."' cols='90' id='myTextArea' data-kind='text' >".$row2->content."</textarea>";
    echo $MY_JS_FUNCTION;
    echo "</li>";
    echo '<br />';
}

That will produce valid HTML. The JS function will fire once that line is reached and update the "name" value to whatever the result of the function is. Be sure to add the "id" field so that the JS knows which element to target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文