I'm trying to make a form whose fields will be automatically populated from the url. Also, if user clicks on "Save me a seat" it should redirect to one location, send email and save cookies. If user clicks on "Not interested, thank you", it should redirect him/her to another location, send email and save cookies.
Now, I haven't worked with PHP for 2 years now and there is much that I forgot.
Here is the code:
<?php
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'];
$all = array($firstname, $lastname, $title, $company);
if($firstname !=''&& $lastname !=''&& $title !=''&& $company !='')
{
// To redirect form on a particular page
header("Location:http://www.example.com");
}
else{
?><span><?php echo "Please fill in all fields!";?></span> <?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="First Name">
<br><br />
Last name:<br>
<input class="lst-name" type="text" name="lastname" value="Last Name">
<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"></textarea>
</form>
</body>
</html>
It's not redirecting where I want it. For start, how do I fix this?
2 Answers 2
Redirecting cannot be made after you have already sent content. In your case, you have already have HTML content before running header():
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
This should trigger an error but you probably have error reporting turned off. To turn on error reporting, you'll need to include this at the top of your main PHP file that runs every time:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
This will save you time when developing.
To solve your exact problem, you need to move the PHP include file above your HTML content.
12 Comments
html and doctype and head. Also, move the error reporting code above all other PHP code.Your PHP code is not getting run. The default method for a form is get, in your code you try to take something from a HTTP POST which never gets send.
To solve that you should either change the form method attribute, or change your PHP to use $_GET. I would recommend to add method='post' to your form tag. For more information take a look at these two pages on PHP.net, $_POST and $_GET.
For what you describe in your question
I'm trying to make a form whose fields will be automatically populated from the url.
Maybe the get would be more interesting for you.
Next to that, why have this code:
If you have any comments/questions please let us know here:<br />
<textarea rows="4" cols="50"></textarea>
If you are not using it anyway. If you are going to use it, then you should use post as your form method, seeing that a url (and so get) has a limit on its size. Either way you might want to add a name attribute to this textarea.
To recap
- Change your
form methodtopost, or change your PHP code to$_GET - Add a
nameattribute to yourtextareaso that it will be accessable by your code and not useless
includes/redirect.phpand therefore included at the bottom of the HTML file?