Page 1 of 1

Using values from form for cookies and db

Posted: Thu May 20, 2004 2:16 am
by SBro
I have a form on my site, one section of the form contains user details (name, address, etc) Once submitted I need the data from the form to be inserted into a table but also a cookie to be set. I know how to get the values into the database, but the problem I'm having is setting a cookie with a value and submitting the values to the database. The reason I want a cookie is so that when users return to the page with the form, their contact details will already be populated. There is no login system (and I don't want one) thus I need the cookie.

Am I missing something? The problem I see is that when submitting the form to the database I then need to send the user to another page, which is why I can't work out how to set the cookie for the previous (form) page. Can I set a cookie with values and submit them to a database at the same time? Ideally I'd like to call the cookie at the top of the (form) page, use that cookie's value to send a query to the database for the users contact details and populate the form boxes with that information. I know the code for all the database side of things, just not sure how to incorporate a cookie into it. Hope I've made some kind of sense, it's pretty hard to describe, any help would be appreciated, thankyou.

Steve.

Posted: Thu May 20, 2004 4:43 pm
by tim
lets see your form code

is your using get or post method

$user = $_POST['username'];

setcookie("username", $user, time() +36000);

should work

Posted: Thu May 20, 2004 4:45 pm
by tim
oh and you can set a cookie anywhere you please. aslong, of course, you dont send any <html> or any output before it.

you can insert the values into the db, and set the cookie, one command after another.

u should atleast have tried it.

Posted: Thu May 20, 2004 8:56 pm
by SBro
In my form I am using post method, I have the action going to another php page where the form values are inserted into db. For eg. in the simplest fashion, should the following work

forms.php:

<form action="process.php" method="post">

<input type="text" name="First">
<input type="text" name="Last">
<input type="text" name="Address">

process.php:

code to get form values and insert in db
$first = $_POST['First'];

setcookie("first", $first, time() +36000);


Won't that set the cookie for "process.php" however and not "forms.php" you see my problem ? thanks for your help.

Steve

Posted: Thu May 20, 2004 9:12 pm
by tim
maybe you have no idea what a cookie is.


no matter where you set it, unless you set the path, will/can be accessed anywhere withint that domain.

so u can access the cookies vars via process.php or the page that holds the form.

and yes that cookie set work.

you should try scripts/codes first, then come back n post errorsssss (if any)

save time n annoyances

Posted: Thu May 20, 2004 9:16 pm
by tim
and read about the isset function @ php.net

Code: Select all

if(isset($_COOKIE['username'])) {
echo 'you are logged in' . $_COOKIE['username'];
} else {
echo "you need to log-in";
}
[/url]