0

I'm trying to write some Javascript inside PHP, I made a simple button that takes you to the previous page. The button is showing but the functionality of the button isn't working. This is the code I have.

<?php
$product;
if (empty($product))
{
 echo
 '
 <script>
 function myFunction() {
 header("location:".history.go(-1));
 }
 </script>
 <button onclick=myFunction()>Click me</button>
 ';
 exit();
}

I go to my local server to the file where this code is and except it to work but it doesn't. For example, I go to http://127.0.0.1/test1.php where there is is a button (which works perfectly) that takes you to http://127.0.0.1/test2.php (where this this piece of code is). When I press the 'Click me' button, nothing happens. I'm wondering what I'm doing wrong? Or is it because It's local that the problems are caused? I've tried this in Chrome and Safari.

asked Apr 27, 2019 at 12:22
3
  • 4
    You are mixing PHP and JavaScript. They are two completely different languages. Commented Apr 27, 2019 at 12:25
  • 1
    @Dharman You're completely right! Thanks for pointing that out. Commented Apr 27, 2019 at 12:29
  • 3
    Check MDN article for Window.history. Take note of history.back() Commented Apr 27, 2019 at 12:29

1 Answer 1

1

You have wrong code history:

function myFunction() {
 window.history.go(-1);
}
answered Apr 27, 2019 at 12:26
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This solved my answer. I was indeed mixing stuff as @Dharman said but your code solves my issue perfectly!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.