[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Php form that remembers

Posted on 2005-04-25
16
Medium Priority
?
337 Views
Last Modified: 2010-04-06
Need my php form to remember the user input fields, so when the user comes back to the form they don't have to fill in the personal info again. Im thinking about using cookies, but have never worked with them before.

Any Ideas would be Great.
0
Comment
Question by:rkteach
  • 7
  • 5
  • 3
15 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 13858486
In php you could use sessions. Sessions are like cookies, except for the fact that they are stored on the server, and are identified by a SESSION_ID which is either stored in a cookie or passed by url index.php?PHPSESSID=somenumber

Read more on sessions at: http://php.net/session

In short; to enable sessions you should start your script with:
<?php
  session_start();
  ..
  all other code
?>

This enables you to access the $_SESSION array, which is a superglobal like $_POST and $_GET.

Anything you store in $_SESSION (except objects, which take some additional coding) can be retrieved like it were using cookies.

Try this code, and run it multiple times
<?php
 session_start();
 if(isset($_SESSION['test'])) echo '<p>'.$_SESSION['test'].'</p>';
 $_SESSION['test'] = 'test '.date('Ymd H:i:s');
?>

Similar you can store your form variables in it. You have to think out an appropriate naming convention such that you can distinct your session data throughout the pages.

-r-
0
 

Author Comment

by:rkteach
ID: 13858874
Hi,
Will sessions work after they leave the site and return, lets say a week later?
I thought sessions were broken after leaving the site.

I'll try the session_start... but where would i put my values like $name, $zip, $city ?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13858931
I think the user wants the form fields to be filled up if the user comes back to his site at anytime! Since sessions are expired when the browser window is closed, only cookies can be used.

Here's an example:

<?php

 $firstname = $lastname = '';

 // Check if the cookie exists.
 if(isset($_COOKIE['FORM_COOKIE']) && !empty($_COOKIE['FORM_COOKIE']))
 {
       // Split the cookie and extract the information.
       $cookie_data_arr = explode('~', $_COOKIE['FORM_COOKIE']);
       $firstname = $cookie_data_arr[0];
       $lastname  = $cookie_data_arr[1];
 }
 
 // If the form is submitted.
 if(isset($_POST['form_submit']) && !empty($_POST['form_submit']))
 {
       // Prepare the data to be cookied.
       $cookie_data = $_POST['firstname'] . '~' . $_POST['lastname'];
       
       // Set the prepared data in cookie.
       setcookie("FORM_COOKIE", $cookie_data, time()+60*60*24*30*6);

      // Redirect or whatever, your stuff goes here.
 }

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="firstname" value="<?php echo htmlentities($firstname); ?>"/>
<input type="text" name="lastname" value="<?php echo htmlentities($lastname); ?>"/>
<input type="submit" name="form_submit" value="Submit"/>  
</form>
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 49

Expert Comment

by:Roonaan
ID: 13858969
You can set sessions to exist longer, but depends on the amount of data that needs to be stored, which would be more apprioriate.
0
 

Author Comment

by:rkteach
ID: 13859003
I would like to try cookies. im adding it now.
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13859012
Sessions are stored in the server! Extending the session time is useful only if the user keeps the browser window opened for long time without accessing. But the asker request is: "Will sessions work after they leave the site and return, lets say a week later?"

I think you misunderstood the question Roonaan. :=)
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 13859102
ldbkutty.

Sessions can be set to be stored days, weeks, even months. I developed an intranet environment which did timeout after 30 days.
It is permanently not correct to state that a session expire when you close your browser. It is the default setting that causes that behaviour. I think you haven't read into sessions enough yet.

-r-
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13859197
Ok, for this question and especially for this requirement: "Will sessions work after they leave the site and return, lets say a week later?" - I argue that using sessions are not feasible. If you have objection, Would you mind posting me a question in PHP TA regarding this ?
0
 

Author Comment

by:rkteach
ID: 13859264
Idbkutty
Hi, im using the code at the top of my page.
but im getting an Pharse error on the second line.
and should FORM_COOKIE be replaced with the name of my form? like form1 ect...

<?php
 $name = $company = '';
 // Check if the cookie exists.
 if(isset($_COOKIE['FORM_COOKIE']) && !empty($_COOKIE['FORM_COOKIE']))
 {
      // Split the cookie and extract the information.
      $cookie_data_arr = explode('~', $_COOKIE['FORM_COOKIE']);
      $name = $cookie_data_arr[0];
      $company  = $cookie_data_arr[1];
 }
 
 // If the form is submitted.
 if(isset($_POST['form_submit']) && !empty($_POST['form_submit']))
 {
      // Prepare the data to be cookied.
      $cookie_data = $_POST['name'] . '~' . $_POST['company'];
      
      // Set the prepared data in cookie.
      setcookie("FORM_COOKIE", $cookie_data, time()+60*60*24*30*6);
      // Redirect or whatever, your stuff goes here.
 }

