nicoschuck nicoschuck - 2 months ago 13
PHP Question

PHP refresh site after post

I make some changes on my db with php. The result i want to see after sending the formular. The side refreshes but the changes are not there. If i put f5 everything ist fine. Is there a solution to fix it?

<form action="" method="Post">

........

if(isset($_POST["saveMetrics"]))
{
$_DB = new DBData();
$_DB -> connect();
$str = "";
for ($i = 3; $i < count($_metricsFb); $i++)
{
if(isset($_POST[$_metricsFb[$i]['COLUMN_NAME']]))
$str=$str.$_metricsFb[$i]['COLUMN_NAME']."=1";
else
$str=$str.$_metricsFb[$i]['COLUMN_NAME']."=0";
if($i!=count($_metricsFb)-1)
{
$str=$str.",";
}
}
$queryCALL ="UPDATE t_user_metrics SET ".$str." WHERE id_user=";

$_DB -> setMetrics( $_GET["client"],$queryCALL) ;
header("Location: dailyReport.php?client=".$_GET["client"]." success=true");
$_DB ->disconnect();
}
<div><input type="submit" class="button" name="saveMetrics" value="Save"></div>
</form>


it doesnt works if i five the path to action.

header('location:'.$_SERVER['PHP_SELF']);


doesnt works too!
I thougt it could be possible to open another window which refresh his parent. But ther must be an easier way. Anyone an idea?

Answer

Probably, You are displaying data before execution of update script. Try to move your code before it.

if(isset($_POST["saveMetrics"]))
{
    $_DB = new DBData();
    $_DB -> connect();
    $str = "";
    for ($i = 3; $i < count($_metricsFb); $i++)
    {
        if(isset($_POST[$_metricsFb[$i]['COLUMN_NAME']]))
            $str=$str.$_metricsFb[$i]['COLUMN_NAME']."=1";
        else
            $str=$str.$_metricsFb[$i]['COLUMN_NAME']."=0";
        if($i!=count($_metricsFb)-1)
        {
            $str=$str.",";
        }
    }
    $queryCALL ="UPDATE t_user_metrics SET ".$str." WHERE id_user=";

    $_DB -> setMetrics( $_GET["client"],$queryCALL) ;
    header("Location: dailyReport.php?client=".$_GET["client"]." success=true");
    $_DB ->disconnect();          
}

... your <form> and other stuff