/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Form sumbit – refresh whole page

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 ?

Thank you

Baloo ?

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@SheldonJun 14.2006 — http://php.net/header header("Location: ". $_SERVER['PHP_SELF']);
Copy linkTweet thisAlerts:
@BalooauthorJun 15.2006 — 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[/QUOTE]
This is my code...
[code=php]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>[/code]
Copy linkTweet thisAlerts:
@BalooauthorJun 16.2006 — Anyone out there?
Copy linkTweet thisAlerts:
@Frankie_CWRUJun 16.2006 — 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
Copy linkTweet thisAlerts:
@Frankie_CWRUJun 16.2006 — 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.
Copy linkTweet thisAlerts:
@BalooauthorJun 16.2006 — 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 ?
    Copy linkTweet thisAlerts:
    @BalooauthorJun 17.2006 — can someone help please? ?
    Copy linkTweet thisAlerts:
    @SheldonJun 18.2006 — Pass, What are you trying to do with that mess of code?
    Copy linkTweet thisAlerts:
    @BalooauthorJun 18.2006 — 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
    Copy linkTweet thisAlerts:
    @NogDogJun 18.2006 — Make sure that any form processing which results in a change to the database is done [i]before[/i] you query the database for the data that will be displayed. So the basic flow of the page (in pseudo-code) will be:
    <i>
    </i>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
    Copy linkTweet thisAlerts:
    @BalooauthorJun 20.2006 — Thank you NogDog! That did they trick ?

    Baloo ?
    ×

    Success!

    Help @Baloo spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...