Contact form not working while click on submit button - php

I was working with an HTML contact form, its action is a mail sending php script. But it is not working when i am clicking on the send button. I couldn't find any possible here. Please someone help me to fix this.
HTML form
<div class="contact-form">
<form action="sendmail.php" method="post">
<div class="control-group"><label class="nameLabel" for="name">Name</label> <input id="name" name="name" type="text" /></div>
<div class="control-group"><label class="emailLabel" for="email">Email</label> <input id="email" name="email" type="text" /></div>
<div class="control-group"><label class="messageLabel" for="message">Message</label><textarea id="message" name="message"></textarea></div>
<div class="control-group"><button type="submit">Send</button></div>
</form>
<h1 class="status-message-contact"></h1>
</div>
PHP code
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$to="sarath.sarigama#gmail.com";
$subject="WEB Enquiry!";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="
Name:
$name
<br>
Email-Id:
$email
<br>
Message:
$query
";
if(mail($to,$subject,$message,$headers))
header("Location:../index.php?msg=Successful Submission! Thankyou for contacting us.");
else
header("Location:../index.php?msg=Error To send Email !");
//contact:-your-email#your-domain.com
}
?>

you dont have submit button name ,change name of the button
<button name='submit' type="submit">Send</button>
or use input tag
<input name='submit' type='submit'>

Replace
<button type="submit">Send</button>
with
<input type="submit" value="Send" />
And maybe add
<input type="hidden" name="submit" value="1" />
For the isset( $_POST['submit'] )

Related

PHP / HTML Contact form - blank fields

