2

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">&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"></textarea>
</form> 
</body>
</html>

It's not redirecting where I want it. For start, how do I fix this?

3
  • What's the problem exactly? Commented Nov 12, 2015 at 10:49
  • I have made edits. :)Sorry Commented Nov 12, 2015 at 10:53
  • Should we assume that the PHP Code you quote is in includes/redirect.php and therefore included at the bottom of the HTML file? Commented Nov 12, 2015 at 10:57

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

12 Comments

If you remember the Life Cycle of a web page, PHP code should almost always be at the top of a file and include a test so it only runs if some action (pressed a button) has taken place.
What do you mean 'nothing happens'? You know that does not help. Have you moved all of your PHP above HTML? Have you turned on error reporting?
Sorry, I was in a rush. Apologies. Please see the edited question.
Yeah, move the PHP to the top of the file. It should be above everything. This includes html and doctype and head. Also, move the error reporting code above all other PHP code.
I did. Now when I click on "submit", I only get this path: localhost/…
|
1

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

  1. Change your form method to post, or change your PHP code to $_GET
  2. Add a name attribute to your textarea so that it will be accessable by your code and not useless

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.