Search code examples
phpsetcookie

Simple PHP cookie question


Why wont this work?

Below code is on send.php

<?php
  $expire=time()+60*60*24*30;
  $name = $_POST['nameField'];
  setcookie("name", $name, $expire);
?>

The cookie's value is blank. Why? How do I fix this?

New question:

Why is $_POST['nameField'] NULL?

<form action="/contact/send.php" method="post" id="contactForm">
<input type="text" id="nameField" name="Name" value="<?php if (isset($_COOKIE["name"])){ echo $_COOKIE["name"];} ?>" class="extra_large" />
<input type="submit" class="submit" name="Submit" value="   Send   " />

Solution

  • <input type="text" id="nameField" name="Name"
    

    This input's name must be nameField , too.

    <input type="text" id="nameField" name="nameField"
    

    $_GET and $_POST variables gets value of form input's , by name. For example $_GET['stack'] and $_POST['stack'] gets <input name="stack">'s value.