-2

I want to add submit form using php but also I want to stay on the same page after clicking the submit button. Also I want that the entries reset after submit button is clicked. I am new to php and I don't know anything about it so please help by giving me proper coding.

This is the PHP code - 

<?php

if(isset($_POST['send'])) {
   // Prepare the email
$to = 'letsavepet@gmail.com';

$name = $_POST['name'];
$mail_from = $_POST['email'];
   $subject = 'Message sent from website';
   $message = $_POST['message'];

$header = "From: $name <$mail_from>";

   // Send it
   $sent = mail($to, $subject, $message, $header);
   if($sent) {
   echo 'Your message has been sent successfully!';
   } else {
   echo 'Sorry, your message could not send.';
   }
}
?>
<html><head><title>Contact Us</title>
</head><body>

<form method="post" action="contact.php" target="formDestination">
    <p>
        <label for="name">Name</label>
        <input type="text" id="name" name="name" required="required" />
    </p>
    <p>
        <label for="email">Email</label>
        <input type="text" id="email" name="email" required="required" />
    </p>
    <p>
        <label for="message">Message</label>
        <textarea id="message" name="message" rows="6" cols="30" required="required"></textarea>
    </p>
    <p>
        <input type="submit" name="send" value="Send message" />
    </p>
</form>

</body></html>
  • 2
    Here's the thing... what you want to achieve is done using AJAX. So go learn AJAX... maybe with the Fetch API, which would be very useful. [More information over here...](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) [and here...](https://developers.google.com/web/updates/2015/03/introduction-to-fetch) – Zeke Feb 01 '18 at 17:05
  • 2
    Also, the idea behind StackOverflow isn't to do the work for you, but to solve issues regarding to a specific problem with some code you wrote and somehow isn't working. So try to do it on your own, maybe follow a tutorial, and if you're stuck on something (for example, you get an error), then ask another question about that. But try to research first, since the question may already have an answer. – Zeke Feb 01 '18 at 17:08
  • 1
    How much research you should do before asking a question: https://meta.stackoverflow.com/a/261593/2960971 – IncredibleHat Feb 01 '18 at 17:11
  • Welcome to Stack Overflow. Check the Stack Overflow's [help on asking questions](http://stackoverflow.com/help/asking) first, please. Focus on [What topics can I ask about here](http://stackoverflow.com/help/on-topic), [What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask), [How to ask a good question](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [Stack Overflow question checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – David Ferenczy Rogožan Feb 01 '18 at 18:44

1 Answers1

2

Remove the action and target attributes of your <form> tag. A form tag without an action attribute defaults to submitting to the current page.

<?php

if(isset($_POST['send'])) {
   // Prepare the email
$to = 'letsavepet@gmail.com';

$name = $_POST['name'];
$mail_from = $_POST['email'];
   $subject = 'Message sent from website';
   $message = $_POST['message'];

$header = "From: $name <$mail_from>";

   // Send it
   $sent = mail($to, $subject, $message, $header);
   if($sent) {
   echo 'Your message has been sent successfully!';
   } else {
   echo 'Sorry, your message could not send.';
   }
}
?>
<html><head><title>Contact Us</title>
</head><body>

<form method="post">
    <p>
        <label for="name">Name</label>
        <input type="text" id="name" name="name" required="required" />
    </p>
    <p>
        <label for="email">Email</label>
        <input type="text" id="email" name="email" required="required" />
    </p>
    <p>
        <label for="message">Message</label>
        <textarea id="message" name="message" rows="6" cols="30" required="required"></textarea>
    </p>
    <p>
        <input type="submit" name="send" value="Send message" />
    </p>
</form>

</body></html>
Ben Harris
  • 547
  • 3
  • 10