Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to preload user data on form via cookie & Perl/CGI? 1

Status
Not open for further replies.

JCHallgren

Technical User
Dec 17, 2004
47
0
0
US
Hi!

New to this (HTML/PERL) but have just read most ALL the SSI Faq links..

Have a simple chatroom based on a variant of a guestbook script...works fine...but would like to save users name/city/st (after a succcesful posting) so that when they return again, i might (assuming cookie was there) preload those fields in FORM data as initial values...

Have a main index page...it links to the "ADD" page...would using SSI on ADD page to do a "include virtual cookie.pl" type req work? So that it would read cookie data and modify the HTML for the page? But then...wouldn't I have to have all the ADD page code embeded in the PL pgm? Which makes the ADD page hard to maintain...

How should i approach this issue?
I want to have this be VERY secure also!

I read that using the XbitHack(?) method might be best way to have SSI code in just the HTML I wish as my Perl Code is in another path totally..

Hope you understand my questions! THANKS!
 
Could you point me to a place where i might find more info on how to do so via Java? And wouldnt that option require Java on users PC also?
 
Javascript is different from Java and they even have different vendors. Almost all users are javascript-ready and javascript is widely used. In fact it is hard to find too many sites that do not use JavaScript even if in some minor limited form.

If you want do use a Javascript solution I would gladly send you one. However it would be a lot easier for me or others to help you if you would post the code for where you want the information to be saved to a cookie.

The easiest place would be in the form where you collect the user information in in the first place.

If you are able to use PHP the solution is way easier, but in either case me or plenty of people could help you.

Clive
 
Clive,

THANKS for the clarification! I did't notice the "script" suffix to Java...

My host says they support PHP so if that would be easier, then maybe I'll persue that...either way...whatever would be easier to understand for a NEWBIE!

As I stated before, I have a simple guestbook that uses a input FORM in HTML to get data...and then a Perl CGI to process it...(which works great!) I would only need to be able to somehow put in a value for the NAME/CITY/ST fields when the ADD form is first displayed (IF they had done so before at my site)

Currently, the ADD form is just a plain HTML page...

If you can give me a sample (or links to where to see one)of how I would do one field, I can obviously adapt to others...and would be MOST grateful!
 
To see it in action go to:
The code below will look better after you cut and paste it.

Code:
INDEX.PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en" lang="en">
<!-- ***************************** HEAD **************************** -->
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Contact Form</title>
<!-- ***************************** BODY **************************** -->
<body><h3>Contact Form</h3>
<form name="mail" method="post"
 enctype="multipart/form-data" action="useinfo.php">
<table>
<tr><td>To: </td><td><b>YOUR SITE</b></td></tr>
<tr><td>Userid: </td>

<?php
//GET A COOKIE if it still exists on users machine
$userid=$_COOKIE[userid];
$password=$_COOKIE[upword];

// ****** remove after testing is ok *******
if (isset($_COOKIE['userid'])) {
  echo('cookie found');
  echo($userid);
  echo($password);
}else{
  echo('cookie not found');
}
// *****************************************

echo('<td><input name="u" type="text" size="36" value="'.$userid.'" /></td></tr>');
echo('<tr><td>Password: </td>');
echo('<td><input name="p" type="text" size="36" value="'.$password.'" /></td></tr>');
?>

<tr><td>&nbsp;</td><td><input value="Send" type="submit" /></td></tr>
</table></form></body></html>

Code:
USEINFO.PHP
<?php
//SAVE A COOKIE on the users machine for 1 year
$userid=$_POST['u'];
$password=$_POST['p'];
setcookie('userid', $userid, time()+31622400, '/');
setcookie('upword', $password, time()+31622400, '/');

// ****** remove after testing is ok *******
echo($userid);echo('<br />');
echo($password);
// *****************************************

?>
<html><head><title>rest of your HTML code</title></head>
<body><br /><br />etc: </body></html>

Clive
 
I do GREATLY appreciate the time/effort you put into that reply!!! I follow/understand that it requires a two-phase/module approach, correct? One to build the form and one to process it, right? Based on my readings thus far, i see to get all of what INDEX.PHP does...and USEINFO.PHP.

