<ki*********@yahoo.com> wrote:
I tried to use "header("Location: ".$_SERVER["PHP_SELF"]);" but I get
an error telling that the header has already been sent before at a
line where I use an echo "something"
"Christopher Finke" <cf****@gmail.com> wrote in message news:<2r*************@uni-berlin.de>... <ki*********@yahoo.com> wrote in message
news:39**************************@posting.google.c om...I have a php script with a form that insert data in a mysql db and
when I click on submit I would like the page to refresh after the
insertion, how can I do that? it's a php script that display data from
a mysql db, and the submit button modify the content of the page yet I
need to manually refresh to see the result of my insertion.
it kinda looks like this:
echo "<form method=\"post\" action=$php_self>";
echo "<br>$dispayed_colname:<BR><INPUT TYPE=\"TEXT\" NAME=\"hey\"
SIZE=\"40\">";
echo "<p><input type=\"submit\" name=\"submit_the_values\"
value=\"$submit\">
</form>";
if($submit_the_values){
$sql=mysql_query("INSERT INTO $tabname($cols_to_insert)". "VALUES
($hey)");
//I would like to refresh $php_self here, please tell me if you know
:)
}
You could move the query code to the top of the page so that the insertion
would already be done by the time the data is displayed:
<?php
if($submit_the_values){
$sql=mysql_query("INSERT INTO $tabname($cols_to_insert)". "VALUES
($hey)");
}
// ... other stuff
?>
If, for some reason, this isn't satisfactory, you can refresh with this
line:
header("Location: ".$_SERVER["PHP_SELF"]);
Chris Finke
1.: According to the RFC, the Location:-Header *has* to be an abolute URL.
2.: Replace header() with print() and look into the generated HTML. While
there are *any* characters in the file before the Location:-part, you have
to move your code upwards in your .php.
An HTTP-response looks like this:
-----BEGIN HTTP BLOCK-----
1: HTTP/1.1 200 OK
2: Content-Type: text/plain
3: Content-Encoding: foo
4:
5: hey, i am the content of the sent text file!
------END HTTP BLOCK------
Line 1-3 are the HTTP header (yes, exactly as in "header()"), followed by
an empty line, followed by the body (on View > Sourcecode, you are only
shown the body).
If output buffering is turned off, PHP will send the prepared headers and
the empty line to the client and starts sending the body, as soon as there
is any data outside of a <?php...?>-block or any output within a php-block
(no matter, whether it's echo, var_dump or a parse error).
As soon as the empty line is sent out, you can *never* get back into the
header area to add something you forgot. It's like saying hurting words to
a loved one: You want to undo it, but no matter how hard you wish or try,
it's impossible to make it undone.
Ok... sorry, back to topic :)
If you want to send headers, you have to put them at the very beginning of
you script. If you have to validate data and while doing that generating
output, instead of just echoing this output write it into a variable and
echo the variable at the correct location.
HTH
Simon
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle