I have this simple email form using the POST function with a PHP mailto() function. I have PHP lines within my form as values to the fields. It takes way too long to send. Any help is appreciated.
PHP:
<?php
if ($_POST['submit']) {
if (!$_POST['name']) {
$error="<br/>-Please enter your name";
}
if (!$_POST['email']) {
$error.="<br/>-Please enter your email";
}
if (!$_POST['message']) {
$error.="<br/>-Please enter a message";
}
if (!$_POST['check']) {
$error.="<br/>-Please confirm you're real";
}
if ($error){
$result="Ohh no, somethings not right... $error";
}
else {
mail("email#email.com","website contact form","Name: ".$_POST['name']."
Email: ".$_POST['email']."
Message: ".$_POST['message']);
{
$result="Thank you, I will reply shortly.";
}
}
}
?>
HTML:
<form action="#contactme" method="post" role="form">
<div class="form-group">
<input type="text" name="name" placeholder="Enter your name" class="form-control form-control-lg" value="<? echo $_POST['name']; ?>" >
</div>
<div class="form-group">
<input type="email" name="email" placeholder="Enter your email" class="form-control form-control-lg" value="<? echo $_POST['email']; ?>">
</div>
<div class="form-group">
<textarea name="message" rows="5" class="form-control" placeholder="Your message..."><? echo $_POST['message']; ?> </textarea>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="check"><span class="real"> I am real</span>
</label>
</div>
<div align="center">
<input type="submit" name="submit" class="btn btn-secondary btn-lg" value="Send">
</div>
</form>
I have done everything in a query now it should be sent faster but it can also be that this code has an error but the first query is correct from the structure.
<?php
if ($_POST['submit']) {
if (!$_POST['name'] || !$_POST['email'] || !$_POST['message'] || !$_POST['check'] ||) {
$error="<br/>-Please enter";
}else
{
if ($error){
$result="Ohh no, somethings not right... $error";
}
else {
mail("email#email.com","website contact form","Name: ".$_POST['name']."
Email: ".$_POST['email']."
Message: ".$_POST['message']);
{
$result="Thank you, I will reply shortly.";
}
} <-- If you have a mistake, take a clip away.
}
}
?>
Related
Im trying to create ajax from with php validation, let explain with screenshot. while validating form im getting all error in same place…
what im trying to get is individual validation for field like below
And after form is submitted i want display success message at the top of form but im getting success message on all field
html form data (index.php)
<form id="first_form" method="POST" action="form.php">
<p class="form-message"></p>
<div class="mb-3">
<input type="text" class="form-control" id="name" name="name" placeholder="Name:">
<p class="form-message"></p>
</div>
<div class="mb-3">
<input type="tex" class="form-control" id="email" name="email" placeholder="Email:">
<p class="form-message"></p>
</div>
<div class="mb-3">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone:">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="address" name="address" placeholder="Address:">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject:">
</div>
<div class="mb-3">
<input type="text" class="form-control" id="services" name="services" placeholder="Services:">
</div>
<div class="mb-3">
<textarea class="form-control" rows="5" id="message" name="message" placeholder="Leave Your Message..."></textarea>
</div>
<button type="submit" name="submit" id="submit" class="btn btn-primary">Submit</button>
js code:
$(document).ready(function() {
$("#first_form").submit(function(event) {
event.preventDefault();
name = $("#name").val();
email = $("#email").val();
phone = $("#phone").val();
address = $("#address").val();
subject = $("#subject").val();
services = $("#services").val();
message = $("#message").val();
submit = $("#submit").val();
$(".form-message").load("form.php", {
name: name,
email: email,
phone: phone,
address: address,
subject: subject,
services: services,
message: message,
submit: 'submitted'
});
});
});
form.php data
if (isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = "Contact Form";
$mailTo = "hello#develoweb.com";
$headers = "From: ".$email;
$txt = "You have received a message from ".$name.
".\n\n".$message;
$errorEmptyname = false;
$errorEmptyemail = false;
$errorEmptymessage = false;
$errorEmail = false;
if (empty($name)) {
echo "<span class='form-error'>Name Field cannot be empty</span> ";
//$errorEmptyName = true;
}
if (empty($email)) {
echo "<span class='form-error'>Email Field cannot be empty</span>";
//$errorEmptyemail = true;
}
if (empty($message)) {
//$errorEmptymessage = true;
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'>
Write a valid e-mail addres </span>";
$errorEmail = true;
}
else {
echo "<span class='form-success'>
Thank you for your message!<br>
I'll get back to you as soon as I can.</span>";
mail($mailTo, $subject, $body, $headers);
}
}
else{
echo "There was an error!";
}
what i have understand, im not getting individual validation error in individual field bcz class is same, how can i append error to related fields
i havent used js validation bcz i want to some validation for spammer to prevent spam mail and as we know while viewing source code we can easily view code for validation too, so pefer validation in php
<?php
function goback()
{
header("refresh:1; url=index.php");
exit;
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comment = $_POST['comment'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$to ='xyz#gmail.com';
$subject= 'Contact form pharma';
$message ="Name: ".$name."\n".
"Comment: ".$comment."\n".
"Mobile: ".$mob."\n".
"E-mail: ".$email;
if(mail($to, $subject, $message)){
echo "<script type='text/javascript'>alert('message successfully sent');window.close();</script>";
goback();
}
else
{
echo "something went wrong";
}
}
?>
Index.php on this page we have a form request acallback for which we use a html form and mail Function
<form>
<fieldset>
<legend>
<center>Request a call back</center>
</legend>
<div class="form-group">
<label class="control-label" for="requestid">Your Name</label>
<div>
<input id="requestid" name="name" placeholder="Name please" class="form-control input-md" required="" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">Your Comment</label>
<div>
<textarea class="form-control" placeholder="Your Comment" id="comment" name="comment"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">Mobile No</label>
<div>
<input placeholder="1234-456-7890" class="form-control input-md" id="phonenum" name="mob" type="tel" required >
</div>
</div>
<div class="form-group">
<label class="control-label" for="dis">E-mail</label>
<div>
<input placeholder="xyz#example.com" class="form-control input-md" id="email" name="email" type="email" required >
</div>
</div>
<div class="form-group">
<div class="text-right">
<button id="submit" name="submit" class="btn btn-info">Submit Message</button>
</div>
</div>
</fieldset>
</form>
I want to display this alert box on the same page. It get redirect to blank page then shows alert box . If goback() removed then it redirects to blank page and does no return back to previous page.
how to solve this?
You are missing something. How it can be possible to display a popup related to another page without sending any request to it? First I have set a success session in mail.php page which is set if mail is sent successfully.
<?php
session_start();
function goback()
{
$_SESSION['success'] = 'true';
header("location:index.php");
}
if(isset($_POST['submit'])){
$name = $_POST['name'];
$comment = $_POST['comment'];
$mob = $_POST['mob'];
$email = $_POST['email'];
$to ='xyz#gmail.com';
$subject= 'Contact form pharma';
$message ="Name: ".$name."\n".
"Comment: ".$comment."\n".
"Mobile: ".$mob."\n".
"E-mail: ".$email;
if(mail($to, $subject, $message)){
goback();
}
else
{
echo "something went wrong";
}
}
?>
Then in index.php page we just have to call this session. if its true then we can show pupop
<?php
if(isset($_SESSION['success']) && $_SESSION['success']=='true')
{
//show pupop
$_SESSION['success'] = 'false';
}
?>
Newbie here, I found the problem when I tried to implement the code onto my website, which is made with bootstrap. I also made this weather scraper with bootstrap and php and that one ended up working under the default index.php ie. website.com
Code. Replaced my email with myEmail
PHP
<?php
if ($_POST["submit"]) {
if (!$_POST['name']) {
$error="<br />Please enter your name";
}
if (!$_POST['email']) {
$error.="<br />Please enter your email address";
}
if (!$_POST['comment']) {
$error.="<br />Please enter a comment";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if ($error) {
$result='<div class="alert alert-danger"><strong>There were errors(s) in your form:</strong>'.$error.' </div>';
} else {
if (mail("myEmail", "Comment from Website!", "Name: ".$_POST['name']."
Email: ".$_POST['email']."
Comment: ".$_POST['comment'])) {
$result='<div class="alert alert-success"><strong>Thank you!</strong>I\'ll be in touch.</div>';
} else {
$result='<div class="alert alert-danger"><strong>Sorry, there was an error
sending your message. Please try again.</strong></div>';
}
}
}
?>
HTML
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3 emailForm">
<h1>Email Us</h1>
<?php echo $result; ?>
<p class="lead">Please get in touch, and we will get back to you asap.</p>
<form method="post">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" class="form-control" placeholder="Name"
value="<?php echo $_POST['name']; ?>" />
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" name="email" class="form-control" placeholder="Email"
value="<?php echo $_POST['email']; ?>" />
</div>
<div class="form-group">
<label for="comment">Comment:</label>
<textarea class="form-control" name="comment" value="<?php echo $_POST['comment']; ?>"></textarea>
</div>
<input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit" />
</form>
</div>
</div>
</div>
I cant see any obvious but try this:
<form method="post">
to
<form method="post" action="/index.php">
If the location of the PHP code is http://website.com/index.php
I have a simple php contact form i got from a web tutorial. It worked yesterday, but will not work today. I'd love some help, as I don;t know much php.
php:
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!filter_var( trim($_POST['email'], FILTER_VALIDATE_EMAIL ))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'person#domain.com'; // Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
HTML:
<form role="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="alert alert-danger">Please check if you've filled all the fields with valid information and try again. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<div class="alert alert-success">
<p><strong>Message Successfully Sent!</strong></p>
<p>Thank you for using our contact form, <strong><?php echo $name;?></strong>! Your email was successfully sent and we’ll be in touch with you soon.</p>
</div>
<?php } ?>
<div class="form-group">
<label for="name">Your Name<span class="help-required">*</span></label>
<input type="text" name="contactname" id="contactname" value="" class="form-control required" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="email">Your Email<span class="help-required">*</span></label>
<input type="text" name="email" id="email" value="" class="form-control required email" role="input" aria-required="true" />
</div>
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
<div class="form-group">
<label for="message">Message<span class="help-required">*</span></label>
<textarea rows="8" name="message" id="message" class="form-control required" role="textbox" aria-required="true"></textarea>
</div>
<div class="actions">
<input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-grey" title="Click here to submit your message!" />
<input type="reset" value="Clear Form" class="btn btn-grey pull-right" title="Remove all the data from the form." />
</div>
</form>
It gets hung up on the validation. Not sure why.
$_POST["subject] is not defined in your form. Your SUBJECT field is called EMAIL:
Change:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="email" id="subject" class="form-control required" role="input" aria-required="true">
</div>
With:
<div class="form-group">
<label for="subject">Subject<span class="help-required">*</span></label>
<input type="text" name="subject" id="subject" class="form-control required" role="input" aria-required="true">
</div>
I am a beginner in PHP. I am stuck on something that seems really easy.
I built a website, and there is a page in the website where people can send me e-mail through a form. The problem is, I think form seems to fail creating $POST_['name']. Just read the codes and you'll understand what im saying.
<form action="?" method="post">
<div id="right">
<label for="name"> <input type="text" class="kutu" name="name" ></label><br />
<label for="email"> <input type="email" class="kutu" name="email" ></label><br />
<label for="subject"> <input type="text" class="kutu" name="subject" ></label><br />
</div>
<div id="left">
<p>Name: </p>
<p>Your Email Adress: </p>
<p>Subject: </p>
</div>
<div id="bottom">
<p>Your Message:</p><br />
<label for="message"><textarea name="message" rows="3" cols="60"> </textarea></label>
</div>
<input type="submit" id="tus" value="submit">
</form>
And here is the PHP
if (isset($POST_['name'])) {
$email = $POST_['email'];
$subject = $POST_['subject'];
$message = "From: " . $POST_['name'] . ", " . $POST_['email'] . "\n Message: " . $POST_['message'];
try {
mail("myEmailAdress#gmail.com", $subject, $message, " ");
unset($POST_['name']);
header("Location: success.php");
}
catch (PDOException $e)
{
include 'index.html';
exit();
}
exit();
}
include 'contact2.php';
When i submit my form, it takes me to contact2.php again, which tells me that POST_['name'] was never created in the first place.
The variable name is $_POST, not $POST_. See http://php.net/EN/reserved.variables.post.php