0

What I want to do is to create a simple contact form. I have a file called contact.php with the simple html form:

<form action="process-contact.php" method="post">
    <input type="text" name="name" placeholder="Nombre*">
    <input type="text" name="company" placeholder="Compañía">
    <input type="text" name="position" placeholder="Posición">
    <input type="text" name="country" placeholder="País*">
    <input type="text" name="email" placeholder="Correo electrónico*">
    <input type="text" name="subject" placeholder="Asunto">
    <textarea name="message" placeholder="Mensaje*"></textarea>
    <input type="submit" value="Enviar">
</form>

And I have this php code:

$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
$to = "email@domain.com";

if($name != '' && $country != '' && $email != '' && $message != ''){  
    mail($to, $subject, $message, $headers); //calling php mail function
    echo 'Thank\'s for contacting us. We will be answering back soon.<br><br><a href="index.php">Go back.</a>';
}else{  
    echo 'Plese verify all the fields and try to send the form again.<br><br><a href="index.php">Go back.</a>';  
}

The php code is in another file, but I would like to have the php code on the same file file. Is that possible? How can I do it?

Thank you.

Andrés Orozco
  • 2,490
  • 5
  • 32
  • 48
  • Yes, easily possible by just placing the PHP code above the form code, and checking first that `$_SERVER['REQUEST_METHOD'] == 'POST'` before processing it. [Many many examples](http://stackoverflow.com/search?q=%5Bphp%5D+post+to+same+form) are already around here. – Michael Berkowski Sep 02 '13 at 00:39
  • 1
    Or ... `action="$_SERVER['PHP_SELF']"...` – Black Sheep Sep 02 '13 at 00:41
  • @aldanux It worked with PHP_SELF but I have two problems: 1. It doesn't matter if I haven't sent the form, I always get the "Please verify all the fields and try to send the form again" in the middle of the page. 2.If I reload the page it ask me to send the form again and I receive another Email. – Andrés Orozco Sep 02 '13 at 01:08
  • @AndresOrozco -- try to change the operator `&&` (and) to `||` (or).. and maybe use the function empty(). Try this: `if (!empty($name) || !empty($country) || !empty($email) || !empty($message)) {...` However but your question was: I would like to have the php code on the same file file. Is that possible? How can I do it? ;-) (edited) – Black Sheep Sep 02 '13 at 01:33
  • @AndresOrozco I posted an answer with 2 handler options. Try it out and let me know. – Funk Forty Niner Sep 02 '13 at 02:32

3 Answers3

1

You can put everything inside one file and using action=""

Your headers had errors in them and by using those, the Email ended up in my SPAM box, so I changed those with proper headers and put other details using the $message variable.

I added header('Location: thank_you.php'); and will redirect to that page if all the fields were filled. Create a file called thank_you.php or change it to go to your home page.

I replaced:

if($name != '' && $country != '' && $email != '' && $message != ''){

by:

if(empty($_POST['name']) ||
    empty($_POST['email']) ||
    empty($_POST['country']) ||
    empty($_POST['message'])) {

    echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}

Form and PHP handler (tested successfully)

<?php

$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email@example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';

if(empty($_POST['name']) ||
    empty($_POST['email']) ||
    empty($_POST['country']) ||
    empty($_POST['message'])) {

echo "Fill in all the fields. All marked by an asterisk are mandatory.";

}

else {

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
            "Reply-To: $email" . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
header("Location: thank_you.php");
}
?>

<!DOCTYPE html>

<html>
<head>
</head>
<body>

<form action="" method="post">
    <input type="text" name="name" placeholder="Nombre*">
    <input type="text" name="company" placeholder="Compañía">
    <input type="text" name="position" placeholder="Posición">
    <input type="text" name="country" placeholder="País*">
    <input type="text" name="email" placeholder="Correo electrónico*">
    <input type="text" name="subject" placeholder="Asunto">
    <textarea name="message" placeholder="Mensaje*"></textarea>
    <input type="submit" name="submit" value="Enviar">
</form>

</body>
</html>

Or you can use this as your PHP handler

<?php

$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email@example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';

if(!empty($_POST['name']) &&
    !empty($_POST['email']) &&
    !empty($_POST['country']) &&
    !empty($_POST['message'])) {

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $email" . "\r\n" .
            "Reply-To: $email" . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers); //calling php mail function
    echo 'Thanks for contacting us. We will be answering back soon.<br><br><a href="index.php">Go back.</a>';

// to prevent re-submission, use header but NOT with echo.
// header("Location: thank_you.php");

}else{  
    echo 'Please verify all the fields.<br><br><a href="index.php">Go back.</a>';  
}

?>

<!DOCTYPE html>

<html>
<head>
</head>
<body>

<form action="" method="post">
    <input type="text" name="name" placeholder="Nombre*">
    <input type="text" name="company" placeholder="Compañía">
    <input type="text" name="position" placeholder="Posición">
    <input type="text" name="country" placeholder="País*">
    <input type="text" name="email" placeholder="Correo electrónico*">
    <input type="text" name="subject" placeholder="Asunto">
    <textarea name="message" placeholder="Mensaje*"></textarea>
    <input type="submit" name="submit" value="Enviar">
</form>

</body>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

Set the action attribute of the form to the name of the file containing the form and php code and then include all your form processing php code in the following conditional.

if(count($_POST)) > 0)

This will make sure the php code runs only when the form is submitted.

Bad Wolf
  • 8,206
  • 4
  • 34
  • 44
0

You'll want to use the isset feature of PHP, perhaps.

$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}
Gary Hayes
  • 1,728
  • 1
  • 15
  • 23
  • Better if you use a ternary operator or do not set name until you verify `$_POST['name']` is set to avoid the undefined index notice. `$name = isset($_POST['name']) ? $_POST['name'] : null;` – Prix Sep 02 '13 at 00:48
  • Better still is to validate on client side before submission. – Gary Hayes Sep 02 '13 at 02:53
  • Nope, you can validate on the client side for double verification but you should never trust on the client side data you should always make sure to validate on your server side to make sure the data is valid as it can be manipulated from client side. – Prix Sep 02 '13 at 14:30