如何自动填充输入字段并使用PHP保存cookie?

时间:2015-11-12 16:17:25

标签: php jquery html forms cookies

我正在尝试制作一个注册表单,其中一些字段将通过电子邮件值填充(名字和姓氏)。

我如何实现这一目标?而且,我想让它保存饼干,2个值。一个“接受”,另一个“拒绝”。 “接受”的cookie会导致用户接受.php和“拒绝”的cookie会导致用户拒​​绝.php页面。

这是我的代码:

<?php

///Check for errors and display them 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$title = $_POST['positiontitle'];
$company = $_POST['companyname'];
$comment = $_POST['comment'];
$all = array($firstname, $lastname, $title, $company);
if($firstname !=''&& $lastname !=''&& $title !=''&& $company !='')
{
//  To redirect user to accepted page
header("Location:accepted.php");
}
}
//  To redirect user to declined page
if (isset($_POST['notsubmit'])) {
    header("Location:declined.php");
}
?>
<!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" />
<meta content="noindex, nofollow" />
</head>
<body>
 <form class="conf-form" method="POST">
    <legend>Please input your information:</legend><br />
    First name:<br>
    <input class="fst-name" type="text" name="firstname" value="<?php echo htmlentities($_GET['firstname']);?>">
    <br><br />
    Last name:<br>
    <input class="lst-name" type="text" name="lastname" value="<?php echo htmlentities($_GET['lastname']);?>">
    <br /><br />
    Position title:<br />
    <input class"pos-title" type="text" name="positiontitle" value="Position Title"/>
    <br /> <br />
    Company name:<br />
    <input class="cmp-name" type="text" name="companyname" value="Company Name"/>
    <br /><br />
    <input id="save-me" type="submit" value="Save me a seat" name="submit">&nbsp;&nbsp;&nbsp;&nbsp;<input id="not-int" type="submit" value="Not interested, thank you" name="notsubmit">
    <br /><br />
    If you have any comments/questions please let us know here:<br />
    <textarea rows="4" cols="50" name="comment"></textarea>
</form> 
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您可以使用PHP的setcookie()函数设置Cookie:http://php.net/manual/en/function.setcookie.php

对于自动填充表单字段,您可以手动将变量回显到表单元素中:

<input type="text" name="something" value="<?php echo $varname; ?>" />

...或者你可以使用这段PHP / jQuery我写了一段时间后自动为我做。

在处理发布数据的页面上设置会话var,其中包含发布数据的输入索引:

$_SESSION['input'] = $_POST;

然后在您的页面上将表格放在页面的头部......

<?php
//Place in the <head> of your page. Requires jQuery
if($_SESSION['input']) {
    //reject sensitive fields
    $reject = array("password","cc_num");
    foreach($reject as $r) {
        unset($_SESSION['input'][$r]);
    }

    echo '
<script>
$(document).ready(function() {
  var inputs = '.json_encode($_SESSION['input']).';
  $.each(inputs,function(index,value) {
      $("[name="+index+"]").val(value);
  });
});
</script>
';
    unset($_SESSION['input']);
}
?>

希望能帮助你到达目的地。