0

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 " />
asked Jan 26, 2011 at 15:41
9
  • Are you sure $_POST['nameField'] has a proper value? Commented Jan 26, 2011 at 15:43
  • what was the value of $_POST['nameField']... var_dump this to your browser to be sure of its value Commented Jan 26, 2011 at 15:44
  • 1
    Add var_dump($_POST['nameField']); Commented Jan 26, 2011 at 15:46
  • 1
    you're confusing id and name entries in the <input> and mixing case Commented Jan 26, 2011 at 15:55
  • 1
    "Why is $_POST['nameField'] NULL?" Because you meant $_POST['Name']. Commented Jan 26, 2011 at 15:58

5 Answers 5

1
<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.

answered Jan 26, 2011 at 15:55
Sign up to request clarification or add additional context in comments.

Comments

0

In the HTML you should write name="nameField", name is the name for posted value not id, if so, the code above seems to be working correctly

answered Jan 26, 2011 at 15:57

Comments

0

The name of the form field is 'Name', the id is 'nameField'. You therefore need to use either

$name = $_POST['Name'];

or change the name on the input to nameField

answered Jan 26, 2011 at 15:57

Comments

0

About the cookie: Cookies will only take effect on the next page load. So if you set a cookie, you can not reference or use the cookie until you load another page. It's just one of the quirks of how they work.

About the POST: You need to use $_POST['name_attribute'] instead of the ID

answered Jan 26, 2011 at 15:58

Comments

0

How do you know it isn't working?

Cookies (and so $_COOKIE) won't be set until the next page load - it has to return the header that sets the cookie to the client before the client will send its request with the cookie in it.

Thus there will be an HTTP request for this script, it will return headers to set the name cookie along with any output, and will only be the next page that will get (and populate $_COOKIE) with the name cookie.

In addition, as noted, $_POST[] references the name attribute of a form element. Your textbox needs to have name='nameField', not just id.

answered Jan 26, 2011 at 15:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.