Page 1 of 1

PHP: Refresh page or variables [Solved]

Posted: Tue Jan 22, 2013 12:15 am
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>


Re: PHP: Refresh page or variables

Posted: Tue Jan 22, 2013 1:47 am
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.

Re: PHP: Refresh page or variables

Posted: Tue Jan 22, 2013 11:05 pm
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...

Re: PHP: Refresh page or variables

Posted: Wed Jan 23, 2013 2:46 pm
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'>
}

Re: PHP: Refresh page or variables

Posted: Sat Jan 26, 2013 5:26 pm
by Eddy Parera
Thanks... Problem solved...

Re: PHP: Refresh page or variables

Posted: Sat Jan 26, 2013 7:16 pm
by Jackolantern
Awesome :) If you could add [resolved] to the title of the post :cool: