Need help with sending data from html form with php


#1

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 = ‘philipkovachev9@abv.bg’;
$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: webmaster@philipkovachev.com\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 -‘philipkovachev9@abv.bg’);

}
?>

<?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!


#2

mail() function

$success = mail($to, $subject, $message, $headers -‘philipkovachev9@abv.bg’);


#3

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


#4

tried what exactly?

$headers - ‘example@email.com’ will always throw an error


#5

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?


#6

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


#7

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


#8

Somebody pointed out that

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


#9

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.


#10

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 = "qualityweb99@gmail.com";
$mailheader = “From: $mail \r\n”;

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

echo "

Thank you

“;
echo"

your message was sent

”;
?>

#11

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’; }


#12

yes sorry I forgot to add this part :slight_smile: