Hi,

I have put a comments box on the bottom of a page and want the page to re-load after the comment is submitted so that the user can see their comment on the page. The page name is photo.php?date=20120101 where the date string can be different depending on which photo the user is viewing. Users do not need to log in to leave feedback. I've pasted my code below (that comes after the form being defined etc.) - any tips on why it's not working would be much appreciated!

The problem is that after submitting a post, the user is returned to photo.php with the value of today's date (which is the behaviour of index.html) rather than the specified date, i.e. going to photo.php?date=20120101 because that's today rather than photo.php?date=20111214 if that's what I've tried to specify in the code.

if($POST) {
if((!$
POST["post_text"]) || (!$POST["post_owner"]) || (!$POST["post_id"])) {
echo "<p>PLEASE ENSURE YOU ADD BOTH YOUR NAME AND A COMMENT BEFORE SUBMITTING</p>";
}

$add_post_sql="[removed for clarity]!;
$add_post_res=mysql_query("{$add_post_sql}",$cyoyo) or die("Could not complete post");
mysql_close($cyoyo);
$newurl="photo.php?date=".$imgdate;
header("Location: ".$newurl);

It's not that the script doesn't work at all:
- Comments are posted successfully
- If I echo $imgdate, the correct date is returned
- If I echo $newurl, the correct url is returned
- If I manually navigate in the browser to photo.php with a different value for date, the site behaves as I would expect (i.e. you navigate to that photo, not today's)

I'm not sure if the above is clear - please let me know if not!

Thanks,

Pav

    The problem is that after submitting a post, the user is returned to photo.php with the value of today's date (which is the behaviour of index.html) rather than the specified date, i.e. going to photo.php?date=20120101 because that's today rather than photo.php?date=20111214 if that's what I've tried to specify in the code.

    if u tell php to redirect to photo.php, I do not think it would load the index.html. Anyways, pls post the echoed strings.

      Also read the documentation on the function [man]header[/man]. You cannot send headers once your script has echoed anything. Your script has an echo statement before you try to call header. You have to send all your headers first and then start echoing stuff.

        sneakyimp;10993919 wrote:

        You have to send all your headers first and then start echoing stuff.

        And if the former includes a 'Location:' header, the latter might as well not be done at all (since it won't do any good other than waste bandwidth by sending data that is simply going to be ignored by the client's browser).

          There you go again, BG. Always up-rocking me. Can I borrow some of that ritalin? 🙂

          Yes if you plan to do a header redirect, your next command should probably be [man]die[/man] or [man]exit[/man].

            Thanks for the suggestions - interesting to note that you can't change the headers after echoing anything, apologies for missing that whilst reading up on it. I've tried to get round it by rather than using a header going for this after closing the db connection (i.e. right at the end of the page):

            echo "<META HTTP-EQUIV=\"Refresh\" Content=\"0; URL=photo.php?date=",$imgdate,"\">";

            However, whilst a redirect of sorts works, I'm still getting the same issue where the behaviour of index.html is being mimicked rather than going to photo.php?date=$imgdate, even though $imgdate at this point echoes just fine (e.g. 20120101)

              sneakyimp;10993927 wrote:

              There you go again, BG. Always up-rocking me. Can I borrow some of that ritalin? 🙂.

              I suppose you could have some of mine - I'm not really using it. I wouldn't expect it ba ... ooh, kitteh!

                Write a Reply...