Reputation: 476
I'm trying to make a sign-up form, where some fields would be populated (First Name and Last Name) by the email values.
How do I achieve this? And also, I want to make it to save cookies, 2 values. One "accepted", other "declined". "Accepted" cookies would lead users to accepted.php and "Declined" cookies would lead users to declined.php page.
Here is my code:
<?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>
Upvotes: 2
Views: 2672
Reputation: 744
You can set cookies with PHP's setcookie() function: http://php.net/manual/en/function.setcookie.php
As for auto-populating the form fields, you could either echo the variables into the form elements manually:
<input type="text" name="something" value="<?php echo $varname; ?>" />
...or you could use this bit of PHP/jQuery I wrote a while back to do it for me automatically.
On the page that processes your posted data set a session var with an input index to your posted data:
$_SESSION['input'] = $_POST;
Then on your page with the form place this in the head of your page...
<?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']);
}
?>
Hope that helps you get where you need to be.
Upvotes: 2