-1

Anyone know how to call goBack() in php?

It keep telling me:

Fatal error: Call to undefined function goBack()

and I don't know why..

Here is my PHP Code:

<?php
include "config.php";
include "session_user.php";
?>
    <script>
        function goBack(){
            alert();
            window.history.go(-1);
        }

    </script>

    <?php
    if(isset($_GET['d'])):

         $stmt = $mysqli->prepare("DELETE FROM bom_equipment WHERE bom_eqp_id=?");
         $stmt->bind_param('s', $id);

         $id = $_GET['d'];

         if($stmt->execute()):
             goBack();
         else:
              echo "<script>alert('".$stmt->error."')</script>";
         endif;
    endif;
    ?>

Any suggestion?

Guillaume
  • 586
  • 3
  • 21
  • 1
    You can't call a Javascript function from PHP like that. Javascript isn't read until it's already been sent to the client's browser. – Carcigenicate Jul 14 '17 at 03:13
  • 1
    Your `goBack()` function is a JavaScript function. As far as the PHP is concerned the whole ` – nnnnnn Jul 14 '17 at 03:13
  • 1
    Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) –  Jul 14 '17 at 03:15
  • 1
    so, is there any suggestion to make it right? im only want to make it go previous page, but i dont want use location – Christian Anthony Jul 14 '17 at 03:15
  • 1
    @ChristianAnthony Calling goBack on the server side doesn't make sense. If you want the client to go back a page, you need to call the function on the client side. Read the suggested duplicate. – Carcigenicate Jul 14 '17 at 03:17
  • You can redirect to `$_SERVER['HTTP_REFERER']` – granch Jul 14 '17 at 03:20

1 Answers1

1

If you want to call javascript function from php. It has lot of ways

Change your code to follows,

Method 1

<?php
include "config.php";
include "session_user.php";
?>
    <script>
        function goBack(){
            alert();
            window.history.go(-1);
        }

    </script>

    <?php
    if(isset($_GET['d'])):

         $stmt = $mysqli->prepare("DELETE FROM bom_equipment WHERE bom_eqp_id=?");
         $stmt->bind_param('s', $id);

         $id = $_GET['d'];

         if($stmt->execute()):
           echo "<script>goBack();</script>";
         else:
              echo "<script>alert('".$stmt->error."')</script>";
         endif;
    endif;
    ?>

Method 2

<?php
include "config.php";
include "session_user.php";

    if(isset($_GET['d'])):

         $stmt = $mysqli->prepare("DELETE FROM bom_equipment WHERE bom_eqp_id=?");
         $stmt->bind_param('s', $id);

         $id = $_GET['d'];

         if($stmt->execute()):
            echo "<script>histort.back()</script>";
         else:
              echo "<script>alert('".$stmt->error."')</script>";
         endif;
    endif;
    ?>
Sahathulla
  • 314
  • 2
  • 14
  • @ChristianAnthony Note that neither of these ways "call javascript function from php". These just produce a call to `goBack` that will be run on the client side. – Carcigenicate Jul 14 '17 at 13:09