Question

ok I've searched stackoverflow and many other sites, I've tried all kinds of solutions for this but nothing seems to work.

I am processing a form in PHP, checking for missed entries and erroring if missed or adding to the SQL DB if ok, the form itself works just fine, the processing works, the form is either error thrown and displayed or added to the Database, I want that page to then display either error or sucess wait a short time and then auto forward either back to the form if there was an error, or to the page that displays the db contents if the add was successful. Nothing I seem to try here works. please help: My code so far:

//If errors present
    if ($errormsg) {
        echo "<div class=\"box red\">$errormsg</div>";
        sleep(2);
        echo '<script>' . "\n"; 
        echo 'window.location="blogmake.html?blogid=" + blogid;'; 
        echo '</script>'; 
    }
    if ($secim == "3"){     //If all present and correct post comment to DB
        if ($valname && $valemail && $valcom){
            $con = mysql_connect("xxxx","User","pass");

            if (!$con)
                die('Could not connect: ' . mysql_error());

            mysql_select_db("dbname", $con);

            $fulcom = mysql_real_escape_string ($_POST['comment']);

            mysql_query("INSERT INTO tabname(blogid, date, email, name, comment)    VALUES ('$blogid', CURRENT_TIMESTAMP(),'$valemail','$valname','$fulcom')") or die('Error: ' . mysql_error());

            mysql_close($con);  
            echo "<div class=\"box green\">Your comment has been submitted</div>"; 
            sleep(2);
            echo '<script>' . "\n"; 
            echo 'window.location="blogread.php?blogid=" + blogid;'; 
            echo '</script>';
        }
    }
?>
</div>
Was it helpful?

Solution 2

You need:

<meta http-equiv="refresh" content="3;url=http://www.google.com/" />

Change 3 for your seconds and url= by your webpage, ie:

echo '<meta http-equiv="refresh" content="2;url=blogread.php?blogid='.blogid.'" />';

OTHER TIPS

You should probably use either an HTML meta tag:

<meta http-equiv="refresh" content="2;URL='http://yoursite.com/blogread.php?blogid=<?= $blogid ?>'" /> 

or JavaScript's setTimeout function:

setTimeout(function(){
    window.location="blogread.php?blogid=<?= $blogid ?>";
}, 2000);

The meta tag has two important parameters: 2 is the number of seconds after which a redirection happens; and URL=... is the url to which it should redirect.

SetTimeout has two parameters in this case, the first one is the function that will be executed (the whole function part); and the other one is the delay time in milliseconds after which that function will be executed (the number 2000).

Note that I used <?= $blogid ?> in both cases - that's just a short code for this: <?php echo $blogid; ?>. Of course, you can use it however you want, for example using echo to echo the whole code, just like you were doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top