PHP passing messages between pages

I have a form on the page where the user has tabs for editing the XML file, the action for the form is to send it to a separate PHP script, where editing occurs after they get into submit. the script will be either successful or unsuccessful, in any case, I redirect it back to the form page through the header. Is there an easy way to submit a confirmation or rejection message to a form page? I can do this in the url, but I prefer to keep this clean look.

+4
source share
2 answers

The way I did it (and I personally use it) is just sessions.

// process something if($success) { flash_message('success','Did whatever successfully.'); } else { flash_message('error','Oops, something went wrong.'); } header('Location: whatever.php'); 

Then, somewhere else, in your library or function file or something else:

 function flash_message($type, $message) { // start session if not started $_SESSION['message'] = array('type' => $type, 'message' => $message); } 

Then on the browse page / page you can:

 if(isset($_SESSION['message'])) { printf("<div class='message %s'>%s</div>", $_SESSION['message']['type'], $_SESSION['message']['message']); unset($_SESSION['message']); } 

It's pretty simple, but you can expand it from there if you want some messages and so on. The bottom line is that I believe that sessions are best suited for this.

+16
source

I prefer to put the form processing code and the form code on the same page. If you really want to separate it, you can move the code to another file and include this file from the form file, but from the client side it will look like the same PHP file.

Then always submit the form and check your mistake at the top before displaying the form. If there is a message to display, you simply visualize the form and display the message along with user data so that they can fix this problem. They can be sent again, and you can check for errors again. If they were sent successfully, you can redirect them to another place or just show the message "Success".

+2
source

All Articles