Click to See Complete Forum and Search --> : Saving Form Data Between Pages
dmichael
11-07-2004, 06:45 PM
Please excuse the novice nature of this question, but when it's time to save the user-entered data from a form before executing the Javascript reload() function, how exactly do you preserve that data so it can be assigned to $_SESSION variables at the top of the page in Php after (or as) the reload() executes? Does it involve the use of cookies? If so, can someone point me in the right direction as far as cookie-related functions/commands to work with?
Not sure if I understand you completely but why wouldn't you just put the form variables into sessions and then reload the page? Sessions stay around until you clear them or until the browser is closed. You could refresh the page 1000 times and the sessions would still be there. But like I said, I'm not sure if I fully understand your question. Maybe try explaining it in a little more detail if I haven't already answered your question. :)
dmichael
11-08-2004, 06:00 PM
By "put the form variables into sessions," you mean assign them to $_SESSION['whatever'], $_SESSION['whatever2'], etc., right? If so, this is Php, correct? And if it's Php, it's only evaluated once by the server before the browser gets it. The user then fills in the fields, and if I can't call some homemade Php function now to make the above assignments (saving their data) because the browser won't process Php, I'm forced to reload the page to get to the Php code which DOES make the assignments. BUT, when the page reloads, how have I saved the user-entered data without the benefit of the $_SESSION Php assignments? It gets hammered in the html fields by the reload, right? Obviously, I'm missing some key ingredient here...
Paul Jr
11-08-2004, 09:17 PM
I'm not sure I understand, either.
When you submit the form, you can access the inputted data through the querystring (GET method) or through HTTP POST (POST method). You can then save that data in sessions and use it in subsequent pages and all that.
dmichael
11-08-2004, 11:05 PM
Well, to get back to my original scenario...After I collect the user-entered data and the form is submitted, I want to email the data to myself AND pass it on to the next page. Paul Jr, you suggested mailing the data on the processing page, and by that I assume you mean use the "action=" for some mail script. Then you said "sessions are created containing the inputted information, you redirect to the next page, and the information is then retrieved via said sessions on that page..." Given my scenario, what do you mean by "sessions are created?" Someone told me they don't use Submit at all (leaving "action=" blank), and instead rely on sessions/cookies. If I process the form without using Submit, then I can't retrieve the data from $_POST, right? But if I did use Submit, then I can't use the mail script in "action=" since the subsequent page would go here (as in "action=page2.php") and I'd use $_POST on page2.php to retrieve the data. So, I will paste the code for a simple form with 1 textbox and a button to reload the page (I don't know offhand how I'd get them to the next page, but that's the least of my worries now). When you see the code, perhaps you can tell me how data in Textbox1 will be both emailed and preserved for the next page.
<?php
//page1.php
session_start( );
echo 'Welcome to page #1';
//What would go here given my scenario?
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Test Page 1</title>
</head>
<script language= "JavaScript">
</script>
<body>
<form method="POST" name="Testform" action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults" u-file="fpweb:///_private/form_results.csv" s-format="TEXT/CSV" s-label-fields="TRUE" --><p>
<input type="text" name="Textbox1" size="20"></p>
<p> </p>
<p><input type="button" value="Button" name="B1" onClick="location.reload();">
</p>
</form>
</body>
</html>
Paul Jr
11-08-2004, 11:31 PM
Sessions are not automatically created. You must use the session_start() (http://www.php.net/session-start) function, then create sessions and assign values to them.
I've attached a .zip file containing three pages. The user fills out the form on page 1, then submits the form to page 2, where the data can be manipulated (sent in an e-mail, for example), then is assigned to a session. The user then goes to page 3 where the information is extracted from the session and displayed on the page.
dmichael
11-10-2004, 11:03 PM
Thanks so much Paul Jr for the code examples. I haven't tested them yet, but I did look through all the code and everything makes sense. The point of confusion between us seems to have been that I was trying to do behind the scenes in 1 page what you did in 2 pages(index.html and page2.php). I thought it was possible and was hoping for explanations, but I guess spreading out the processing among 2 or 3 pages should be fine. Now, as for the emailing of the user-entered data, you say that could be handled in page2.php? How would you do it so the user would not have to get involved, via a PHP function, if that's possible? Thanks again.
Paul Jr
11-10-2004, 11:06 PM
It is possible to condense the first two pages into one -- it's a bit more complicated, but certainly doable.
Yes, it is possible to have the info that the user inputted in the form e-mailed to someone without the user having to do anything more than filling out the info and pressing a button. The particular function you're looking for is mail() (http://www.php.net/mail).
If you wish, I can provide another example.