Srinivas V.
Srinivas V.

Reputation: 321

storing cookies and retrieving them in the html form values using php

hi friends i have created a form as below and tried to set cookies for every-field, i succeed in setting cookies for the input text boxes, but i failed to do the same for textarea fields and for drop-down list as well as for check-boxes. The form is as below....

<input name="date" type="text" size="10" value="<?php if(isset($_COOKIE["date"])) echo $_COOKIE["date"];?>" onblur="setCookie(this.name,this.value,60*60*2)" /></td>
          Order Number:
          <input name="order_num" type="text" size="10" value="<?php if(isset($_COOKIE["order_num"])) echo $_COOKIE["order_num"];?>" onblur="setCookie(this.name,this.value,60*60*2)" />
         First Name:
          <td><input name="fname" type="text" value="<?php if(isset($_COOKIE["fname"])) echo $_COOKIE["fname"];?>" onblur="setCookie(this.name,this.value,60*60*2)"/></td>
          Last Name:
          <td><input name="lname" type="text" value="<?php if(isset($_COOKIE["lname"])) echo $_COOKIE["lname"];?>" onblur="setCookie(this.name,this.value,60*60*2)"/>

          Image Submitted:
          <input name="image" type="text" value="<?php if(isset($_COOKIE["image"])) echo $_COOKIE["image"];?>" onblur="setCookie(this.name,this.value,60*60*2)"/>
          General Comments:
         <textarea name="gen_comments" cols="50" rows="6"  onblur="setCookie(this.name,text,60*60*2)" ><?php if(isset($_COOKIE["gen_comments"])) { echo $_COOKIE["gen_comments"];}?></textarea>

          Internal Comments:
          <textarea name="int_comments" cols="50" rows="6" onblur="setCookie(this.name,text,60*60*2)"><?php if(isset($_COOKIE["int_comments"])) { echo $_COOKIE["int_comments"];}?></textarea>
         Quality of the File:
           <select name="quality" onchange="" >
           <option >Select One</option><option name="good" value="<?php if(isset($_COOKIE["good"])) echo $_COOKIE["good"];?>">Good</option><option value="ok">A bit low but we can use it</option><option value="low">Low. We are concerned it might effect the qaulity of the final</option><option value="not good">Not good. We cannot work with it</option><option value="test2">test2</option><option value="test">test</option><option value="lkdjfalkdjlaksjdla dlkajsdaksjdlkajsdlkjaslkdjas alksdjaslkdjlaksjdla">Lisa1</option><option value="bbb">aaa</option>        <!--option value="good">Good</option>
           <option value="ok">A bit low but we can use it</option>
           <option value="low">Low. We are concerned it might effect the qaulity of the final </option>
           <option value="not good">Not good. We can't work with it</option-->
            </select> 
          <input type="button" name="qofoption" id="qofoption" value="Add New Option" 
          onClick="optionWindow('http://www.lightaffection.com/Hema2/addnewoption.php?option=qof','optionwindow','400','200')"/>        </td>
        </tr>
        <tr><div name="qofdiv"></div></tr>
        <tr>
          <td align="right">Image Proportions and Content:</td>
          <td colspan="2" style="padding-right:4px">
           <select name="imageprop" id="imageprop">
           <option>Select One</option><option value="test2">test2</option><option value="not good">Too much details for a Night Light</option><option value="good">Good</option><option value="test">test</option><option value="asdas">asdsa</option><option value="bbb">bbb</option>
          <td><b>How would you like us to proceed?</b></td><td></td></tr>

         <input type="checkbox" name="proceed_opt[]" value="I will upload new Image">
              Insert Option &quot;I will upload new Image&quot;</td></tr>
        <tr><td></td><td colspan="2">
                &nbsp; 
              <input type="checkbox" name="proceed_opt[]" value="I approve this sample for a Night Light">
              Insert Option &quot;<font face="Arial, Helvetica, sans-serif" size="2">I approve this sample for a Night Light</font>&quot; </td></tr><tr><td></td><td colspan="2">
                &nbsp; 
              <input type="checkbox" name="proceed_opt[]" value="Select One">
              Insert Option &quot;<font face="Arial, Helvetica, sans-serif" size="2">Select One</font>&quot; </td></tr>     
         <tr><td></td><td colspan='2'> &nbsp;
         <input type="checkbox" name="proceed_opt[]" value="other">
              Insert Option &quot;Other See comments&quot;</td></tr>

          <td> Ship by date: <input name="shipdate" type="text" /><br />
          <br />
          Update Order Status to: <select name="orderstatus">
            <option>Do not change</option>
            <option>Waiting for designer</option>
            <option>Waiting for customer response</option>
          </select>
          <br />
          <input name="ordermanager" type="checkbox" value="" /> Update Order Manager <br />
          <input name="createhtml" type="checkbox" value="" /> Create an HTML page <br />
          <input name="sendemail" type="checkbox" value="" /> Send Email to Customer <br />      </td>


    </form>

thanks in advance..

Upvotes: 0

Views: 3989

Answers (2)

Zuul
Zuul

Reputation: 16269

Simple example on how to set/read/delete cookies with PHP:

<?php

// set a cookie
setcookie("cookiename", "cookievalue", time()+3600);

// read a cookie
if (isset($_COOKIE["cookiename"])) {
  $mycookievalue = $_COOKIE["cookiename"];
}

// delete a cookie
setcookie("cookiename", "", time()-3600);

?>

To your practical case, just repeat for every field you need to store.

e.g.,

<?php
foreach($_POST as $key =>$value) {
  setcookie($key, $value, time()+3600);
}
?>

KNOW MORE

The cookie Syntax:

setcookie(name, value, expire, path, domain);

Where can you read about this:

W3C - PHP Cookies

PHP.net Cookies

PHP Cookies Tutorial

Upvotes: 1

1321941
1321941

Reputation: 2170

To answer your question, although there are better ways to do this:

PHP cookies must be set before the headers are sent(before any output is given, so you could not set them as you wish)

You are left with the following choices:

 -set the cookies using javascript
 -Create a file which acts as the forms action, and in that file pull all the $_POST values and set them there.

But again I repeat, there are better ways to do this.

Upvotes: 0

Related Questions