what i'm trying to do is get the submitted name, email and message to my php script then send email message. The problem is my form action doesn't trigger instead it reloads the page.
UPDATE
Don't know what im missing from my html form here:
<form method="post" action="">
<div class="input-group">
<input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
<span id="invalid-name">
Please enter at least 2 chars
</span>
</div>
<div class="input-group">
<input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
<span id="invalid-email">
Please enter valid email
</span>
</div>
<div class="input-group">
<textarea id="message" name="message" placeholder="Message">
</textarea>
<span id="invalid-message">
Please write something for us
</span>
</div>
<div>
</div>
<input type="submit" name="submit" value="Book a Demo">
</form>
UPDATE
php script first get values and contruct email message then finally send:
<?php
if (isset($_POST['submit'])) {
include 'index.php';
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
} else {
echo 'isset was false';
}
?>
Here's my folder structure
Im running this in localhost ubuntu apache server.
My Answer:
It seems like you may be missing the method attribute in your form tag. The method attribute specifies the HTTP method to be used when submitting the form data. By default, the method is set to "GET" which may cause the form data to be appended to the URL and reload the page.
To fix this issue, you can set the method attribute to "POST" in your form tag like this:
html
By setting the method to "POST", the form data will be sent in the request body instead of appended to the URL, and your PHP script should be triggered when the form is submitted.
Rate this post
4 of 5 based on 5943 votesComments