将值存储到cookie中并在页面加载时显示这些值

时间:2012-05-16 08:19:20

标签: php javascript cookies

嗨,我有一个包含许多字段的php表单,现在我尝试编写用于将所有这些值存储到数组中的代码,并将它们存储在javascript中的cookie中。代码如下:

<script type="text/javascript">

function setDate()
{
var today = new Date();
var month = today.getMonth() + 1;
var day = today.getDate();
var year = today.getFullYear();
document.forms['form1'].elements['date'].value= month + "/" + day + "/" + year;
}

function add_newoption(optionname)
{
  document.getElementById(optionname).InnerHTML = "";
}
var win = null;
function optionWindow(mypage,myname,w,h){
LeftPosition = (screen.width) ? (screen.width-w)/2: 0;
TopPosition = (screen.height) ? (screen.height-h)/2: 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition;
win = window.open(mypage,myname,settings);
win.focus();
}

function setAction(act)
{
    document.forms['form1'].action = act;
    document.forms['form1'].submit();
}

</script>

表格的代码如下:

<body onLoad="setDate();">
<br/>
<center>
<fieldset class="style1" style="width:800px; background-color:#FFFFFF">
<h4 align="center"> Designer Form </h4>
<form id="form1" name="form1" method="post" >
  <table width="700" border="0" cellpadding="2" cellspacing="2" bgcolor="#FFFFFF" style="text-align:left; padding: 25px 0px 25px 0px;">
    <tr>
      <td width="174" align="right">Date:</td>
      <td width="331"><input name="date" type="text" size="10" readonly/></td>
      <td width="175" style="text-align:center;">Order Number:
      <input name="order_num" type="text" size="10" value=""/></td>
    </tr>
    <tr>
      <td >&nbsp;</td>
      <td>&nbsp;</td>
      <td style="text-align:center;"><input name="orderdetails" type="submit" value="Get Order Details" onClick="setAction('?action=getorder');"/></td>
    </tr>
    <tr>

      <td colspan='4'>     <input name="email" id="email" type="hidden" value=""/>     </td> 
    </tr>
    <tr>
      <td align="right">First Name:</td>
      <td><input name="fname" type="text" value=""/></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">Last Name:</td>
      <td><input name="lname" type="text" value=""/></td>
      <td>&nbsp;</td>
    </tr>


    <tr>
      <td align="right">Image Submitted:</td>
      <td><input name="image" type="text" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">General Comments: </td>
      <td><textarea name="gen_comments" cols="50" rows="6"></textarea></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">Internal Comments:</td>
      <td><textarea name="int_comments" cols="50" rows="6"></textarea></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="right">Quality of the File:</td>
      <td colspan="2" style="padding-right:4px">
       <select name="quality">
       <option>Select One</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 cannot work with it</option><option value="test2">test2</option><option value="test">test</option><option value="lisa">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>

现在我想要做的是将表单中输入的所有值存储到数组中,并且必须设置cookie以在页面重新加载时检索它们... 我如何使用javascript编写一个函数,并在表单中调用该函数,以便将这些值放入页面重新加载的表单中。 我需要任何帮助....提前感谢..

1 个答案:

答案 0 :(得分:0)

我认为您应该使用localStorage.setItem("DataKey","DataValue");存储任何值。 为了获得价值,您可以使用localStorage.getItem("DataKey");

FF,chrome,safari支持localStorage,但我不了解IE。