AdRock Posted August 3, 2011 Share Posted August 3, 2011 Here is my situation 1. User fills out form and clicks the submit button 2. On next page, get the POST variables 3. If POST variables empty, redirect back to original page and show form 4. If POST variables complete, do other code I did have a theory that before i send to the other page, i send it to iteself to do some form validation and if that passes, then go to the other page. What i don't want to do is have the user having to type in onfo all over again. What would be best? Could i have the form validation done of the other page and if it fails send it back and if so, how i would fill in the form with validated data so the user doesn't have to type it again <?php if(issset($_POST['submit']) { //validate the post variables to make sure nothing is left out //if all passed okay go to other page header('Location: mypage.php'); } ?> //my form code goes here<form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/ Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 you can set the user info to sessions and set the input values to the sessions...so if the user has not filled out any info..the value will be empty...however if they have filled out info, the input value will be set to that info...that way if they are redirected back the the form, their info will remain intact..however make sure that you unset the sessions upon form completion Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251177 Share on other sites More sharing options...
silkfire Posted August 3, 2011 Share Posted August 3, 2011 For every input type="text", in the value="" field you write inline PHP code, checking if there was a $_POST. If there was for that item, you echo out its value otherwise leave it blank: <input type="text" name="username" value="<?=(isset($_POST['username']) ? $_POST['username'] : '')?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251182 Share on other sites More sharing options...
silkfire Posted August 3, 2011 Share Posted August 3, 2011 I've done this in a project before. I had a very neat solution: you create an error variable and set it to 0. For each field that you validate and it fails validation, you increment $error_count by 1. Then after validation BUT BEFORE any output, if $error_count > 0, then show same page (still checking for posted variables), otherwise redirect the user to the completion page. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251183 Share on other sites More sharing options...
the182guy Posted August 3, 2011 Share Posted August 3, 2011 The only real solution is sessions are AyKay47 suggests. @silkfire, I don't see how your suggests can solve this problem because once the user is redirected back to the form there will be no $_POST data, redirects don't transport the previous post data. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251191 Share on other sites More sharing options...
PFMaBiSmAd Posted August 3, 2011 Share Posted August 3, 2011 The simplest solution is to put the form and the form processing code on one page. Any existing post data is available to put back into the fields and you can validate and display any errors next to the field they apply to. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251197 Share on other sites More sharing options...
silkfire Posted August 3, 2011 Share Posted August 3, 2011 The only real solution is sessions are AyKay47 suggests. @silkfire, I don't see how your suggests can solve this problem because once the user is redirected back to the form there will be no $_POST data, redirects don't transport the previous post data. I never said redirect to same page I said show the same page thus retaining the POST variables. You won't leave the page until all fields are correctly entered. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251236 Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 you can use $_POST values...however that method is not full-proof and will rely on the user not refreshing the page by accident or something along those lines.. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251239 Share on other sites More sharing options...
silkfire Posted August 3, 2011 Share Posted August 3, 2011 you can use $_POST values...however that method is not full-proof and will rely on the user not refreshing the page by accident or something along those lines.. If you refresh a page with post variables most modern browsers will ask if you want to repost the page. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251243 Share on other sites More sharing options...
AyKay47 Posted August 3, 2011 Share Posted August 3, 2011 you can use $_POST values...however that method is not full-proof and will rely on the user not refreshing the page by accident or something along those lines.. If you refresh a page with post variables most modern browsers will ask if you want to repost the page. and the average user does not know what that means and will cancel Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251245 Share on other sites More sharing options...
AdRock Posted August 3, 2011 Author Share Posted August 3, 2011 What PFMaBiSmAd said, has given me food for thought. I did think about having the validation on the other page and redirect back if it fails, but i could check for empty fields etc within the new page and display the form if needed. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251348 Share on other sites More sharing options...
PFMaBiSmAd Posted August 3, 2011 Share Posted August 3, 2011 Here's an additional suggestion for any form submission - After you have successfully processed a form submission, you actually need to redirect to the same page as the form's action="" attribute to change the browser's history for that page from a form submission to a GET request so that the browser won't attempt to resubmit the data should you browse back to that page. If you have single page or separate pages for the form and the form processing code, anytime you browse back to the page that matches the action="" attribute of the form, the browser will attempt to perform the last operation it has recored in the browser's history for that page. Quote Link to comment https://forums.phpfreaks.com/topic/243690-passing-post-variables-between-pages/#findComment-1251352 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.