0

嗨朋友们,我创建了一个如下的表单并尝试为每个字段设置 cookie,我成功为输入文本框设置 cookie,但我未能为 textarea 字段和下拉列表以及复选框。表格如下......

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

提前致谢..

4

2 回答 2

1

关于如何使用 PHP 设置/读取/删除 cookie 的简单示例:

<?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);

?>

对于您的实际情况,只需对您需要存储的每个字段重复。

例如,

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

了解更多

cookie 语法:

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

你在哪里可以读到这个:

W3C - PHP Cookie

PHP.net Cookie

PHP Cookie 教程

于 2012-05-17T15:19:48.273 回答
0

要回答您的问题,尽管有更好的方法可以做到这一点:

必须在发送标头之前设置 PHP cookie(在给出任何输出之前,因此您无法根据需要设置它们)

您有以下选择:

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

但我再次重申,有更好的方法可以做到这一点。

于 2012-05-17T15:27:01.580 回答