Where i am lost is WHERE and how do i execute/invoke the CGI script that is now in the "Action" parm for the Form?
(And does it have to be modified?) Would I use a Virtual command or similar somewhere? As right now, the form goes to the Perl code to be processed (which may update the guestbook if values are correct)

I hope I have not been to much of a bother.. but have NOT yet found a similar example on various sites..
 
I do fully realize how you may have missed it (even though I mentioned it in three replies thus far) as my eyes get blurry sometimes :)

The CGI/Perl does a "simple chatroom based on a variant of a guestbook" so it validates the Form data, and either gives user a chance to fix it, or adds entry to guestbook, and shows results...

I have this in my HTML page for doing the Add:
FORM METHOD=POST ACTION="cgi-bin/updateTC.cgi"

 
I do fully realize how you may have missed it (even though I mentioned it in three replies thus far) as my eyes get blurry sometimes :)

The CGI/Perl does a "simple chatroom based on a variant of a guestbook" so it validates the Form data, and either gives user a chance to fix it, or adds entry to guestbook, and shows results...

I have this in my HTML page for doing the Add:
FORM METHOD=POST ACTION="cgi-bin/updateTC.cgi"

 
I realize that you said in general terms what it did but I meant more specifically. However, if you are completely happy with your CGI/Perl solution it seems that you have 2 choices.
1. Ask in the Perl forum how to send and get a cookie. That way you would keep your server-side code all in one language.
OR
2.Call your CGI from index.php as you were doing (but with the get cookie part). When you were sure you wanted to write a cookie, call useinfo.php using a hidden form with the fields entered as values in the form fields ( type="hidden" instead of type="text". in the example I gave you would need to get the entered userid and password into a hidden form with action="useinfo.php".

Clive
 
I almost hate to ask again...but here goes:

The downside with using all Perl is (as I understand it) that loading the form from cookies is more convoluted..
Since I would have to more or less have HTML in the code..
So use of PHP seems to be easier...

Should I want to "always" write the cookie, would i then execute the CGI in USEINFO instead of having it do HTML display? You had said it could be called from there...but how? And thus would there have to be any other HTML code for display there? I'm fuzzy on that..

Once i get over this hump, it's downhill from here :)
 
Really I see no value in conditionally writing the cookie, just write it and then move on with your processing. The advantage in doing it in Javascript is that you could get and put the cookie in the same HTML. In PHP, writing cookies has to be the first thing done in a new browser window.

If you used PHP you could use my original example, as written and then call your cgi program after writing the cookie in useinfo.php

The general advantage to PHP is that users cannot see your code.

To call a program using POST, you have to send a form. However the fields can be hidden.

<form name="mail" method="post"
enctype="multipart/form-data"
action="cgi-bin/updateTC.cgi">
<input type="hidden" length="36" value="USERID" />
etc.

If you were using PHP you could get the variable fields in
by echoing the line and using .$userid. as I did in the first example in INDEX.PHP. Periods (full stops) are the concatenation characters in PHP.




Clive
 
That's part of the problem for me also...I did COBOL for 23+ yrs so this stuff is alien to me! However, i do like to let people know that i value their time so like to thank them and not feel it is expected...

One thing you said left me wondering..."In PHP, writing cookies has to be the first thing done in a new browser window."
Does that imply that the second module would create a new window? How would that appear to user? Or would it simply update the screen and then be overlaid by output from the CGI/Perl?
 
Does that imply that the second module would create a new window?
NO

would it simply update the screen and then be overlaid by output from the CGI/Perl?
YES

Don't know whether you use PC COBOL, but if you do I have put out a couple of COBOL Webler faqs in the COBOL forum. I also do a lot of stuff with automatic HTML generation from COBOL.




Clive
 
This reply may be a bit late but here goes.
I would handle the whole thing from within the perl script.
Attempt to set a cookie as the visitor registers with you and also issue them with a user name and password.
If subsequent visits load a cookie - you have all the details on file and can welcome them by name.
If an attempted cookie read fails, display a standard Log In / Register screen.

Keith
 
Audiopro, you late reply was ok...but I have already coded it in Javascript using cookie and got it working (thanks to tips in that forum)...and most of your reply would not have worked for my site as it uses NO registration...just wanted to save form input for next time...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top