Need help with sending data from html form with php

Hello, I am mostly into frontend and now I am trying to link my webform with php and make her send the data to my email, here is the form:

<form action="p.php" id="form" method="post" name="form">
          <input class="lang-en" name="client" placeholder="Your Name" type="text" value="" required pattern="[A-Za-z-0-9]+\s[A-Za-z-'0-9]+" title="firstname lastname">


          <input class="lang-en" name="email" placeholder="Your Email" type="email" value="" required>

          <textarea class="lang-en" name="comment" placeholder="Your Comments Here..." id="comment"></textarea>

          <input class="o-btn lang-en" type="Submit" value="Submit">


        </form>

and my php code:
[php]<?php
if(isset($_POST[‘send’])) {
$to = ‘[email protected]’;
$subject = ‘Message from your Site’;
$message = 'Name: ’ . $_POST[‘name’] . “\r\n\r\n”;
$message .= 'Email: ’ . $_POST[‘email’] . “\r\n\r\n”;
$message .= 'Comments: ’ . $_POST[‘comments’];
$headers = “From: [email protected]\r\n”;
$headers .= ‘Content-Type: text-plain; charset=utf-8’;
$email = filter_input(INPUT_POST, ‘email’, FILTER_VALIDATE_EMAIL);
if($email) {
$headers .= “\r\nReply-To: $email”;
}
$success = mail($to, $subject, $message, $headers -‘[email protected]’);

}
?>

<?php if (isset($success) && $success) { ?>

Thank you

Your message hase been sent <?php } else { ?>

Oops!

Sorry, there was a problem sending your message <?php } ?> [/php]

So the form works in a way, when you type the data it displays the Error message, but it does not make sucessfull sendings, why? What am I doing wrong?

Thank you very much for your help!

mail() function

$success = mail($to, $subject, $message, $headers -‘[email protected]);

Yeah, I tried that but it still dosen’t work

tried what exactly?

$headers - ‘[email protected]’ will always throw an error

I tried to remove my email and just set the mail function to: $success = mail($to, $subject, $message, $headers );
That’s what you meant , right?

Right. And it still shows the failed message?

Is this on a server or local? If local, you need a mailserver running in order for it to work.

I’ll take this time to point out what is normally brought up, using a mail library rather than the mail function: PHPMailer and Swiftmail

Yes. it still displays the Error message, it is run on a server

Somebody pointed out that

if(isset($_POST[‘send’])) {…} never happened but I dont see how it didnt happen?

Your form does not have a field named ‘send’, so the if(isset($_POST[‘send’])) test will always be false and none of the form processing code runs. Since it is the form processing code that sets $success at all, the later code testing $success will always display the Oops … message.

If this is the only form processing code that the page needs to deal with, instead of testing if(isset($_POST[‘send’])), test -
[php]if($_SERVER[‘REQUEST_METHOD’]== ‘POST’)[/php]

Next, you should put the form and the form processing code on the same page. This will let you produce and display validation error messages on the same page with the form and populate the form fields with previously submitted data so that the visitor doesn’t need to keep re-entering data and increase the chance of typo errors.

use this

   $telefon=filter_var($_POST['telefon'], FILTER_SANITIZE_STRING);
    $imie=filter_var($_POST['imie'], FILTER_SANITIZE_STRING);
    $nazwisko=filter_var($_POST['nazwisko'], FILTER_SANITIZE_STRING);
    $mail=filter_var($_POST['email'], FILTER_SANITIZE_STRING);
    $message=filter_var($_POST['message'], FILTER_SANITIZE_STRING);
  $formcontent=" From: $imie \n nazwisko: $nazwisko \n telefon: $telefon \n Email: $mail \n Wiadomosc: $message \n  ";

$recipient = "[email protected]";
$mailheader = “From: $mail \r\n”;

mail($recipient, $mailheader, $formcontent ) or die("Error!");

echo "

Thank you

“;
echo"

your message was sent

”;
?>

just revisiting the isset mentioned earlier:

you typed: “”
but you test for if(isset($_POST[‘send’])).

you should have the following:

then if (isset($_POST[‘send’])) { echo ‘will work’; }

yes sorry I forgot to add this part :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service