archbeta Posted September 5, 2006 Share Posted September 5, 2006 Hello everyone ;D, This is my first post, my question is: [color=green][font=Verdana][b]Is there a way to pass form values from a previous form to the new form?[/b][/font][/color] For example, i have "firstpage.php" and I have in that form a textbox with a value "retain me", if i wanted to have that value to "secondpage.php" - how would you implement it without using a cookie? Is there another way arround it? ??? ??? ??? many thanx, [color=navy][b]-zbatev[/b][/color] Quote Link to comment https://forums.phpfreaks.com/topic/19781-how-to-retain-form-values-from-previous-form-with-no-cookies/ Share on other sites More sharing options...
ober Posted September 5, 2006 Share Posted September 5, 2006 SESSIONS. Look them up in the manual. See my sig for a link to the manual. Quote Link to comment https://forums.phpfreaks.com/topic/19781-how-to-retain-form-values-from-previous-form-with-no-cookies/#findComment-86476 Share on other sites More sharing options...
roopurt18 Posted September 5, 2006 Share Posted September 5, 2006 This can be done without sessions if the first form is posting to the second form.[code]// Form1.phpecho '<form name="form1" method="post" action="Form2.php">' . '<input type="text" name="frm1_txt1" value=""/>' . '<input type="submit name="submit" value="Submit"/>' . '</form>';[/code][code]// Form2.php// Check for values from form 1if(count($_POST)){ $Hiddens = Array(); if(isset($_POST['frm1_txt1'])){ $Hiddens['frm1_txt1'] = $_POST['frm1_txt1']; }}// Create the hiddens HTMLif(is_array($Hiddens) && count($Hiddens)){ $HiddensHTML = ''; foreach($Hiddens as $key => $val){ $HiddensHTML .= "<input type=\"hidden\" name=\"{$key}\" value=\"{$val}\"/> }}// Dump form2echo '<form name="form2" method="post" action="post.php">' . $HiddensHTML . '<input type="text" name="frm2_txt1" value=""/>' . '<input type="submit name="submit" value="Submit"/>' . '</form>';[/code]But why create a new .php file for each page of the form? They're all the same form after all, so...[code]// Form.php// Call this function to display the formShowForm(){ if(ValidateForm()){ // Form is valid $Html = ProcessForm(); }else{ // Either form had errors or was never submitted $Html = ShowSpecificForm(); } return $Html;}function ValidateForm(){ if(!count($_POST)){ return false; } $Page = // Get the page of the form to validate, either from $_GET or possibly from a $_POST value switch($Page){ case PAGE_3: $Valid= ValidatePage3(); break; case PAGE_2: $Valid = ValidatePage2(); break; case PAGE_1: $Valid = ValidatePage1(); break; } return $Valid;}function ShowSpecificForm(){ $Page = // Get the page of the form to show, either from $_GET or possibly from a $_POST switch($Page){ case PAGE_3: $Html = ShowPage3(); break; case PAGE_2: $Html = ShowPage2(); break; case PAGE_1: default: $Html = ShowPage1(); break; } return $Html;}function ProcessForm(){ // Follow the same procedure as in validate and showspecific for determining which page // is being processed.}[/code]That puts all the logic for the entire form, even with multiple pages, into a single .php file. If you're good about using functions it will save you a lot of code rewriting and wild goose chases between files. Quote Link to comment https://forums.phpfreaks.com/topic/19781-how-to-retain-form-values-from-previous-form-with-no-cookies/#findComment-86618 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.