gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:36:27 25 4
gpt4 key购买 nike

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

我如何实现这一点?而且,我想让它保存 cookies,2 个值。一个“接受”,另一个“拒绝”。 “接受”cookie 会将用户引导至 accepted.php,“拒绝”cookie 会将用户引导至 declined.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>

最佳答案

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

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

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

...或者您可以使用我不久前写的这段 PHP/jQuery 自动为我完成。

在处理您发布的数据的页面上,设置一个 session 变量,其中包含您发布的数据的输入索引:

$_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']);
}
?>

希望这能帮助你到达你需要去的地方。

关于php - 如何使用 PHP 自动填充输入字段并保存 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com