-3

Inside of my tags I have an echo statement which in it has the following line:

"<button style='onclick='window.location.href='https://www.google.com''>Go to Google</button>"

my understanding is that this should make it so that t would bring you to whatever link is in the onclick argument. My button however is not going anywhere when I click it

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
Gabe S.
  • 1
  • 1

2 Answers2

0

1st problem: You have onclick event inside of style which is wrong. These are 2 separate attributes. Use only onclick.

2nd problem: You have same quotation marks for all strings (inside/outside). You have to use different for inner arguments.

Valid solution:

<button onclick="window.location.href='https://www.google.com'">Go to Google</button>

Btw. I would rather use <a> instead of <button/>

Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • The problem is that this whole thing is inside a php echo statement. That means the whole line has to be inside of ""s and when I keep nesting quatation marks inside of each other it starts to screw it all up – Gabe S. Oct 11 '19 at 15:30
  • @GabeS. Use a backslash to escape quotation marks. [Escaping quotation marks in PHP](https://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php) – Jax-p Oct 11 '19 at 15:32
0

This should be what you are looking for

EDIT: edited to fit inside php echo,

function gotoPage(link){
window.location.href = "" +link;
}
$link = "hoi";
echo "<button onclick='gotoPage(".$link.")'>Go to Google</button>";
Ramon de Vries
  • 1,312
  • 7
  • 20