By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,719 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,719 IT Pros & Developers. It's quick & easy.

refresh on submit

P: n/a
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
:)

}

thanx in advance

Pat
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<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

--
I'll send you a gMail invite if you sign up for a free iPod and complete an
offer:
http://www.freeiPods.com/default.aspx?referer=9228418
Jul 17 '05 #2

P: n/a
.oO(ki*********@yahoo.com)

Just some hints:
echo "<form method=\"post\" action=$php_self>";


If you want your script to run on all servers you should read the manual
about register_globals.

Chapter 27. Using Register Globals
http://www.php.net/manual/en/security.globals.php

register_globals
http://www.php.net/manual/en/ini.sec...gister-globals

Additionally HTML also allows single quotes around attribute values,
this avoids ugly escaping:

echo "<form method='post' action='$_SERVER[PHP_SELF]'>";

Micha
Jul 17 '05 #3

P: n/a
Michael Fesser <ne*****@gmx.net> wrote:
echo "<form method='post' action='$_SERVER[PHP_SELF]'>";


If you are trying to advocate good practice, please don't write sloppy
code :)

echo "<form method='post' action='{$_SERVER["PHP_SELF"]}'>";

E_ALL should be the default on development machines when it comes to
error reporting :)

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
.oO(Daniel Tryba)
Michael Fesser <ne*****@gmx.net> wrote:
echo "<form method='post' action='$_SERVER[PHP_SELF]'>";
If you are trying to advocate good practice, please don't write sloppy
code :)


The above is correct (simple) syntax. Really. ;)

(The assoc. array is accessed directly from inside the string, quoting
the index would cause a parse error.)
echo "<form method='post' action='{$_SERVER["PHP_SELF"]}'>";
This is correct either (complex/curly syntax).

(In this case the curly braces are kind of an escaping mechanism to
allow more complex expressions, one could say the array is accessed
outside the string and hence needs quotes around the index.)
E_ALL should be the default on development machines when it comes to
error reporting :)


Full ACK

And believe me, this is one of the first settings I change after an
initial PHP installation on a dev-machine.

Micha
Jul 17 '05 #5

P: n/a
Michael Fesser <ne*****@gmx.net> wrote:
echo "<form method='post' action='$_SERVER[PHP_SELF]'>";


If you are trying to advocate good practice, please don't write sloppy
code :)


The above is correct (simple) syntax. Really. ;)

(The assoc. array is accessed directly from inside the string, quoting
the index would cause a parse error.)


Note to self, test before port next time :)

--

Daniel Tryba

Jul 17 '05 #6

P: n/a
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

Jul 17 '05 #7

P: n/a
<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
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.