Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    $_COOKIE problems. Not storing consistently.

    This is only my second time using cookies, so I may be missing something.

    I have a form:
    Code:
    <form id="eduPartSearch" name="partSrch" method="post" action="results.php">
         <label>Search </label><input type="text" name="qrystr"/>
         <label>PartNumber </label><input type="radio" name="type" value="pn" checked="checked" />
         <label>Description </label><input type="radio" name="type" value="desc" />
         <label>Filter by Type </label><select name="category">
              <option  value="none"> - - Optional - -</option>
              <option  value="none">options ...</option>
         </select>
         <button type="submit">Search</button>
         <button type="reset">Reset</button>
    </form>
    And here are the pertinent parts of my action page:
    PHP Code:
    if(isset($_POST["type"])){setcookie("type"$_POST["type"], $expire);}
    if(isset(
    $_POST["qrystr"])){setcookie("qrystr"mysql_real_escape_string($_POST["qrystr"]), $expire);}
    if(isset(
    $_POST["category"])){setcookie("category"$_POST["category"], $expire);}

    // echo for testing
    echo "search type: ".$_COOKIE["type"]."<br/>";
    echo 
    "search term: ".$_COOKIE["qrystr"]."<br/>";
    echo 
    "category group: ".$_COOKIE["category"]."<br/><br/>"
    The problem is that, sometimes, after submitting a new query from the form page, the values from the previous search are still held in the cookies. After one or two more tries, the cookies will capure the correct data.

    PLEASE HELP !!!

    note: only tested in FF so far, but it must work at least in FF.

    Thanks-a-bunch,
    ~ Mo

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    $_COOKIE is not set until the browser makes a page request. You cannot use setcookie() (which sends the cookie to the browser) and a corresponding $_COOKIE (which is set by any cookies the browser sends when it makes a page request) in the the same page request.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Ohhh!

    So then, should I be using the POST on the first results page, and then just use the COOKIE if the POST !isset?

    ~ Mo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •