开发者

getting cookie value from php form

what do I set the cookie value and name to be for a value a user may enter in a form? and what do i use to display that value on my second page? (I can't not use cookies for this, so while there may be a smarter way to do this, I would just like to know how to do it with cookies!!) Thanks!

<?php
setcookie($color, 'color');
setcookie($name, 'name');
?>

<?php
echo "<form action=\"form_data.php\" method=\"post\">";
echo "favorite color:<input type=\"text\" name=\"color\" size=\"20\"><br/>";
echo "name:<input type=\"text\" name=\"name\" size=\"20\"><br/>";
echo "<input type=\"submit\" value=\"Submit\" />";
echo "<br /><input type=\"hidden\" name=\"submitted\" value=\"true\" />"开发者_StackOverflow;
?>

data on form_data:

  <?php
  echo "<b>fav color:</b>".$_COOKIE['color'];
  echo "<b>name:</b>".$_COOKIE['name'];
  ?>


First, you have your form:

<?php
echo "<form action=\"form_data.php\" method=\"post\">";
echo "favorite color:<input type=\"text\" name=\"color\" size=\"20\"><br/>";
echo "name:<input type=\"text\" name=\"name\" size=\"20\"><br/>";
echo "<input type=\"submit\" value=\"Submit\" />";
echo "<br /><input type=\"hidden\" name=\"submitted\" value=\"true\" />";
?>

Then in form_data.php:

<?php
// set the cookie with the submitted user data
setcookie('color',$_POST['color']);
setcookie('name', $_POST['name']);
echo "<b>fav color:</b>".$_COOKIE['color'];
echo "<b>name:</b>".$_COOKIE['name'];
?>

However, you'll notice that the $_COOKIE variables are not available yet... if you reload that page, they will appear.

In order to accomodate this behavior of cookies, you can setup a redirect in form_data.php:

<?php 
 if (!empty($_POST)) {
  // set the cookie with the submitted user data
  setcookie('color',$_POST['color']);
  setcookie('name', $_POST['name']);
  // redirect the user to final landing page so cookie info is available
  header("Location:form_data.php");
 } else {
  echo "<b>fav color:</b>".$_COOKIE['color'];
  echo "<b>name:</b>".$_COOKIE['name'];
 }
?>

You can redirect them anywhere suitable. Hope this helps and good luck!


function setcookie of your question fail. setcookie($name, $value);

Ex: setcookie('color', 'red');

echo $_COOKIE['color']; //outout: red


I don't know why you want to use cookies.as I know you can use sessions for passing the information of users like this: It is the full example that is working for me

<?php
session_start();
if(array_key_exists('sub',$_POST))
$_SESSION['name']=$_POST['name'];
?>
<html>
<form  method="post">
<input type='text' name="name">
<input type='submit' name='sub' value='send my info'>
</html>

and in another page just use this one:

<?php
session_start();
$r=$_SESSION['name'];
echo $r;
?>

remember to call session_start(); on each page when you want to use sessions


Can anyone explain what baraboom wrote:

However, you'll notice that the $_COOKIE variables are not available yet... if you reload that page, they will appear.

<?php 
 if (!empty($_POST)) {
  // set the cookie with the submitted user data
  setcookie('color',$_POST['color']);
  setcookie('name', $_POST['name']);
  // redirect the user to final landing page so cookie info is available
  header("Location:form_data.php");
 } else {
  echo "<b>fav color:</b>".$_COOKIE['color'];
  echo "<b>name:</b>".$_COOKIE['name'];
 }
?>

Just trying to understand why it is like that since the user is redirected to the page on submit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