dudleylearning Posted December 29, 2016 Share Posted December 29, 2016 Hi All, couldn't seem to figure how to pass data from page 2 to page 1. On page 2 I have this code block: # if the form has been posted if ($_SERVER["REQUEST_METHOD"] == "POST") { # check for empty fields if (!empty(trim(($_POST['name'] == '')))) { $nameErr = "<span class=\"error\">Name is required</span>"; } if (!empty(trim(($_POST['email'] == '')))) { $emailErr = "<span class=\"error\">Email is required</span>"; } # if all is fine, then add data to the database else { $sql = 'INSERT INTO author SET name = :name, email = :email'; $query = $dbConnection -> prepare($sql); $query -> bindValue(':name', $_POST["name"]); $query -> bindValue(':email', $_POST["email"]); $query -> execute(); $message = 'done'; header('Location: index.php'); } } The variable $message I wanted to pass to page 1, where I have this in: <?php if(isset($_POST['message'])) { echo $_POST['message']; } ?> I can't seem to figure out the problem? Any tips? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/302829-passing-a-variable-from-page2-to-page1/ Share on other sites More sharing options...
NotionCommotion Posted December 29, 2016 Share Posted December 29, 2016 While you "could" post from one page to another, don't and use sessions. <?php // Page 1 session_start(); $_SESSION['message']=$message; <?php // Page 2 session_start(); echo($_SESSION['message']); Quote Link to comment https://forums.phpfreaks.com/topic/302829-passing-a-variable-from-page2-to-page1/#findComment-1540852 Share on other sites More sharing options...
dudleylearning Posted December 29, 2016 Author Share Posted December 29, 2016 I was looking at using $_POST if possible, but couldn't figure it out from my trial. Quote Link to comment https://forums.phpfreaks.com/topic/302829-passing-a-variable-from-page2-to-page1/#findComment-1540854 Share on other sites More sharing options...
Jacques1 Posted December 29, 2016 Share Posted December 29, 2016 (edited) You can't, at least not in your current workflow. You'd have to create an extra form which the user then has to submit only to help you pass internal data around – which obviously makes no sense. This also violates the purpose of POST request (which are meant to modify data). Use a cookie, session variable or URL parameter. Anything but a POST request. Edited December 29, 2016 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/302829-passing-a-variable-from-page2-to-page1/#findComment-1540856 Share on other sites More sharing options...
dudleylearning Posted December 29, 2016 Author Share Posted December 29, 2016 thanks for the tips. I did it through a URL parameter Quote Link to comment https://forums.phpfreaks.com/topic/302829-passing-a-variable-from-page2-to-page1/#findComment-1540860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.