当表单从PHP提交时如何设置cookie?

时间:2014-02-26 15:43:37

标签: javascript forms cookies setcookie onsubmit

如何在表单提交时设置cookie?

这是我在使用cookie后即可使用的代码。

<?php
if(isset($_COOKIE['v_regUsr']))
{
header("Location: dont-show-pop-up-with-form.php");
}
else
{
header("Location: show-pop-up-form.php");
}
?>

但是我需要在提交表单时使用'v_regUsr'的名称或值设置cookie。

因此用户访问该网站。页面检查cookie是否存在...如果是,那么它不会显示弹出窗口。如果cookie不在那里,那么它应该显示一个带有“form”的弹出窗口

一旦表单上的点击提交,就会放置cookie“v_regUsr”并且他们不会再看到弹出窗口。

任何人都可以帮助我吗? 感谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<script src="https://app.buildtopia.com/english/include/jquery-plugins/jquery.validate.js" type="text/javascript"></script>
<script src="https://app.buildtopia.com/english/include/jquery-plugins/jquery.metadata.js" type="text/javascript"></script>

<script type="text/javascript">
  $.metadata.setType("attr", "validate");

  $(document).ready(function() {
    $("#theForm").validate();
  });
</script>




<style type="text/css">
  #theForm { width: 100%; }
  #theForm label { display: block; float: left; width: 200px; text-align: right; padding-right: 5px; margin-right: 10px; }
  #theForm label.required { border-right: solid 3px red; }
  #theForm fieldset { border: none; margin: 0 0 0 215px; }
  #theForm fieldset input { display: block; float: left; }
  #theForm fieldset label { float: none; text-align: left; padding-left: 5px; width: auto; }
  #theForm label.error, #theForm input.submit {  display: none; float: none; color: red; }
  .question { padding-bottom: 15px; }
</style>

 <form id="theForm" name="theForm" action="send.php" method="post"> 
      <div class="question">
 <label for="fname"  class="required"  >First Name</label>
 <input id="fname" name="fname" size="25" maxlength="40"  validate="{required:true, messages:{required:'Please enter your first name'}}"  /> 
 </div>
<div class="question">
 <label for="lname"  class="required"  >Last Name</label>
 <input id="lname" name="lname" size="25" maxlength="40"  validate="{required:true, messages:{required:'Please enter your last name'}}"  /> 
 </div>
<div class="question">
 <label for="home_phone"  class="required"  >Home Phone</label>
 <input id="home_phone" name="home_phone" size="15" maxlength="20"  validate="{required:true}"  /> 
 </div>
<div class="question">
 <label for="email1"  class="required"  >Email (Primary)</label>
 <input id="email1" name="email1" size="50" maxlength="55" validate="{ required:true,  email:true }" /> 
 </div>
<div align="right">
  <input type="submit" name="submit" value="Register" />
</div>
 </form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

setcookie("v_regUsr", "value");

您还可以为Cookie添加有效的持续时间:

setcookie("v_regUsr", "value", time()+3600*24*365);  // 1 year.

答案 1 :(得分:0)

在javascript中:

 document.cookie="v_regUsr=" + cookievalue;

参考:http://www.tutorialspoint.com/javascript/javascript_cookies.htm

如果您想使用php设置Cookie,请使用 setcookie() 功能

$value = 'Your Values';
setcookie("v_regUsr", $value, time()+3600);  /* expire in 1 hour */

<强>更新

 $(document).ready(function() {      
   $("#theForm").validate({
       submitHandler: function(form) {
       // do other things for a valid form
             var cookievalue="your value";
             document.cookie="v_regUsr=" + cookievalue;
             form.submit();
          }
      });
});