John Davidson

php - Message Send Form

0 comments
Message:


How to connect "Message send form" in html file to php so that all Message typed in given form on website are sent directly to my gmail address?


 <section id="contact">
<div class="container">
<div class="row">

<div class="col-md-6 col-sm-12">
<form id="contact-form" role="form" action="action.php" method="post">
<div class="section-title">
<h2>Contact us <small>We are open to talk. We work for you. Let us talk!</small></h2>
</div>

<div class="col-md-12 col-sm-12">
<input type="text" id="text" class="form-control" placeholder="Enter full name" name="text" required="">

<input type="email" id="email" class="form-control" placeholder="Enter email address" name="email" required="">

<textarea id="message" class="form-control" rows="6" placeholder="Tell us about your message" name="message" required=""></textarea>
</div>

<div class="col-md-4 col-sm-12">
<input type="submit" id="submit" class="form-control" name="send message" value="Send Message">
</div>

</form>
</div>

<div class="col-md-6 col-sm-12">
<div class="contact-image">
<img src="images/kingdom.jpg" class="img-responsive" alt="Kingdom Education Logo">
</div>
</div>

</div>
</div>
</section>


My Answer:
To connect the message send form in the HTML file to PHP so that all messages typed in the form are sent directly to your Gmail address, you will need to create a PHP script that handles the form submission and sends the email. Here's an example of how you can do this:

1. Create a new PHP file named "action.php" in the same directory as your HTML file.

2. Add the following code to the "action.php" file:

php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['text'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = "your-email@gmail.com";
$subject = "New message from website contact form";
$body = "Name: $name\nEmail: $email\nMessage:\n$message";
$headers = "From: $email";

if (mail($to, $subject, $body, $headers)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message. Please try again.";
}
} else {
echo "Invalid request";
}
?>


3. Update the form action attribute in your HTML file to point to the "action.php" file:

html



4. Replace "your-email@gmail.com" with your actual Gmail address in the PHP script.

5. Upload both the HTML file and the PHP file to your web server.

Now, when a user submits the form on your website, the form data will be sent to the "action.php" file, which will then send an email to your Gmail address with the message details. Make sure to test the form to ensure that it is working correctly.

Rate this post

3 of 5 based on 2303 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us