Using values from form for cookies and db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Using values from form for cookies and db

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

lets see your form code

is your using get or post method

$user = $_POST['username'];

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

should work
Last edited by tim on Thu May 20, 2004 4:46 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
SBro
Forum Commoner
Posts: 98
Joined: Tue Sep 30, 2003 10:06 pm

Post 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
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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]
Post Reply