PHP ContactForm - php

I cant seem to figure out what the problem is with my code. After pressing submit it will redirect to a error page and says "This page isn’t working website is currently unable to handle this request. HTTP ERROR 500"
HTML
<div class="container">
<form action="contact.php" method="POST" class="form">
<div class="form-group">
<label for="name" class="form-label">Your Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Jane Doe" tabindex="1" required>
</div>
<div class="form-group">
<label for="email" class="form-label">Your Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="jane#doe.com" tabindex="2" required>
</div>
<div class="form-group">
<label for="subject" class="form-label">Subject</label>
<input type="text" class="form-control" id="subject" name="subject" placeholder="Hello There!" tabindex="3" required>
</div>
<div class="form-group">
<label for="message" class="form-label">Message</label>
<textarea class="form-control" rows="5" cols="50" id="message" name="message" placeholder="Enter Message..." tabindex="4"></textarea>
</div>
<div>
<button type="submit" class="btn">Send Message!</button>
</div>
</form>
</div>
PHP
<?php
$message_sent = false;
if(isset($_POST['email]') && $_POST['email'] != ''){
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messgeSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "email"
$body = "";
$body .= "From: ".$userName. "\r\n";
$body .= "Email: ".$userEmail. "\r\n";
$body .= "Message: ".$message. "\r\n";
mail($to,$messgeSubject,$body);
$message_sent = true;
}
}
?>

there is an typing error in your php (line no 3).. you have written
isset($_POST['email]'
it should be
isset($_POST['email'])

Related

adding a success message to a html form with php

I´m tring to add a success message to my form after you click submit, but i dont know how, i have tried different code from different websites, but they dont work with my code. What line do i need to add to this code please?
<form action="contact.php" method="post" id="contact">
<h1>Contact-me!</h1>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="name">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="name#example.com">
</div>
<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea name="message" class="form-control" id="message" rows="3"></textarea>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-3" value="SEND MESSAGE">Submit</button>
<div>
</div>
</div>
</form>
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = '';
$email_subject = "New Form Submission";
$email_body = "User Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$to = "";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: index.html");
?>

PHP: How to clear form after submit and show success alert?

I've been having trouble with PHP (new to php) to clear my form fields AFTER a successful submission. I would also like to show an alert that it was successful. Any ideas? Using javascript with the onclick listener for the submit button will not work because it clears the fields before PHP has time to send the data.
Here is my html:
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-strech" data-aos="fade-left">
<form id="contact-form" action="contact-form-handler.php" method="post" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 characters"/>
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" id="email" data-rule="email" data-msg="Please enter at valid email"/>
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter a subject of at least 8 characters"/>
<div class="validate"></div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please enter a message"></textarea>
<div class="validate"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
Here is my PHP:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "Portfolio Email";
$email_body = "Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$email_to = "email#gmail.com";
$headers = "From: $email_from \r\n";
mail($email_to, $subject, $email_body);
?>
If you do not want to use ajax, please try the following
(assuming the html is known as "form.html", and the php is "contact-form-handler.php")
The php redirection to form.html will clear the input fields (a normal behavior in web-browsers), and the javascript will show the word "successful" as an alert.
Please make further adjustment(s) according to your needs.
form.html
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-strech" data-aos="fade-left">
<form id="contact-form" action="contact-form-handler.php" method="post" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 characters"/>
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" id="email" data-rule="email" data-msg="Please enter at valid email"/>
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter a subject of at least 8 characters"/>
<div class="validate"></div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please enter a message"></textarea>
<div class="validate"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
<script>
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
</script>
<script>
if (get("result")=="success")
{
alert("Successful");
}
</script>
contact-form-handler.php
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "Portfolio Email";
$email_body = "Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$email_to = "email#gmail.com";
$headers = "From: $email_from \r\n";
mail($email_to, $subject, $email_body);
?>
<script>
window.location.href="form.html?result=success"
</script>

Bootstrap 4 form not sedning any messages

My bootstrap 4 form is not sending any messages, I've uploaded the index.php and contact.php files on the server and I'm not having any response. Would you be able to help me find the problem?
Landing page with simple form
<form id="#contacts" method="POST" class="form" role="form">
<div class="row">
<div class="col-md-6 form-group">
<input class="form-control" id="name" name="name"
placeholder="Podaj swoje Imię i nazwisko" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="website" name="website"
placeholder="Adres strony www" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="telephone" name="telephone"
placeholder="Podaj swój numer telefonu" type="text" required/>
</div>
<div class="col-md-6 form-group">
<input class="form-control" id="email" name="email"
placeholder="Podaj swój email" type="email" required/>
</div>
</div>
<textarea class="form-control" id="message" name="message" placeholder="Twoja wiadomość"
rows="4"></textarea>
<br>
<div class="row">
<div class="col-md-12 form-group">
<button class="btn btn-primary" type="submit">Wyślij</button>
</div>
</div>
</form>
contact.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$website = $_POST['website'];
$telephone = $_POST['telephone'];
$email_from = 'SEO';
$email_subject = 'Wiadomość kontaktowa z... '
$email_body = "Name: $name.\n".
"Email: $email.\n".
"Message: $message.\n";
"Website: $website.\n";
"Telephone: $telephone.\n";
$to = "biuro#of.pl";
$headers = "From: $email_from r\n";
$headers .= "Reply-To: $email r\n";
mail($to,$email-$email_subject,$email_body,$headers);
header("location: index.php");
?>
Your form is missing Action
<form id="#contacts" method="POST" class="form" role="form" action="contact.php">

contact form using php in angularjs

html :
View/contactform.html
<form method="post" role="form" name="myemailform" action="sendingemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" cols="10" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><input type="submit" class="btn applyBtn" name="submit"/></div>
</form>
PHP:
view/sendingemail.php
<?php
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$emails = "From: $email_from \r\n";
$emails .= "Name: $name \r\n";
$emails .= "Message: $message \r\n";
mail('sunitha_anilkumar#yahoo.com',$subject,$emails);
echo 'Mail sent!';
header('Location: thank-you.html');
?>
app.js
.when('/contactus', {
templateUrl: 'view/contactform.html',
controllerAs:'vm'
})
I wanted a contact form in my angular project and I given the form in view/contactform.html and sendingemail.php for submitting the form details. But when I submit the form, i am just seeing the sendingemail.php code , no form submission is done.Need assistance.
This means your webserver does not recognise your php file to be run as a php file. You might want to check your webserver's documentation to install and/or enable the PHP module.

PHP contact form not working right

I've been working on a contact form. I have added used the POST method to send it to a set email address. It doesn't seem to be working though. It just runs and stops as if the code is broken. The HTML and PHP are below.
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="name">Your name</label> <input class=
"form-control" id="name" name="name" type="text">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email"
type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class=
"form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name=
"message" rows="6">
</textarea>
</div>
<div class="submit">
<input class="button button-small" type="submit"
value="Send">
</div>
</form>
<?php
if(isset($_POST['submit'])) {
$to = "gfrazer#hotmail.co.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $name . " " . " wrote the following: " . "\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
header('Location: http://www.google.co.uk');
}
?>
You need to add a name to your submit button. From your snippit, you have no $_POST['submit']:
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email" type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class="form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name="message" rows="6"></textarea>
</div>
<div class="submit">
<!-- ADD name="submit" -->
<input name="submit" class="button button-small" type="submit" value="Send" />
</div>
</form>

Categories