Click to See Complete Forum and Search --> : [RESOLVED] Form sumbit - refresh whole page


Baloo
06-14-2006, 06:35 PM
I have a form. The form is sent to the same page that the form submitted from. When the form is submitted i would like the entire page to be updated? How can i do this? At the moment, the only thing i can do is, after i've clicked submit, to press refresh, but that is like submitting the form twice. Help :confused:

Thank you

Baloo :)

Sheldon
06-14-2006, 06:57 PM
http://php.net/header header("Location: ". $_SERVER['PHP_SELF']);

Baloo
06-14-2006, 08:33 PM
Thank you for replying Sheldon. I have no idea where i should place it. I've tried many places but i keep getting...

Warning: Cannot modify header information - headers already sent by (output started at /home/www/baloo.disturbedmortality.com/php/koc/members.php:3) in /home/www/baloo.disturbedmortality.com/php/koc/members.php on line 4
This is my code...
Members List
</p>
<?
//connect to database
include("config.php");
mysql_connect($server,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM members";
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$name=mysql_result($result,$i,"name");

Echo "$name<br>
";

$i++;
}

?>

<table>
<tr>
<form action="members.php" method="post">
<td>Add member: </td>
<td><input type="text" name="addname">
<input type="Submit" value="Add"></td></tr>
</form>

<tr>
<form action="members.php" method="post">
<td>Delete member: </td>
<td><input type="text" name="delname">
<input type="Submit" value="Delete"></td></tr>
</form>
</table>
</p>

<?

$addname = $_POST['addname'];
$delname = $_POST['delname'];

//Was the add box sumbitted?
if (isset($addname)){

$query="SELECT * FROM members WHERE name='$addname'";
$result=mysql_query($query);
$num=mysql_numrows($result);

//Does the member alreadt exist?
if($num > 0){Echo "Error adding: Member $addname already exists.";

//Was a member's name actually submitted?
} elseif ($addname == ""){Echo "Error adding: No member specified.";

} else {
//Add the member to the database
$query = "INSERT INTO members VALUES ('','$addname')";
mysql_query($query);

Echo "$addname sucessfully added.";
} }

//Was the delete box sumbitted?
if (isset($delname)){

$query="SELECT * FROM members WHERE name='$delname'";
$result=mysql_query($query);
$num=mysql_numrows($result);

//Does the member exist to be deleted?
if($num == 0) {Echo "Error deleting: Member $addname does not exist.";

//Was a member's name actually submitted?
} elseif ($delname == ""){Echo "Error Deleting: No member specified.";

} else {
//Delete the member
$query = "DELETE FROM members WHERE name='$delname'";
mysql_query($query);

Echo "<p>$delname sucessfully deleted.";
} }
mysql_close();
?>

</p>

Baloo
06-16-2006, 09:39 AM
Anyone out there?

Frankie_CWRU
06-16-2006, 10:24 AM
since PHP is run server-side you cannot refresh a page once it has been sent to the browser.

javascript can do it with
location.refresh(true);

put that just after the mysql_close(); statement

Frankie_CWRU
06-16-2006, 10:27 AM
alternatively (if you don't want to use javascript) you could post to
"members.php?update=1"

then in
<html>
<head>
<?php
if($_GET['update'] == 1)
header("Location: members.php");
?>
</head>

In this case you would have to do all of your database operations before the refresh call.

Baloo
06-16-2006, 03:18 PM
Thank you for posting Frankie_CWRU. I dont really want to use javascript as not everyone has it enabled, and i dont really understand what you said in your second post :(

Basically, my page is:

- List of members
- Option to add/delete members

That's all it is. When i add a member, i would like 1) the "list of members" to be updated, or maybe resent, so that the new member appears in the listing, and 2) the variable values that were input for adding, to be dropped or forgotten (I'm not sure what the word is), so pressing F5 wont try to add them again.

For example, when i make a post on these forums, using the Quick Reply option, i dont have to refresh the entire page to see my post.

Thank you

Baloo :)

Baloo
06-17-2006, 11:03 AM
can someone help please? :(

Sheldon
06-18-2006, 03:19 AM
Pass, What are you trying to do with that mess of code?

Baloo
06-18-2006, 08:06 AM
What am i trying to do? Simply display a list of names on a page. Below the page have an option to add a name and an option to remove a name. That is all i am trying to do. Yes i am very new to php so i wouldn't expect code i write to be 100% efficient. Instead of just saying that it is a mess, you could give some ways in which it could be improved please. I'm sure i can make improvements, but i cant really see where, i haven't repeated anything in the code. I look forward to hearing you response!

Anyway. That was not my actual question. My question is, when i input a name to be added, and press the "add" button, i would to know how to also update the displayed list of names on the page. Otherwise, in order to see the newly added name in the list, i must manually refresh the page! Refreshing the page will try to add the same name to the list again. This is not efficient. Similarly, i would like to know how to do for for deleting a name

Thank you

Baloo

NogDog
06-18-2006, 09:27 AM
Make sure that any form processing which results in a change to the database is done before you query the database for the data that will be displayed. So the basic flow of the page (in pseudo-code) will be:

IF we got an add user request
THEN add the new user to the database
ELSE IF we got a delete user request
THEN delete that user from the database
query the database for current list of users
display the list of current users
display the form for adding a new user

Baloo
06-19-2006, 07:31 PM
Thank you NogDog! That did they trick :)

Baloo :)