【问题标题】:How do i set a cookie when a form submits from PHP?从 PHP 提交表单时如何设置 cookie?
【发布时间】:2014-02-27 02:24:19
【问题描述】:

表单提交时如何设置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 不存在,那么它应该显示一个带有“表单”的弹出窗口

一旦在表单上点击提交,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>

【问题讨论】:

  • 但是我需要用户点击提交设置cookie时的代码
  • 不应该像 onSubmit "form" setcookie("v_regUsr", "value");

标签: javascript forms cookies setcookie onsubmit


【解决方案1】:
setcookie("v_regUsr", "value");

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

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

【讨论】:

  • 我知道如何设置cookies....我只需要知道表单提交时如何设置
【解决方案2】:

在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();
          }
      });
});

【讨论】:

  • 我知道如何设置cookies....我只需要知道表单提交时如何设置
  • 这就是我所缺乏的,我所拥有的只是一个工作表格......我错过了 javascript @Krish R
  • form onsubmit 调用 jaavscript 函数,在该函数内部设置 cookie。显示您的html,以便我可以为您提供更多帮助
  • 它看起来可以工作,但它没有放置 cookie...我错过了什么吗? @Krish R
  • 你是如何检查 cookie 值的?
猜你喜欢
  • 2015-08-09
  • 2012-08-02
  • 2017-11-13
  • 2011-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 1970-01-01
相关资源
最近更新 更多