英文:
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 '<td><a href="case.php?id=' . $row['id'] . '">' . $row['time'] . '</a></td>';
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 '<tr onclick="window.location="case.php?id=' . $row['id'] . '";"';
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['id'];
$url = "case.php?id=" . rawurlencode($id);
$javascript = "window.location=" . json_encode($url) . ";";
$html = '<tr onclick="' . htmlspecialchars($javascript) . '">';
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:
<?php
$id = $row['id'];
$url = "case.php?id=" . rawurlencode($id);
?>
<tr>
<td>
<a href="<?=htmlspecialchars($url)?>"> <?=htmlspecialchars($id);?></a>
</td>
// etc
<script>
$("tr").on("click", event => {
const tr = event.currentTarget;
const link = $(tr).find("a");
const url = link.attr("href");
location = url;
});
</script>
答案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
<?php
echo '<tr onclick="abc('.$row['id'].')"';
?>
JS:
function abc(id)
{
window.location="case.php?id="+id;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
- html
- javascript
- php
评论