0

I have a php function which ends with a refresh of the page and then throws a javascript function declared:

<script>
    var u = null;
    function tab(u) {
        $('#element1').css('display', 'block');
        $('#element2').css('display', 'none');
    }
</script>
<?php
    if (isset($_POST['submit1'])) {
        $tab = 1;
        header("Refresh:0");            
        echo "<script>window.scrollTo(0, 1000);</script>";
        echo "<script>tab(".$tab.");</script>";
    }
?>

The function of window.scrollto works, but the tab function not. I wrote ob_start and ob_end_flush to avoid headers problems.

6
  • Do you get any errors in your javascript console? Commented Jan 7, 2017 at 18:45
  • 1
    there might be a problem because you are using header function after generating an output... Commented Jan 7, 2017 at 18:46
  • No, I don't get errors in javascript console and php errors Commented Jan 7, 2017 at 18:46
  • I wrote ob_start and ob_end_flush to avoid headers problems Commented Jan 7, 2017 at 18:47
  • See here: stackoverflow.com/questions/1045845/… Commented Jan 7, 2017 at 18:49

1 Answer 1

2

You are missing a closing ) on the if statement. Try this:

  <script>
    var u = null;
    function tab(u) {
        $('#element1').css('display', 'block');
        $('#element2').css('display', 'none');
    }
</script>
<?php
    if (isset($_POST['submit1'])) {
        $tab = 1;
        header("Refresh:0");            
        echo "<script>window.scrollTo(0, 1000);</script>";
        echo "<script>tab(".$tab.");</script>";
    }
?>

The header("Refresh:0"); shoudn't be a problem as you said. In the worst case you will get an warning saying Warning: Cannot modify header information - headers already sent by....

Also, in development you should always have error_reporting turned on to be able to see the possible errors or warnings. You can turn on error reporting like this:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Don't forget to remove/comment that on production/live.

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

5 Comments

Good catch!. Should it not throw error on server side?
I have it like this but writing the question I made a mistake.
@SandeepNayak, it should have if he had the error reporting turned on.
@GerryStudios, let me check it again.
I've erased the header("Refresh:0") because I saw I don't need it and now it works, thanks.

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.