0

I am trying to make a page redirect after it sends form data to my MySQL server.

The following try and catch loop executes an array I have set and sends it to the MySQL database.

if(!empty($_POST)) {
    try { 
        $stmtCE = $dbMain->prepare($queryCE); 
        $resultCE = $stmtCE->execute($query_paramsCE);
        echo "<meta http-equiv='refresh' content='0;url=mypage.php'>";
    } 
    catch(PDOException $exCE) { 
        die("Failed to run query: " . $exCE->getMessage()); 
    }
}

I chose to use the meta redirect, because I have had issues in the past with header() due to echoing out some other code to the page.

Using this code, my form submits to the MySQL database correctly, but fails to redirect after clicking the submit button. Are there any possible solutions to this problem?

Nihir
  • 135
  • 1
  • 1
  • 11
  • 1
    Try this echo ' I think the problem is in meta tag not beeing in . -Just thinking out loud – Luka Jul 04 '14 at 01:16
  • I tried both of these methods, but neither seem to work. Should I try putting a PHP script exclusively in the body/head of the document? – Nihir Jul 04 '14 at 01:17
  • 3
    Try adding exit(); after echo, this should disable all code after echoing redirect. And check your DOM – Luka Jul 04 '14 at 01:20
  • I tried the exit() with several other variations of the redirects, and still have the submit button blink, the MySQL database update, but no redirect. Could you clarify by what you mean with checking my DOM? – Nihir Jul 04 '14 at 01:24
  • 1
    Re-arrange your code so this occurs prior to any HTML output then simply use `header('Location: mypage.php'); exit;` – Phil Jul 04 '14 at 01:28
  • Phil, won't that redirect my page immediately? The page has a form on it, and it submits to itself. I am just wanting to redirect after the PHP submits this form data from POST to my MySQL server. – Nihir Jul 04 '14 at 01:30
  • Err, yes? Isn't that what you want it to do? You'd just be replacing your `echo " – Phil Jul 04 '14 at 01:31
  • fully agree with @Phil . You can also use [ob_start()](http://php.net/manual/en/function.ob-start.php) etc ... hf :) – Bobot Jul 04 '14 at 01:31
  • @Phil the only issue is that my page has the PHP script on it, so the redirect would stop the HTML form from showing. Also, all of this PHP is before the tag as you instructed. – Nihir Jul 04 '14 at 01:35
  • 1
    My testing with meta refresh in a try catch block is working just fine, whether in the or the . So I think the problem lies elsewhere, and that would indicate this is not a duplicate question having to do with headers already sent. – bloodyKnuckles Jul 04 '14 at 01:50

0 Answers0