Jump to content


Photo

How to retain form values from previous form-with no cookies???


  • Please log in to reply
2 replies to this topic

#1 archbeta

archbeta
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 05 September 2006 - 03:38 PM

Hello everyone ;D,

    This is my first post, my question is: Is there a way to pass form values from a previous form to the new form?

    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,
                                                                                                                              -zbatev
                                                         

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 05 September 2006 - 03:41 PM

SESSIONS.  Look them up in the manual.  See my sig for a link to the manual.

Info: PHP Manual


#3 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 05 September 2006 - 07:39 PM

This can be done without sessions if the first form is posting to the second form.

// Form1.php
echo '<form name="form1" method="post" action="Form2.php">'
     . '<input type="text" name="frm1_txt1" value=""/>'
     . '<input type="submit name="submit" value="Submit"/>'
     . '</form>';

// Form2.php

// Check for values from form 1
if(count($_POST)){
  $Hiddens = Array();
  if(isset($_POST['frm1_txt1'])){
    $Hiddens['frm1_txt1'] = $_POST['frm1_txt1'];
  }
}
// Create the hiddens HTML
if(is_array($Hiddens) && count($Hiddens)){
  $HiddensHTML = '';
  foreach($Hiddens as $key => $val){
    $HiddensHTML .= "<input type=\"hidden\" name=\"{$key}\" value=\"{$val}\"/>
  }
}

// Dump form2
echo '<form name="form2" method="post" action="post.php">'
     . $HiddensHTML
     . '<input type="text" name="frm2_txt1" value=""/>'
     . '<input type="submit name="submit" value="Submit"/>'
     . '</form>';

But why create a new .php file for each page of the form?  They're all the same form after all, so...
// Form.php

// Call this function to display the form
ShowForm(){
  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.
}

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.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users