简体   繁体   中英

php redirecting page and not sending email

When a I hit submit on my form, the form redirect's me to "process.php" and the email doesn't send, but when I hit refresh it sends the email with garbage in it. How can I get PHP not to redirect me to a different page and send the email based on what's in the form.

Here's my code

PHP

$toemail = 'email@gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
    echo 'Your email was sent successfully.';
} else {
    echo 'There was a problem sending your email.';
}

HTML

<h2>Leave a message</h2>
  <div class="sepContainer"></div>
  <form action="process.php" method="post" id="contact_form">
    <div class="name">
      <label for="name">Your Name:</label>
      <p> Please enter your full name</p>
      <input id=name name=name type=text placeholder="e.g. Mr. John Smith" required />
    </div>
    <div class="email">
      <label for="email">Your Email:</label>
      <p> Please enter your email address</p>
      <input id=email name=email type=email placeholder="example@domain.com" required />
    </div>
    <div class="message">
      <label for="message">Your Message:</label>
      <p> Please enter your question</p>
      <textarea id=message name=message rows=6 cols=10 required></textarea>
    </div>
    <div id="loader">
      <input type="submit" value="Submit" />
    </div>
  </form>

It's hosted on a Unix server. Try not to be to harsh I've only been learning PHP for 2 days now.

   <?php
$name = $_POST ['name'];
$request = $_POST['request'];

$to = "(email goes here)";
$subject = "Website Quote";
$body = "This is an automated message. Please do not reply to this emai. \n\n $request";

mail ($to,$subject,$body);

echo "Message Sent. <a href='index.html'>Click here to return to the site.</a>"
 ?>

Here is REALLY basic PHP code to send an email, which is what it seems like you're attempting to do.

HTML -

 <form action="send.php" method="POST">
      Name: <br> <input type="text" name="name"><br>
      Request <br> <textarea name="request"></textarea><br><br>
      <input type="submit" name="submit" value="Send">
    </form>

Hopefully this clears something up, if I approached this incorrectly please explain it differently, thanks, have a great day!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM