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.
Using values from form for cookies and db
Moderator: General Moderators
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
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
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
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
and read about the isset function @ php.net
[/url]
Code: Select all
if(isset($_COOKIE['username'])) {
echo 'you are logged in' . $_COOKIE['username'];
} else {
echo "you need to log-in";
}