1

I have a page showing a random picture on each reload. This page only displays if a number is passed through $_GET (represents a user profile). To go to the next random picture I'd like a submit button to simply reload the page. However, I'm hitting two snags:

  • In PHP using the header function produces the infamous "headers already sent by" error. Plus the user is redirected because no parameter is passed.
  • In Javascript, a location.reload does reload the page but, again, the parameter is missing leading to the same result.

So my question is whether it is possible in Javascript to reload a page with a parameter? Or do you know of another solution?

Edit: Did some tests with an alert box and the url shows up as it appears in the adress bar. No idea why the parameter isn't being passed.

James P.
  • 19,313
  • 27
  • 97
  • 155

5 Answers5

2

You should wonder why you receive the headers already sent error. It is usually because your flow is incorrect. Make sure you handle your PHP before you generate the output. Check for the submitted form at the top of your code - and safely redirect without executing anything else. Should solve your problem.

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
  • As explained below @Berry Langerak's answer, there's a couple of `require_once` lines that will be difficult to move elsewhere. I've tried using `header` at the very start but no success so far. – James P. Jun 07 '11 at 11:47
2

In another topic i found that

window.location.replace(window.location.href)

will keep the parameters.

joakimdahlstrom
  • 1,575
  • 1
  • 11
  • 23
1

In PHP using the header function produces the infamous "headers already sent by" error.

That's simple enough: move the code that does the header( ) call up to the top (before any other output). "Headers already sent" is infamous, but also incredibly easy to solve.

In Javascript, a location.reload does reload the page but, again, the parameter is missing leading to the same result.

Then pass it along? This should work, I think?

window.location( window.location.toString( ) );
Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
  • Just tested this again with the ´toString´ as a variant. The result is the same and it's as if the parameter isn't being passed. The check near the top of the page is this: `isset($_GET['param']) && intval($_GET['param']) > 0`. As for the header error, this is most likely caused by a couple of `require_once` lines at the near top which do things like initialize the user and database connection. – James P. Jun 07 '11 at 11:19
1

Instead of location.reload the page, you can use document.location to redirect the page to the same page with a parameter passed through GET. For example,

<?php
    $id = $_GET['user_id'];
    echo "<script type='text/js'>
          document.location='".$_SERVER['PHP_SELF']."&id=".$id."';
          </script>";
?>
0

Ok, I might as well post this as it's been a pain getting something working through javascript and PHP and I don't have enough time to get something in ajax working. A simple workaround is to simply specify the parameter in the action attribute of the form (special markup is from smarty).

   <form Method="POST" ACTION="my_page.php?param={$smarty.get.i}">
          <!-- PHP: If ( isset($_POST['next']) ) do SQL ... -->
          <input type="submit" name="like" value="I like">
          <input type="submit" name="next" value="Next" >
   </form>
James P.
  • 19,313
  • 27
  • 97
  • 155