2

in PHP how can I redirect my page once scrpit has run with a successful case 0 but stay on current page for anything else?

exec( $command, $output = array(), $worked );

switch( $worked ) {

    case 0:  
        echo "Database <b>". $database. "</b> successfully exported to <b>". $filename. '</b>';
      // add redirect here
        break;

    case 1:
        echo 'There was a warning during the export of <b>'. $database .'</b> to <b>'. $filename .'</b>';
        break;

    case 2:

        echo 'There was an error during import.'
            . 'Please make sure the import file is saved in the same folder as this script and check your values:'
            . '<br/><br/><table>'
            . '<tr><td>MySQL Database Name:</td><td><b>'. $database .'</b></td></tr>'
            . '<tr><td>MySQL User Name:</td><td><b>'. $user .'</b></td></tr>'
            . '<tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr>'
            . '<tr><td>MySQL Host Name:</td><td><b>'. $host .'</b></td></tr>'
            . '<tr><td>MySQL Import Filename:</td><td><b>'. $filename .'</b></td>'
            . '</tr></table>'
        ;
        break;
}
4
  • 1
    write this line in case 0 redirect('another_page', 'refresh'); Commented Jun 21, 2017 at 12:52
  • You cannot echo before a redirect because you'll get an error. Commented Jun 21, 2017 at 12:53
  • doing it as redirect would it be redirect("page.php", "refresh); or full url? Commented Jun 21, 2017 at 12:59
  • 1
    redirect is code igniter function if you are working in core php so you can need to use header("Location : your full urll"); Commented Jun 21, 2017 at 13:01

1 Answer 1

1

If you don't mind removing your "echo" before the redirect you can redirect with header

 header("Location: http://you-url");

If not you can use javascript

<script> window.location = "http://your-url"; </script>

And you could add something to delay the redirect or put your "successfully exported" message on an alert before the redirect, otherwise the user might not see the message if the redirect works too fast.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.