A bit confusing code (a mixture of HTML, PHP and JS), but in fact everything is simple: I want to output the following HTML code through PHP:

<button onClick="document.location="admin-edit-head.php?id=1""> --- <button> 

I just can not understand: where do quotes come from before ID?

enter image description here

By the way, I previously sent the ID through a variable, but during the experiment I found out that with the direct input the same thing.

 echo '<tr><td><button onClick="document.location="admin-edit-head.php?id='.$row["ID"].// и т. д. 

Update

Another interesting effect: I inserted a semicolon between JS "optional" for JS, but, as you can see, this semicolon is no longer between two quotes (the first of which closes the address, the second - onClick="" ), but before them.

enter image description here

  • Better when the event is onclick, hang up either a function or make a class, when clicking on which this function will be called. And so it is nonsense, and so no one writes. <button onClick = "goToPage ()" /> </ button> - user190134

1 answer 1

In nature, there are not only double quotes, but also single quotes.
Like this

 <button onClick="document.location='admin-edit-head.php?id=1'"> 

right.

You can also escape quotes:

 echo '<tr><td><button onClick="document.location=\'admin-edit-head.php?id='.$row["ID"].'\'"'; 
  • Great, it works! Thank you for the answer! But what was the reason? - Hokov Gleb
  • one
    @GurebuBokofu, you cannot use the same quotes inside some quotes without escaping. - Visman