I've made a custom contact form with PHP working with HTML but I am getting some blank fields when sending an email.
Actually, I have made a table of 4 including Name, Email, Subject, and Message but the fields including Subject & Message are being sent empty.
I would appreciate any help given.
Thank you.
Html Code:
<form action="mail.php" method="post">
<div class="form-block clearfix">
<input type="text" placeholder="name*" id="name" />
<input type="text" placeholder="email*" id="email" />
</div>
<div class="form-block clearfix">
<input type="text" placeholder="subject*" id="sub" />
</div>
<div class="form-block">
<textarea cols="1" rows="1" placeholder="Message*" id="message" ></textarea>
</div>
<div class="submit-btn">
<input type="button" id="submit" value="submit" class="detail-submit"/>
</div>
</form>
PHP:
<?php
$to = "My email";
$from = "";
$cc = "";
$subject = "Contact us form";
$errmasg = "";
$name = htmlentities(trim($_POST['name']));
$email = htmlentities(trim($_POST['email']));
$sub = htmlentities(trim($_POST['sub']));
$message = htmlentities(trim(nl2br($_POST['message'])));
if($email){
$message = "<table border='0' cellpadding='2' cellspacing='2' width='600'>
<tr><td>Name: ".$name." </td></tr>
<tr><td>Email: ".$email."</td></tr>
<tr><td>Subject: ".$sub."</td></tr>
<tr><td>Message:".$message."</td></tr>
</table>";
}else{
$errmasg = "No Data";
}
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$from . "\r\n";
$headers .= 'Cc:'.$cc . "\r\n";
if($errmasg == ""){
if(mail($to,$subject,$message,$headers)){
echo 1;
}else{
echo 'Error occurred while sending email';
}
}else{
echo $errmasg;
}
?>
You need to add an element NAME in the subject and message field.
Just replace your form code to below code:
<form action="mail.php" method="post">
<div class="form-block clearfix">
<input type="text" placeholder="name*" id="name" />
<input type="text" placeholder="email*" id="email" />
</div>
<div class="form-block clearfix">
<input type="text" name="sub" placeholder="subject*" id="sub" />
</div>
<div class="form-block">
<textarea cols="1" rows="1" name="message" placeholder="Message*" id="message" ></textarea>
</div>
<div class="submit-btn">
<input type="button" id="submit" value="submit" class="detail-submit"/>
</div>
</form>
Each form element that you wish to appear in the POST array data when the form is submitted ( and thus to be available using $_POST['fieldname'] ) needs a name attribute. The ID attribute is optional but of limited use in many situations - certainly not required in a traditional form submission such as this..
The input button submit will NOT submit the form unless you do so with Javascript. It might be better to use a submit button as below.
<form action="mail.php" method="post">
<div class="form-block clearfix">
<input type="text" placeholder="name*" name="name" />
<input type="text" placeholder="email*" name="email" />
</div>
<div class="form-block clearfix">
<input type="text" placeholder="subject*" name="sub" />
</div>
<div class="form-block">
<textarea cols="100" rows="1" placeholder="Message*" name="message" ></textarea>
</div>
<div class="submit-btn">
<input type="submit" name="submit" value="Submit" class="detail-submit"/>
</div>
</form>
Not sure why you are having issues ~ perhaps the following will offer enlightenment. It is tested to the point of failing to send an email ( no local mailserver on dev machine at present ) and is an "all in one page" demo where the PHP emulates the original form action mail.php
<?php
/* this emulates mail.php */
error_reporting( E_ALL );
/* use a session variable */
session_start();
/* for testing single page demo */
$singlepage=true;
if( $_SERVER['REQUEST_METHOD']=='POST' ){
$to = "My email";
$from = $cc = '';
$subject = "Contact us form";
$errmasg = '';
/* filter POST data */
$args=array(
'name' => FILTER_SANITIZE_STRING,
'email' => FILTER_SANITIZE_EMAIL,
'sub' => FILTER_SANITIZE_STRING,
'message' => FILTER_SANITIZE_STRING
);
$_POST=filter_input_array( INPUT_POST, $args );
/* assign as variables */
extract( $_POST );
$name = htmlentities(trim($name));
$email = htmlentities(trim($email));
$sub = htmlentities(trim($sub));
$message = htmlentities(trim(nl2br($message)));
if( $email ){
$message = "<table border='0' cellpadding='2' cellspacing='2' width='600'>
<tr><td>Name: ".$name." </td></tr>
<tr><td>Email: ".$email."</td></tr>
<tr><td>Subject: ".$sub."</td></tr>
<tr><td>Message:".$message."</td></tr>
</table>";
}
# REMOVE THIS LINE or COMMENT IT OUT FOR REAL USAGE
#exit( sprintf("<pre>%s\n%s</pre>",$message, print_r( $_POST,true ) ) );
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From:'.$from . "\r\n";
$headers .= 'Cc:'.$cc . "\r\n";
if($errmasg == ""){
if( mail( $to, $subject, $message, $headers ) ){
$_SESSION['mailsent']=1;
}else{
$_SESSION['mailsent']=2;
}
}else{
$_SESSION['mailsent']=3;
}
/*
If you are using mail.php then use a `header` to redirect the user
back to the contact page - assumed to be called `contact.php`
*/
if( !$singlepage ) header( 'Location: contact.php' );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>POST to email</title>
</head>
<body>
<!-- removed attribute action as this works on same page here -->
<form method="post">
<?php
if( !empty( $_SESSION['mailsent'] ) ){
switch( $_SESSION['mailsent'] ){
case 1:$message='Your message was sent successfully';break;
case 2:$message='Sorry - we had a problem sending your email';break;
case 3:$message='Bogus - no data';break;
}
printf( '<h1>%s</h1>', $message );
unset( $_SESSION['mailsent'] );
}
?>
<div class="form-block clearfix">
<input type="text" placeholder="name*" name="name" /><!-- element has a NAME -->
<input type="text" placeholder="email*" name="email" />
</div>
<div class="form-block clearfix">
<input type="text" placeholder="subject*" name="sub" />
</div>
<div class="form-block">
<textarea cols="100" rows="1" placeholder="Message*" name="message" ></textarea>
</div>
<div class="submit-btn">
<input type="submit" class="detail-submit" /><!-- a SUBMIT button -->
</div>
</form>
</body>
</html>
Typical output for debugging
Array
(
[name] => fred flintstone
[email] => fred#bedrock.com
[sub] => betty had better bake a cake
[message] => hey betty
)

php and html email forms

I am creating a website for my church choir in HTML. I have a "contact us" page written in HTML. It has a form for the user to send the choir director an email from the website. I am aware that I have to write the email in php in order for the email to send. Do I need to duplicate the html file and rewrite it in PHP?
<form name="Send-mail" action="mailto:email#email.com" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
I need the server to send the email from the website to the email of our choir director.
You would need to create the PHP file where there is an email sending logic,eg: sendEmail.php. The form tag would change to:
<form name="Send-mail" action="sendEmail.php" method="post">
Below is how you can send the mail in php from form inputs.
<form action="mail.php" method="post">
<label>Name:</label>
<input class="nice" name="name" type="text"/>
<label>Email:</label>
<input class="nice" name="email" type="text"/>
<label>Subject:</label>
<input class="nice" name="subject" type="text"/>`enter code here`
<label>Message:</label>
<text-area type="text" name="message"></text-area>
<input class="centered" type="submit" name="submit" value="Send email"/>
</form>
You will have to name your php file mail.php like in case below
mail.php
<?php
$to = $_POST['email'];
$subject = $_POST['subject'];
$sender = $_POST['name'];
$message = $_POST['message'];
$message .= "<h1>This is Message from $sender.</h1>";
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
You will need to ensure that email for this line of code is correctly set.
$header = "From:senderemail#somedomain.com \r\n";
$header .= "Cc:senderemail#somedomain.com \r\n";
In otherword, if your site email is support#hey.com
Try Replacing it snderemail#somedomain and it will work. You can also try the replacement with some other emails like good working gmail and let me know what happen. Thanks

PHP Contact Form Not Sending Email

I'm writing to create a contact form on my website and then get the information sent to my inbox, however it is not working whatsoever. Please take a look at my code below (PHP is not my thing) and let me know where i've gone wrong.
Here's the PHP script:
<?php
$to = 'example#gmail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$tel = $_POST['tel'];
$project = $_POST['project'];
$range1 = $_POST['range1'];
$range2 = $_POST['range2'];
$body = <<<EMAIL
Hi, my name is $name.
I'd like to discuss the possibility of working together on a $project.
My budget for the project is £$range1 and I would like to complete the project within $range2 Month(s).
Additional Information:
$message
Regards, $name
<hr>
$name
$email
$tel
EMAIL;
$header = "From: $email";
if($_POST['send']){
mail($to, $subject, $body, $header);
$feedback = 'Thank you for your message, we will get back to you shortly.';
}
?>
And here's the HTML form:
<form id="form_id" name="form_name" action="" method="post">
<div>
<input type="text" name="name" id="name" placeholder="Name" required/>
</div>
<div>
<input type="email" name="email" id="email" placeholder="Email" required/>
</div>
<div>
<input type="tel" name="tel" id="tel" placeholder="Phone" required/>
</div>
<div>
<select required id="project">
<option selected>Select type of project…</option>
<option value="Responsive Website">Responsive Web Design</option>
<option value="Graphic Design">Graphic Design</option>
<option value="Motion Graphics">Motion Graphics</option>
</select>
</div>
<div>
<label for="range1">Budget: </label>
<input type="range" name="range1" id="range1" min="400" max="2000" step="50" value="6" required onchange="rangevalue.value=value"><output id="rangevalue">400</output>
</div>
<div>
<label for="range2">Timeframe: </label>
<input type="range" name="range2" id="range2" min="1" max="12" step=".5" value="1" required onchange="rangevalue1.value=value"><output id="rangevalue1">1</output>
</div>
<div>
<label for="message">Additional Information: </label><br/>
<p>(Please use this space to tell us about your company, the main objectives of the proposed website and anything else you think might be useful)</p>
<textarea name="message" id="message" rows="5" cols="30" placeholder="Message" required></textarea>
</div>
<div>
<input type="submit" name="submit" value="submit" />
</div>
</form>
<p id="feedback"><?php echo $feedback; ?></p>
Thanks for the help. FYI this can be achieved easily with WordPress through Contact Form 7 (or a similar plugin).
<input type="submit" name="submit" value="submit" />
You have to change this line to:
<input type="submit" name="send" value="submit" />
if($_POST['send']){
actually checking if submit button is clicked...
And, yes - if html and php are on different pages, you have to set proper form action link...
I think you should take a look at this or this, be aware that despite W3Schools may serve as a basics tutorial because of the friendly-user examples, always complement with other resources look here why.
Then you can take a look at this answers, this is just because you need some more basis to understand everything you are doing.
I can't see your $_POST['send'] name in the form, or it's just too late and I'm tired:
Don't know, but maybe, you want this in your form before the submit:
<input type="hidden" name="send" >
Then:
I already posted this answer HERE, but here it is:
First i think your missing some headers look at those
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
2nd check if its true or false when calling the mail function
if( mail($email_to, $email_subject, $email_message, $headers)!==true)
{
die('Fail to send');
}
die('Success');
}
You should take a look at the isset() function to make sure all your variables are set from the form.

Can someone find where's wrong in this code PHP email

I'm not receiving mails on the email mail#example.com. Below is my form code and my send-mail.php code. Can anyone help me with this cause everything seems working great bu i'm not receiving any emails. I'm using localhost as the server.
Contact form:
<form id="contactForm" action="#" method="post">
<p>Email us by filling in the form below. Make sure you fill in the message and all fields.</p>
<fieldset>
<div>
<input name="name" id="name" type="text" class="form-poshytip" title="Enter your name" />
<label>Name</label>
</div>
<div>
<input name="web" id="web" type="text" class="form-poshytip" title="Enter your surname" />
<label>Surname</label>
</div>
<div>
<input name="email" id="email" type="text" class="form-poshytip" title="Enter your email address" />
<label>Email</label>
</div>
<div>
<textarea name="comments" id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
</div>
<!-- send mail configuration -->
<input type="hidden" value="mail#example.com" name="to" id="to" />
<input type="hidden" value="Enter the subject here" name="subject" id="subject" />
<input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
<!-- ENDS send mail configuration -->
<p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
</fieldset>
</form>
<p id="sent-form-msg" class="success">Form data sent. Thanks for your feedback.</p>
<!-- ENDS form -->
and here is the send-mail.php
<?php
//vars
$subject = $_POST['subject'];
$to = explode(',', $_POST['to'] );
$from = $_POST['mail#example.com'];
//data
$msg = "NAME: " .$_POST['name'] ."<br>\n";
$msg .= "EMAIL: " .$_POST['email'] ."<br>\n";
$msg .= "WEBSITE: " .$_POST['web'] ."<br>\n";
$msg .= "COMMENTS: " .$_POST['comments'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send for each mail
foreach($to as $mail){
mail($mail, $subject, $msg, $headers);
}
?>
$_POST['subject'];
$_POST['to'];
$_POST['myemail#gmail.com'];
$_POST['name'];
$_POST['email'];
$_POST['web'];
$_POST['comments'];
I didn’t find any of these elements in your form. That's the reason why nothing is happening.Try
echo '<pre>';
print_r($_POST);
This will give you the posted array when the form is submitted.
i have some suggestion.if u have kept the 'to' address as hidden in the form then why cant u try keeping it directly in sendmail function and in $from you try to keep
<?php
$to="kurtfarrugia92#gmail.com";
$from =$_POST['field_name'];
// not the mail id because i didn't see any field with name as "kurtfarrugia92#gmail.com"
?>
You cannot use this function to send mail from localhost. I am not sure but you should try PHP mailer for this task.

Jquery form plugin not submitting to process page

I have a basic contact form that uses the Jquery form plugin. I cannot get it to submit using the plugin. It only submits if I add a php include to process.php.
Javascript:
$(document).ready(function() {
$("#contact").validate({
submitHandler: function(form) {
$(form).ajaxSubmit({
url:"process.php",
type:"post",
});
}
});
});
And here is my contact form:
<form id="contact" method="post" name="validation" action="contact.php" onsubmit="return validateForm();">
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Full Name" title="Enter your name" class="required">
<label for="email">E-mail</label>
<input type="email" name="email" placeholder="yourname#domain.com" title="Enter your e-mail address" class="required email">
<label for="phone">Phone</label>
<input type="tel" name="phone" placeholder="ex. (555) 555-5555">
<label for="message">Question/Comment</label>
<textarea name="message"></textarea>
<input type="submit" name="submitted" class="button" id="submit" value="Send Message" />
</fieldset>
</form>
Process.php
<?php
function GetHeaders()
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Company Name<info#companyname.com>' . "\r\n";
return $headers;
}
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$message = strip_tags($_POST['message']);
// Send Message
$headers = GetHeaders();
$intro = "\"Thank you for contacting Company Name. We are very interested in assessing your situation and will be in touch as soon possible.\" <br />
<br/>
Best Regards,<br/>
<br/>
Company<br/>
";
mail($email, "RE: Contact Form Submission", $intro, $headers);
?>
Thanks for any help in advance.
You need to remove the onsubmit="return validateForm(); from your form attributes. You are already validating using $("#contact").validate({ in your query ready function
Also, your form seems to be submitting to contact.php, not process.php
action="contact.php"

Categories