i have written a php code that saves a cookie containing a certain value then another cookie to retrieve that value, like that:
<?php
$inTime = 60 60 24 * 60 + time();
$txt = date("D, M/d/Y")." ".'never been here';

setcookie('mylastvisit',$txt ,$inTime);
?>

<?php

if(isset($COOKIE['lastvisit'])) {
$visit = $
COOKIE['lastvisit'];

echo "Your last visit was - ". $visit;
}

else
{echo "Cookie NOT Working!"; }

echo isset($COOKIE['lastvisit'])? $COOKIE['lastvisit'] : ''
?>

//------------------------------------…
then i added a form with three text areas and a submit button to let the user write in 'em, now i need to make the form save what the user writes in a cookie so as to retrieve it later before being saved to database...
and can i make a button to save to a cookie and a button to save to database?
can i make the values get saved to cookie automatically???

form goes like::

<form>
<input type="text" name="box1" />
<input type="text" name="box2" />
<input type="text" name="box3" />
<input type="text" name="box1" />
<input type="submit" value="submit" name="submit" />

any help is much appreciated!!

    eternityseeker wrote:

    now i need to make the form save what the user writes in a cookie so as to retrieve it later before being saved to database...

    Okay, so... what have you tried? You seem to already have the gist of how cookies work, so I'm confused as to what it is that you're having problems with.

    eternityseeker wrote:

    can i make a button to save to a cookie and a button to save to database?

    Sure; give the two buttons different names and/or values so that you can tell which button the user clicked.

    eternityseeker wrote:

    can i make the values get saved to cookie automatically???

    Er... are you asking if it's possible for PHP to read your mind and know that you want certain POST'ed data to automagically cause cookies to be set? If so, then the answer is no.

      thanks for replying, i don't know how to get the what the user type saved in the cookie, i'm not sure what should be the value of each input box, i think what i'm missing is the value, the value of the input box, the value of the submit button wich saves it all to a cookie, i mean i could use something like:

      <input type="text" name="box1" value=" <?php echo isset($COOKIE('mylastvisit'))? $COOKIE('mylastvisit') : " " ?>" />

      i know that the value here will be to check if the cookie has something saved and then echo it back in the text box, but how can i save what has been written here to the cookie in the first place, i mean the value here will be nothing if the cookie has nothing saved, right??

      i'm sorry i'm new to php but i really really wanna learn this, so can you please help me on this... thanks again...

        eternityseeker wrote:

        i don't know how to get the what the user type saved in the cookie

        Have you read through the following PHP manual page: [man]variables.external[/man] ? That pretty much illustrates how to deal with incoming data (e.g. from a form), so from there you simply use the incoming data as the 'value' of the cookie.

          You're setting the cookie on line 5 with this code:

          setcookie('mylastvisit',$txt ,$inTime);

          You're setting a cookie named "mylastvisit"

          but later on (a couple lines down), you're trying to reference it by a different name -

          $_COOKIE['lastvisit']

          $COOKIE['lastvisit'] needs to be changed to $COOKIE['mylastvisit']
          and I think that will solve your issues???

          To update a cookie, you just have to set it again with: setcookie('mylastvisit',$txt ,$inTime);

          Cookies are funny, they're not like sessions or post value superglobals, meaning you can read and write a session variable like this: $SESSION['a'] = 'haha'; and then read it with $SESSION['a']
          but for cookies you write a cookie with setcookie(); and can read it with $_COOKIE[''] only.

          Does that help???

            well i checked PHP manual page: variables.external, i've changed the cookie name to $_COOKIE['mylastvisit'], i forgot to edit it..
            i can set a cookie and recall what i've already set in the cookie, but what i'm not able to do is make what's typed in the text box get added to the cookie, because i used $txt variable as the value in the text when i used :
            $setcookie('mylastvisit',$txt ,$inTime);

            so if i used the cookie in a box it will look like this :

            <input type="text" name="box1" value="<?php echo isset($COOKIE['mylastvisit'])? $COOKIE['mylastvisit'] : ' ' ?>" />

            and that will make what i've already added in the cookie show up in "box1" and it will not let me add what i write in the box to the cookie...
            can you help me here ? 🙂 🙂 🙂

              i wanna know how to make what i write in the box get added to the cookie, the "variables.external" page helped me with the post command but i need to use a cookie, any ideas?

                well i found out how to do it thanks anyway..
                here's my code, i hope somebody Find this useful :

                <!-------------------------------My Code-------------------------->
                <html>

                <title>Set My Cookie 7 - By Mohamed A.Nasr</title>
                <head>

                <script type="text/javascript">
                //Javascript Functions - By Mohamed A.Nasr
                function writeCookie(name, value, expire) {
                var date = new Date();
                date.setTime(date.getTime()+(360000));
                var expires = "; expires="+date.toGMTString();

                document.cookie = name+"="+value+expires;
                }

                function savemydata() {
                // if (!(document.getElementById('cfd2').disabled)) {
                id="yourcookie7";
                writeCookie(id, escape(document.getElementById(id).value));
                }

                function get_cookie ( cookie_name )
                {
                var results = document.cookie.match ( '(|😉 ?' + cookie_name + '=([;]*)(;|$)' );
                if ( results )
                return ( unescape ( results[2] ) );
                else
                return '';
                // return null;
                }

                function clearAll() {
                document.data_form.text0.value = '';
                document.data_form.text1.value = '';}

                function getdata() {
                document.data_form.text1.value = get_cookie('yourcookie7');}

                function cookiedie() {

                document.cookie = 'yourcookie7'+"="+'null'+";expires=Fri, 01-Sep-2000 00:00:01 GMT";
                }

                </script>
                </head>
                <body onLoad="getdata();">
                <table align="left">
                <tr>
                <td>
                <FORM>
                Enter A Word: <INPUT TYPE="text" NAME="yourcookie7" id="yourcookie7" size="20"/>
                <INPUT TYPE="button" Value="Set to Cookie" onClick="savemydata()" />
                </FORM>

                <br />

                <p>
                <a href="javascript:get_cookie('yourcookie7')" style="color:#009900">Read cookie using JavaScript 2 - This One IS REALLY WORKING!!!!!!</a><br />
                </p>
                </td>
                </tr>
                </table>

                <table align="left" style="margin-left:20px">
                <tr>
                <td>
                <form name="data_form">
                <input type="button" name="get data" value="get data" onClick="window.location.href=window.location.href" />
                <input type="text" disabled name="text0" style="background-color:#CCCCFF" value="<?php echo isset($COOKIE['yourcookie7'])? $COOKIE['yourcookie7'] : '' ?>"/>
                <input type="text" disabled name="text1" style="background-color:#CCCCFF" /><br />
                <input type="button" name="Clear" value="Clear" onClick="clearAll();" /><br />
                <input type="button" name="kill Cookie" value="Kill My Cookies!" onClick="cookiedie();" />
                </form>
                </td>
                </tr>
                </table>

                </body>
                </html>
                <!-------------------------------End Of Code-------------------------->

                  Write a Reply...