0

I want to echo something in a post after submitting a form, then redirecting the header to cancel the double submit problem. Currently, Output_buffering turned on to allow the redirect to work. here some example code that illustrates the problem. Just make sure Output_buffering is on in php.ini.

<?php
   if(isset($_POST['submit'])){
   echo "hi";
}

   if (count($_POST) {
    header("Location: ".$_SERVER['REQUEST_URI']);
    exit();
    }
?>

<form action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST" name="edit" >
    <button type="submit" value="submit" name="submit">edit</button>
</form>
  • possible duplicate of [Redirect in PHP without use of header method](http://stackoverflow.com/questions/27123470/redirect-in-php-without-use-of-header-method) – Im0rtality Aug 13 '15 at 12:38
  • The solution is not very applicable. – Sameh Khasawneh Aug 13 '15 at 12:48
  • You can't output after headers being sent. That means you have to perform redirect from HTML/Javascript. It looks like Rocky copy-pasted HTML way from same answer and here's JS way. Why do you think these are not applicable? – Im0rtality Aug 13 '15 at 12:55
  • Sorry for next explaining earlier. We're redirecting the header to prevent the user from doing a double submit when he refreshes. We want the echo to show, so redirecting it back at it's self is going to remove the echo. – Sameh Khasawneh Aug 13 '15 at 13:07

2 Answers2

0

You should reverse the order.

On submit:

  1. Process yours submit form.
  2. Push desired output message into yours session.
  3. Redirect to whatever/message page.

On message page show:

  1. Retrive message, pushed to session.
  2. Cleanup session record.
  3. Display message.

It's can be separate page, same page, as with submit for, or any other page.

Or... Do the work with JavaScript and AJAX:

<?php
    if(isset($_POST['submit'])) {
        ...
        if (/all is ok/)
            die(json_encode(array('status' => 'ok', 'message' => 'Hi!')));
        else {
            die(json_encode(array('status' => 'err', 'message' => 'I\'m failed!')));
        }
    }
?>

<form action="<?php echo htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST" name="edit" >
    <button type="submit" value="submit" name="submit">edit</button>
</form>
<script language="javascript">
  $('form').submit(function() {
      var parameters = ...; // collect parameters from form
      $.getJSON('/url-to-script', parameters)
       .success(function(response) {
           if (response.message == "ok")
               alert(response.message);
           else
               alert('Can\'t process input:\n' + response.message);
       })
       .error(function(response) {
           alert('What a terrible failure!');
       });
  });
</script>

When .success() fired - you can show supplied message to user via alert/custom modal message box an then make redirect (document.location = '<?php echo ... ?>') or replace form on page with some custom message and link/button to proceed... Lots of variants.

ankhzet
  • 2,517
  • 1
  • 24
  • 31
0

Headers must be sent BEFORE any content (that's what echo gives).

You could store your message into session and print it on next request (which would be your follow up redirect).

EXAMPLE:

sessions_init.php

<?php 
session_start();

post.php

<?php
include_once 'sessions_init.php';
// assuming POST succeeded, data is valid, etc
$_SESSION['messages'][] = 'You have been redirected';

header("Location: ".$_SERVER['REQUEST_URI']);
exit();

html template:

include_once 'sessions_init.php';
// print HTML:
// <html><head>...</head><body>...

foreach($_SESSION['messages'] as $message) {
   print($message);
}

// some other content </body></html>
Im0rtality
  • 3,463
  • 3
  • 31
  • 41