-2

I am trying to display a confirmation message when you click the icon. but when I click on the icon I get a message saying: Parse error: syntax error, unexpected 'return' (T_RETURN) in ... on line 42

I have tried to play around with the quotes but can't get it working.

This is the code on line 42:

echo "<td>".$row["id"]."</td><td>".$row['naam']."</td><td>".$row['adres']."</td><td>" . $row['email'] . "</td><td><a href='index.php'><i class='fas fa-pen'></i></a></td><td>
              <a href="delete.php?id=<?php echo $row["id"];?>"onclick="return confirm('Are you sure?');"><i class='fas fa-user-times'></i></a></td>";

Thanks in advance.

  • Can you post a little more than just line 42? Posting the whole block of code would help us in finding your answer. have you tried using single quotes for `$row["id"]` instead of using double quotes? – JeanPaul98 Nov 30 '18 at 21:00
  • @JeanPaul98 why would single quotes make a difference? – miken32 Nov 30 '18 at 21:05
  • I updated it to show the whole echo, and yes I have tried using `$row['id']` but that shouldn't do anything different – Mark van der Dam Nov 30 '18 at 21:05
  • Why are you using ` – miken32 Nov 30 '18 at 21:06
  • I have tried using `href='delete.php?id=" . $row['id'] ." '` but that didnt work too, so thought I would do it in the php tag but that also is not working so I don't know what to use – Mark van der Dam Nov 30 '18 at 21:08

2 Answers2

1

So, this is how things are typically done. Your PHP and HTML are separate – ideally in separate files, but at the very least you should not be outputting large chunks of your page with echo statements. Break out of PHP mode to output most of your page, only going back in when a variable needs to be output, or you need a control structure like a loop. In this code I used alternative syntax for control structures, and the short echo tag to make things (IMO) neater. Final note, use htmlspecialchars() to ensure values are escaped properly.

As for the JavaScript, you should be using event listeners to attach actions to elements, like the following example. This is easier with a library like jQuery, but is simple enough to do with native DOM code as well.

<?php
$conn = ...
$sql = "SELECT id, naam, adres, email FROM klanten";
$result = $conn->query($sql);
// do some error checking on the result before proceding
?>
<html>
...
<table>
<?php foreach($result->fetch_assoc() as $row): ?>
    <tr>
        <td><?=htmlspecialchars($row["id"])?></td>
        <td><?=htmlspecialchars($row["naam"])?></td>
        <td><?=htmlspecialchars($row["adres"])?></td>
        <td><?=htmlspecialchars($row["email"])?></td>
        <td>
            <a href='index.php'>
                <i class='fas fa-pen'></i>
            </a>
        </td>
        <td>
            <a href="delete.php?id=<?=htmlspecialchars($row["id"])?>" class="delete">
                <i class="fas fa-user-times"></i>
            </a>
        </td>
    </tr>
<?php endforeach ?>
</table>
...
<script>
    // look for every element with the "delete" class
    var links = document.getElementsByClass("delete");
    // loop over each one that we find
    for (var i = 0; i < links.length; i++) {
        // run a function when the element is clicked
        links[i].addEventListener("click", function(e) {
            return confirm("Are you sure?");
        }, false);
    }
</script>
miken32
  • 42,008
  • 16
  • 111
  • 154
  • Hey, I am getting a whole bunch of Illegal string offsets `$sql = "SELECT id, naam, adres, email FROM klanten"; $result = $conn->query($sql); ` is what I use to get the results then in the `foreach` I do `foreach ($result->fetch_assoc() as $row) :` and then your beautiful code – Mark van der Dam Nov 30 '18 at 21:35
  • Then that should work fine. "illegal string offset" suggests you're trying to treat a string like an associative array. – miken32 Nov 30 '18 at 21:38
  • shouldn't it show the actual data from the database instead of that Warning ? – Mark van der Dam Nov 30 '18 at 21:40
  • If `$row` is actually an associative array, yes it will. Is it possible you're not getting values returned from the database? – miken32 Nov 30 '18 at 21:42
  • That is weird, it gives me data from 1 record in the database, although I have 2 records. – Mark van der Dam Nov 30 '18 at 21:44
-1

This is not very practical nor is it an answer but rather a means of finding an answer.

Try doing this. We can fix/clean the code after we've learned which line the error is appearing at. If no error appears then we'll try and fix/clean the code too.

echo "<td>".$row['id']."</td>";
echo "<td>".$row['naam']."</td>";
echo "<td>".$row['adres']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td><a href='index.php'><i class='fas fa-pen'></a></td>";
echo "<td><a href='delete.php?id=".$row['id']." onClick='return confirm(\"Are you sure?\");'><i class='fas fa-pen'></a></td>";
miken32
  • 42,008
  • 16
  • 111
  • 154
JeanPaul98
  • 492
  • 6
  • 18
  • Well as I stated in the Question it appears on line 42 which is `echo "";` – Mark van der Dam Nov 30 '18 at 21:21
  • Replace line 42 with what i posted up top. We can toss it all back on one line after we've figured out which line the issue is coming from. Most likely the last echo statement is where things are going wrong. – JeanPaul98 Nov 30 '18 at 21:24
  • The line I added in this comment is where the problem is at – Mark van der Dam Nov 30 '18 at 21:27
  • You're making exactly the same mistake as in the question. Quotes are not escaped. At least you didn't use ` – miken32 Nov 30 '18 at 21:28
  • Using single quotes for the `Are you sure` confirm should fix it. If not the I'd go with mike's answer....(regardless I'd go with @miken32's answer) – JeanPaul98 Nov 30 '18 at 21:30
  • Single quotes would not fix it, because then you'd be breaking out of the `onclick` attribute. – miken32 Nov 30 '18 at 21:31