Jump to content

Archived

This topic is now archived and is closed to further replies.

archbeta

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

Recommended Posts

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]
                                                         

Share this post


Link to post
Share on other sites
SESSIONS.  Look them up in the manual.  See my sig for a link to the manual.

Share this post


Link to post
Share on other sites
This can be done without sessions if the first form is posting to the second form.

[code]
// 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>';
[/code]

[code]
// 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>';
[/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 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.
}
[/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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.