我正在尝试创建一个注册表单,其中一些字段将由电子邮件值填充(名字和姓氏)。
我该如何实现?而且,我想使其保存2个值的cookie。一个“被接受”,另一个“被拒绝”。“已接受” Cookie会引导用户访问accepted.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"> <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来自动为我做这件事。
在处理已过帐数据的页面上,设置一个会话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']);
}
?>
希望能帮助您到达需要的地方。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句