我在PHP的echo语句中用完了引号。

huangapple go评论69阅读模式
英文:

I ran out of quotation marks in a PHP echo statement

问题

以下是您要翻译的内容:

我已经看到了一些解决方案,但似乎没有适用于我的情况,因为我不打算显示任何引号。

值得一提的是,可能有其他方法可以做到这一点,只是我现在没想到,所以绝对欢迎备选方案!

我有一个表格是从MySQL数据库生成的,我想要使每一行(tr)可点击。
我之前已经做到了这一点,但我使td标签可点击,这不太好,因为我必须点击文本,而不是容器中的任何位置。

这是我对td标签所做的事情

echo '<td><a href="case.php?id=' . $row['id'] . '">' . $row['time'] . '</a></td>';

这个方法有效,但如我所说,我必须点击文本本身,所以我放弃了这个想法。
对于tr标签使用相同的方法(a标签)是不受支持的,如果我尝试使用那种方法,什么都不会发生。

这是我现在对tr标签所尝试的事情。

echo '<tr onclick="window.location=\'case.php?id=' . $row['id'] . '\';">';

这不起作用,因为onclick的引号被window location的引号关闭了。

有没有其他设置窗口位置的方法?或者我可以使用其他方法来重定向用户吗?

英文:

I have seen some solutions to this, but none that seem to work in my case, as I'm not trying to display any quotation marks.

For the record, there is probably some other method of doing this that I'm just not thinking of right now, so alternative solutions are definitely welcome!

I have a table that is generated from a MySQL database, and i want to make each row (tr) clickable.
I had already done this, but i made the td tags clickable instead, which didn't work as well since i had to click the text, not anywhere in the container.

This is what i did for the td tags

echo &#39;&lt;td&gt;&lt;a href=&quot;case.php?id=&#39; . $row[&#39;id&#39;] . &#39;&quot;&gt;&#39; . $row[&#39;time&#39;] . &#39;&lt;/a&gt;&lt;/td&gt;&#39;;

This worked as i said, but i had to click the text itself, so i scrapped the idea.
Using the same method (a tags) for the tr tags is not supported, and nothing happens if i try using that method.

This is what i have tried doing to the tr tags now.

echo &#39;&lt;tr onclick=&quot;window.location=&quot;case.php?id=&#39; . $row[&#39;id&#39;] . &#39;&quot;;&quot;&#39;;

This doesn't work, as the onclick quotation mark is closed by the window location quotation mark.

Is there some other way to set the window location? Or can i use some other method to redirect the user?

答案1

得分: 4

以下是您要翻译的内容:

通常,最容易逐步构建嵌套的语言结构,使用标准函数来逐层转义它。

例如:

$id = $row['id'];
$url = "case.php?id=" . rawurlencode($id);
$javascript = "window.location=" . json_encode($url) . ";";
$html = '

';
echo $html;

尽管如此,尽量避免尽可能多地嵌套不同的语言更好。

您可以通过在行内使用常规链接,并将JS移到单独的事件处理程序中来处理(我在此示例中使用了jQuery,因为它更方便),如果点击行的任何部分,它将从该链接获取URL:

// 等等

英文:

It is generally easiest to build your nested language structures piece by piece using standard functions to escape each level of it.

For example:

$id = $row[&#39;id&#39;];
$url = &quot;case.php?id=&quot; . rawurlencode($id);
$javascript = &quot;window.location=&quot; . json_encode($url) . &quot;;&quot;;
$html = &#39;&lt;tr onclick=&quot;&#39; . htmlspecialchars($javascript) . &#39;&quot;&gt;&#39;;
echo $html;

That said, it's generally even better to avoid nesting different languages as much as possible.

You could approach it by using a regular link inside the row and moving the JS into a separate event handler (I'm using jQuery for this example as it makes it more convenient) which will get the URL from that link if any part of the row is clicked:

&lt;?php
    $id = $row[&#39;id&#39;];
    $url = &quot;case.php?id=&quot; . rawurlencode($id);
?&gt;

&lt;tr&gt;
    &lt;td&gt;
         &lt;a href=&quot;&lt;?=htmlspecialchars($url)?&gt;&quot;&gt; &lt;?=htmlspecialchars($id);?&gt;&lt;/a&gt;
    &lt;/td&gt;
    // etc

&lt;script&gt;
    $(&quot;tr&quot;).on(&quot;click&quot;, event =&gt; {
        const tr = event.currentTarget;
        const link = $(tr).find(&quot;a&quot;);
        const url = link.attr(&quot;href&quot;);
        location = url;
    });
&lt;/script&gt;

答案2

得分: 0

以下是翻译好的部分:

一个 JavaScript 函数在被"某事"调用时执行。您可以在单击 tr 时随时调用 abc() 函数。

您可以这样做:

<?php
echo '<tr onclick="abc(' . $row['id'] . ')">';
?>

JS:

function abc(id)
{
    window.location="case.php?id=" + id;
}
英文:

> A JavaScript function is executed when "something" invokes it (calls
> it). You can call the abc() function whenever tr is clicked.

You can do like

&lt;?php
echo &#39;&lt;tr onclick=&quot;abc(&#39;.$row[&#39;id&#39;].&#39;)&quot;&#39;;
?&gt;

JS:

function abc(id)
{
	window.location=&quot;case.php?id=&quot;+id;
}

huangapple
  • 本文由 发表于 2020年1月6日 19:18:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定