1

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?

Kossi
  • 77
  • 2
  • 10
  • 1
    the key is, inline js is a really bad practice. As is using PHP to create JS functions - best bet is to separate the languages into separate files and pass data between them – treyBake Jan 06 '20 at 11:21
  • Is this related to your question?: https://stackoverflow.com/questions/17147821/how-to-make-a-whole-row-in-a-table-clickable-as-a-link – tbijl Jan 06 '20 at 11:21
  • 1
    Just produce `onclick="window.location='newlocation';"` with single quotes after the equals sign? Although, you're better off just writing proper JS in the first place. – VLAZ Jan 06 '20 at 11:22
  • try this echo ' – Gaurav Kandpal Jan 06 '20 at 11:27
  • 1
    @GauravKandpal — That won't work. the value of the `onclick` attribute is now ```window.location=\```. A ```\``` is not an escape character in HTML. – Quentin Jan 06 '20 at 11:30
  • Try this echo ""; ?> – Trupti Barad Jan 06 '20 at 11:58
  • @TruptiBarad That sends me to "/undefined" for some reason.. I _think_ when you use a backslash, it treats the quotation marks as text or a string, not an operator, so im pretty sure thats why it fails. **Maybe...** – Kossi Jan 06 '20 at 12:13

2 Answers2

4

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>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I tried your second version, but that generates the url "https://example.com/help/%3C?=htmlspecialchars($url)?%3E", not for example "https://example.com/help/case.php?id=14". Have you tested this method? There is a chance im doing something wrong here, so your code may still be correct. Do you have any idea why im getting this malformed URL? - Thanks for the answer by the way! – Kossi Jan 06 '20 at 12:04
  • 1
    @Kossi — It works fine [when I test it](https://imgur.com/fou9VZT). Maybe you are trying to put the HTML inside a `` on line three of my example. – Quentin Jan 06 '20 at 12:19
  • That was it, i didn't even notice that :P It works! I know its not quite related, but do you by any chance know how i can set a :hover property for the tr tag? As is i can only set a :hover property for the td tag, but i want the whole row to be affected, not just one td.. If not, thanks for the help anyways! I marked this as the answer :) – Kossi Jan 06 '20 at 12:33
  • `tr:hover { /* whatever */ }`. – Quentin Jan 06 '20 at 12:34
  • That's what i tried, but it doesn't do anything :/ I guess i'll have to pose a new question :'( Thanks anyways! – Kossi Jan 06 '20 at 12:49
  • I cant seem to edit my comment, but i got it to work! I had set the color of the td to grey, so when the tr changed color, nothing happened.. Easy fix :) – Kossi Jan 06 '20 at 12:56
0

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;
}
Amanjot Kaur
  • 2,028
  • 4
  • 18
  • 33