?>

<?php
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13859344
Hi rkteach,

>>  should FORM_COOKIE be replaced with the name of my form? like form1 ect...
Nope! "FORM_COOKIE" is the name of the cookie. you can replace it with any string. Examples in the manual are easier to understand: http://www.php.net/setcookie

>> but im getting an Pharse error on the second line
I dont see a reason for parse error, but can you post your full existing code so I can have a look.

note that $_POST["form_submit"] in my code refers to the submit button named as "form_submit".
0
 

Author Comment

by:rkteach
ID: 13859401
<?php require_once('../Connections/award.php'); ?>

<?php
 $name = $company = '';
 // Check if the cookie exists.
 if(isset($_COOKIE['FORM_COOKIE']) && !empty($_COOKIE['FORM_COOKIE']))
 {
      // Split the cookie and extract the information.
      $cookie_data_arr = explode('~', $_COOKIE['FORM_COOKIE']);
      $name = $cookie_data_arr[0];
      $company  = $cookie_data_arr[1];
 }
 
 // If the form is submitted.
 if(isset($_POST['form_submit']) && !empty($_POST['form_submit']))
 {
      // Prepare the data to be cookied.
      $cookie_data = $_POST['name'] . '~' . $_POST['company'];
     
      // Set the prepared data in cookie.
      setcookie("FORM_COOKIE", $cookie_data, time()+60*60*24*30*6);
      // Redirect or whatever, your stuff goes here.
 }

?>

<?php

....
  header(sprintf("Location: %s", $insertGoTo));
....

?>
<form action="<?php echo $editFormAction;?>" method="post" name="form2" onSubmit=".....">
Name: <input type="text" name="name" value="" size="32">
Company: <input type="text" name="company" value="" size="32">
<!-- other fields -->
<input name="submit" type="submit" value="Send Quote Request">
</form>

>> Code reduced to be more specific: ldbkutty ( EE Page Editor ) <<
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 2000 total points
ID: 13859651
Change the "name" textbox to this:
<input type="text" name="name" value="<?php echo htmlentities($name); ?>" size="32">

Change the "company" textbox to this:
<input type="text" name="company" value="<?php echo htmlentities($company); ?>" size="32">

Change the submit button to this:
<input name="quote_request_submit" type="submit" value="Send Quote Request">

Make the PHP code like this:

<?php
 $name = $company = '';

 // If the form is submitted.
 if(isset($_POST['quote_request_submit']) && !empty($_POST['quote_request_submit']))
 {
      // Prepare the data to be cookied.
      $cookie_data = $_POST['name'] . '~' . $_POST['company'];
     
      // Set the prepared data in cookie.
      setcookie("FORM_COOKIE", $cookie_data, time()+60*60*24*30*6);
      // Redirect or whatever, your stuff goes here.
 }
 else
 {
       // Check if the cookie exists.
       if(isset($_COOKIE['FORM_COOKIE']) && !empty($_COOKIE['FORM_COOKIE']))
       {
            // Split the cookie and extract the information.
            $cookie_data_arr = explode('~', $_COOKIE['FORM_COOKIE']);
            $name = $cookie_data_arr[0];
            $company  = $cookie_data_arr[1];
       }
             
 }
?>

Since you are using "header" function to redirect the page, there should not be any echo or html tags before the header function. There should not be even a single whitespace before the PHP starting tag <?php.

I mean:

<?php // => Correct.

  <?php // => Wrong, see the whitespace before <?php

I can't seem to find the reason for "parse error". It is mostly because you have missed a semi-colon or something like that. Try the above and post if you got any errors.
0
 

Author Comment

by:rkteach
ID: 13860411
Hi Idbkutty,
I made the changes you sugested but
Im still getting the pharse error on line 3

<?php require_once('../Connections/award.php'); ?>
<?php
 $name = $company = '';
0
 

Author Comment

by:rkteach
ID: 13860518
Ok! now I understand about the spaces before the header.
Im past the error now but it's not putting in the data base
error "name cannot be null"

im double checking the input field naming now
0
 

Author Comment

by:rkteach
ID: 13860743
Works Great!
Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question