PHP: Refresh page or variables [Solved]

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Eddy Parera
Posts: 18
Joined: Mon Feb 06, 2012 7:54 pm

PHP: Refresh page or variables [Solved]

Post by Eddy Parera »

Well, I have a page that is called updateBudget.php, and I have the PHP script above the html form to retrieve information...

My problem is, I need to refresh the page by itself to get some variables clean, so if the user hit refresh of the page, it doesn't re-run the script and to prevent double actions...

But I have already a header.php and it gives me back this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Desirelist\updateBudget.php:41) in C:\xampp\htdocs\Desirelist\updateBudget.php on line 46

This is the code:

Code: Select all

<?php
include_once 'connect.php';
session_start();

include 'header.php';

if (isset($_SESSION['user']))
{
  $user=$_SESSION['user'];
}
else
{
   echo "<br><br> There is no logged in account. Please <A href='login.php'>Login</a><br>";
   exit;
}


if ($_POST['amount'])
{
$amount=$_POST['amount'];
}
if ($_POST['operation'])
{
$operation=$_POST['operation'];
}

$userinfo="SELECT * from account where user='$user'";
$userinfo2=mysql_query($userinfo) or die("could not get account stats!");
$userinfo3=mysql_fetch_array($userinfo2);
$money=$userinfo3['money'];

if($operation == "+")
{
$money=$money+$amount;
}
else
{
$money=$money-$amount;
}

echo "Now you have ",$money,"€<br>";

mysql_query("UPDATE account SET money=$money
WHERE user='$user'");

$page=updateBudget.php;


?>

<p align="center">
<table border="3" width="21%">
<tr>
  <td>
  
<?php
$userinfo="SELECT * from account where user='$user'";
$userinfo2=mysql_query($userinfo) or die("could not get account stats!");
$userinfo3=mysql_fetch_array($userinfo2);
$money=$userinfo3['money'];

echo "You have ",$money,"€<br><br>";
?>

<form method ="post" action="updateBudget.php">
Type the amount to be added or subtracted here: <input type="text" name="amount"><br>
<select name="operation">
  <option value="+">Add Money</option>
  <option value="-">Subtract Money</option>
</select>
<br>
<p align="center"><input type="submit" value="Submit" name="submit"></td></p>
</form>
</tr>
</table></p>

Last edited by Eddy Parera on Tue Mar 19, 2013 9:13 pm, edited 1 time in total.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP: Refresh page or variables

Post by hallsofvallhalla »

any change or call to headers MUST be on the very first line of the page other than <?php. No empty lines or anything.
Eddy Parera
Posts: 18
Joined: Mon Feb 06, 2012 7:54 pm

Re: PHP: Refresh page or variables

Post by Eddy Parera »

Now, it goes to google chrome error Too Many Redirects...

I want something that I click on the submit button, it comes back to the same page, by running the script and at the end, the page refresh itself to clean the html form informations coming from the submit action...
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: PHP: Refresh page or variables

Post by hallsofvallhalla »

Hmm not sure what you mean but just set a post variable in the form

detect the form submit on submit..so when page loads

if(isset($_POST['submit']))
{
do code after submit.....


}
else
{
echo "<form method='post'>

....

...
<input type='submit'>
}
Eddy Parera
Posts: 18
Joined: Mon Feb 06, 2012 7:54 pm

Re: PHP: Refresh page or variables

Post by Eddy Parera »

Thanks... Problem solved...
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP: Refresh page or variables

Post by Jackolantern »

Awesome :) If you could add [resolved] to the title of the post :cool:
The indelible lord of tl;dr
Post Reply

Return to “Beginner Help and Support”