質問

I recently started to learn PHP and am having problem with a contact form.

The problem with the form is that it doesn't send an email and echoes the error message below the form even before I send it. Sometimes I get it to work and then it breaks again.

Also, I would like the message "Message sent!" to replace the whole contact form when it is sent. How do I make that happen without linking to another page?

The code is the following:

<form method="POST" action="index.php">
    <input name="name" type="text" placeholder="Name">
    <input name="email" type="email" required placeholder="Email">
    <input name="subject" type="text" placeholder="Subject">
    <textarea name="message" rows="15" required placeholder="Message"></textarea>
    <input name="submit" type="submit" value="Send">
</form>

<?php
    if(isset($_POST['submit'])) 
    {
    $name_field=$_POST['name'];
    $email_field=$_POST['email'];
    $subject_field=$_POST['subject'];
    $message_field=$_POST['message'];
    $to="example@outlook.com";
    $from="example@outlook.com";
    $subject="Contact Form Message";
    $body="Name: $name_field\n Email: $email_field\n Subject: $subject_field\n Message:\n $message_field";
    mail($to,$subject,$body,$from);
    echo "<p>Message sent!</p>"; 
    } 
    else
    {
    echo "<p>An error occured. Please try again.</p>";
    }
?>

Thank you very much for any help whatsoever. Also, if you have any idea how to make the code better, cleaner or more efficient, please do tell!

役に立ちましたか?

解決

You haven't said what doesn't work, but to not display the form if it has been submitted, you want to move your form into the php, and only print it if the form hasn't been submitted.

Also for some further reading to help you out, there are very nice video tutorials here, this one is specifically on making a contact form :-D http://thenewboston.org/watch.php?cat=11&number=100

You also appear to be echoing the error message if the form hasn't been submitted, so one first load you would get the error. And your mail command doesn't looks right.

<?php

//if the form hasn't been submitted yet, print the form.
if (!isset($_POST['submit'])){
print <<<END
<form method="POST" action="index.php">
  <input name="name" type="text" placeholder="Name">
  <input name="email" type="email" required placeholder="Email">
  <input name="subject" type="text" placeholder="Subject">
  <textarea name="message" rows="15" required placeholder="Message"></textarea>
  <input name="submit" type="submit" value="Send">
</form>
END;
}

//if the form has been submitted.
if(isset($_POST['submit'])) 
{
$name_field=$_POST['name'];
$email_field=$_POST['email'];
$subject_field=$_POST['subject'];
$message_field=$_POST['message'];
$to="example@outlook.com";
$subject="Contact Form Message";
$body="Name: $name_field\n Email: $email_field\n Subject: $subject_field\n Message:\n $message_field";
$headers = "From: example@outlook.com";

  if(!mail($to,$subject,$body,$headers)){
      echo 'failed !!';
  }
  else{
      echo "<p>Message sent!</p>"; 
  }
} 

他のヒント

Try this, hope this will work for you:

<? if (!isset($_POST['submit']))
{?>
<form method="POST" action="test.php">
<input name="name" type="text" placeholder="Name">
<input name="email" type="email" required placeholder="Email">
<input name="password" type="password" required placeholder="Password">
<textarea name="message" rows="15" required placeholder="Message"></textarea>
<input name="submit" type="submit" value="Send">
</form>
<?
}
if(isset($_POST['submit']))
$name_field=$_POST['name'];
$email_field=$_POST['email'];
$subject_field=$_POST['subject'];
$message_field=$_POST['message'];
$to="example@outlook.com";
$from="example@outlook.com";
$subject="Contact Form Message";
$body="Name: $name_field\n Email: $email_field\n Subject: $subject_field\n Message:\n $message_field";
mail($to,$subject,$body,$from);
if($body)
echo "<p>Message sent!</p>"; 
if(!$body) 
{
echo "<p>An error occured. Please try again.</p>";
}
}
?>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top